Skip to content

Commit dc4da7b

Browse files
authored
Add golangci/golangci-lint (#181)
* Add golangci/golangci-lint * Fix golanglint
1 parent 71464b4 commit dc4da7b

10 files changed

+94
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7+
tools/bin
78

89
# Test binary, built with `go test -c`
910
*.test

.golangci.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
run:
2+
timeout: 10m
3+
linters:
4+
disable-all: true
5+
enable:
6+
- misspell
7+
- ineffassign
8+
- typecheck
9+
- unused
10+
- gosimple
11+
- errcheck
12+
- staticcheck
13+
- stylecheck
14+
- gosec
15+
- asciicheck
16+
- bodyclose
17+
- exportloopref
18+
- rowserrcheck
19+
- unconvert
20+
- makezero
21+
- durationcheck
22+
- prealloc
23+
- predeclared
24+
25+
linters-settings:
26+
staticcheck:
27+
checks: ["S1002","S1004","S1007","S1009","S1010","S1012","S1019","S1020","S1021","S1024","S1030","SA2*","SA3*","SA4009","SA5*","SA6000","SA6001","SA6005", "-SA2002"]
28+
stylecheck:
29+
checks: ["-ST1003"]
30+
gosec:
31+
severity: "low"
32+
confidence: "low"
33+
excludes:
34+
- G101
35+
- G107
36+
issues:
37+
exclude-rules:
38+
- path: _test\.go
39+
linters:
40+
- errcheck
41+
- gosec
42+
- rowserrcheck
43+
- makezero

Makefile

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,19 @@ all:
1919
make clean
2020
./scripts/build.sh $(OS) $(ARCH)
2121

22-
test:
22+
tools-dir:
23+
mkdir -p tools/bin
24+
25+
install-staticcheck: tools-dir
26+
GOBIN=`pwd`/tools/bin go install honnef.co/go/tools/cmd/staticcheck@latest
27+
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b ./tools/bin v1.51.2
28+
29+
static-check: install-staticcheck
30+
#S1000,SA1015,SA4006,SA4011,S1023,S1034,ST1003,ST1005,ST1016,ST1020,ST1021
31+
tools/bin/staticcheck -checks all,-ST1000 ./...
32+
GO111MODULE=on tools/bin/golangci-lint run -v $$($(PACKAGE_DIRECTORIES)) --config .golangci.yml
33+
34+
test: static-check
2335
go test -v -coverprofile=coverage.txt -covermode=atomic
2436

2537
clean:

config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var (
2323
remoteRaw = "remote-raw"
2424
config Config
2525
version = "0.5.0"
26-
releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/"
26+
releaseURL = "https://github.com/webp-sh/webp_server_go/releases/latest/download/"
2727
)
2828

2929
const (

encoder.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func convertImage(raw, optimized, itype string) {
5555

5656
//we need to create dir first
5757
err = os.MkdirAll(path.Dir(optimized), 0755)
58+
if err != nil {
59+
log.Error(err.Error())
60+
}
5861
//q, _ := strconv.ParseFloat(config.Quality, 32)
5962

6063
switch itype {
@@ -137,7 +140,7 @@ func webpEncoder(p1, p2 string, quality float32) error {
137140
log.Warnf("Can't encode source image: %v to WebP", err)
138141
}
139142

140-
if err := os.WriteFile(p2, buf.Bytes(), 0644); err != nil {
143+
if err := os.WriteFile(p2, buf.Bytes(), 0600); err != nil {
141144
log.Error(err)
142145
return err
143146
}

helper.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ func checkAllowedType(imgFilename string) bool {
8484

8585
// Check for remote filepath, e.g: https://test.webp.sh/node.png
8686
// return StatusCode, etagValue and length
87-
func getRemoteImageInfo(fileUrl string) (int, string, string) {
88-
res, err := http.Head(fileUrl)
87+
func getRemoteImageInfo(fileURL string) (int, string, string) {
88+
res, err := http.Head(fileURL)
8989
if err != nil {
9090
log.Errorln("Connection to remote error!")
9191
return http.StatusInternalServerError, "", ""
9292
}
93+
defer res.Body.Close()
9394
if res.StatusCode != 404 {
9495
etagValue := res.Header.Get("etag")
9596
if etagValue == "" {

router.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ func convert(c *fiber.Ctx) error {
6969

7070
var availableFiles = []string{rawImageAbs}
7171
for _, v := range goodFormat {
72-
if "avif" == v {
72+
if v == "avif" {
7373
availableFiles = append(availableFiles, avifAbs)
7474
}
75-
if "webp" == v {
75+
if v == "webp" {
7676
availableFiles = append(availableFiles, webpAbs)
7777
}
7878
}

router_test.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func TestServerHeaders(t *testing.T) {
5252

5353
// test for chrome
5454
response, _ := requestToServer(url, app, chromeUA, acceptWebP)
55+
defer response.Body.Close()
5556
ratio := response.Header.Get("X-Compression-Rate")
5657
etag := response.Header.Get("Etag")
5758

@@ -60,7 +61,8 @@ func TestServerHeaders(t *testing.T) {
6061

6162
// test for safari
6263
response, _ = requestToServer(url, app, safariUA, acceptLegacy)
63-
ratio = response.Header.Get("X-Compression-Rate")
64+
defer response.Body.Close()
65+
// ratio = response.Header.Get("X-Compression-Rate")
6466
etag = response.Header.Get("Etag")
6567

6668
assert.NotEqual(t, "", etag)
@@ -96,14 +98,16 @@ func TestConvert(t *testing.T) {
9698

9799
// test Chrome
98100
for url, respType := range testChromeLink {
99-
_, data := requestToServer(url, app, chromeUA, acceptWebP)
101+
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
102+
defer resp.Body.Close()
100103
contentType := getFileContentType(data)
101104
assert.Equal(t, respType, contentType)
102105
}
103106

104107
// test Safari
105108
for url, respType := range testSafariLink {
106-
_, data := requestToServer(url, app, safariUA, acceptLegacy)
109+
resp, data := requestToServer(url, app, safariUA, acceptLegacy)
110+
defer resp.Body.Close()
107111
contentType := getFileContentType(data)
108112
assert.Equal(t, respType, contentType)
109113
}
@@ -119,12 +123,14 @@ func TestConvertNotAllowed(t *testing.T) {
119123

120124
// not allowed, but we have the file, this should return File extension not allowed
121125
url := "http://127.0.0.1:3333/webp_server.bmp"
122-
_, data := requestToServer(url, app, chromeUA, acceptWebP)
126+
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
127+
defer resp.Body.Close()
123128
assert.Contains(t, string(data), "File extension not allowed")
124129

125130
// not allowed, random file
126131
url = url + "hagdgd"
127-
_, data = requestToServer(url, app, chromeUA, acceptWebP)
132+
resp, data = requestToServer(url, app, chromeUA, acceptWebP)
133+
defer resp.Body.Close()
128134
assert.Contains(t, string(data), "File extension not allowed")
129135

130136
}
@@ -139,6 +145,7 @@ func TestConvertProxyModeBad(t *testing.T) {
139145
// this is local random image, should be 500
140146
url := "http://127.0.0.1:3333/webp_8888server.bmp"
141147
resp, _ := requestToServer(url, app, chromeUA, acceptWebP)
148+
defer resp.Body.Close()
142149
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
143150

144151
}
@@ -154,11 +161,13 @@ func TestConvertProxyModeWork(t *testing.T) {
154161
url := "https://webp.sh/images/cover.jpg"
155162

156163
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
164+
defer resp.Body.Close()
157165
assert.Equal(t, http.StatusOK, resp.StatusCode)
158166
assert.Equal(t, "image/webp", getFileContentType(data))
159167

160168
// test proxyMode with Safari
161169
resp, data = requestToServer(url, app, safariUA, acceptLegacy)
170+
defer resp.Body.Close()
162171
assert.Equal(t, http.StatusOK, resp.StatusCode)
163172
assert.Equal(t, "image/jpeg", getFileContentType(data))
164173
}
@@ -171,8 +180,9 @@ func TestConvertBigger(t *testing.T) {
171180
app.Get("/*", convert)
172181

173182
url := "http://127.0.0.1:3333/big.jpg"
174-
response, data := requestToServer(url, app, chromeUA, acceptWebP)
175-
assert.Equal(t, "image/jpeg", response.Header.Get("content-type"))
183+
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
184+
defer resp.Body.Close()
185+
assert.Equal(t, "image/jpeg", resp.Header.Get("content-type"))
176186
assert.Equal(t, "image/jpeg", getFileContentType(data))
177187
_ = os.RemoveAll(config.ExhaustPath)
178188
}

update.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ func autoUpdate() {
2525
}
2626
var res Result
2727
log.Debugf("Requesting to %s", api)
28-
resp1, _ := http.Get(api)
28+
resp1, err := http.Get(api)
29+
if err != nil {
30+
log.Errorf("Error requesting to %s", api)
31+
}
32+
defer resp1.Body.Close()
2933
data1, _ := io.ReadAll(resp1.Body)
3034
_ = json.Unmarshal(data1, &res)
3135
var gitVersion = res.TagName
@@ -42,13 +46,13 @@ func autoUpdate() {
4246
filename += ".exe"
4347
}
4448
log.Info("Downloading binary to update...")
45-
resp, _ := http.Get(releaseUrl + filename)
49+
resp, _ := http.Get(releaseURL + filename)
4650
if resp.StatusCode != 200 {
4751
log.Debugf("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
4852
return
4953
}
5054

51-
err := update.Apply(resp.Body, update.Options{})
55+
err = update.Apply(resp.Body, update.Options{})
5256
if err != nil {
5357
// error handling
5458
log.Errorf("Update error. %v", err)

update_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
package main
66

77
import (
8-
"github.com/stretchr/testify/assert"
98
"os"
109
"testing"
10+
11+
"github.com/stretchr/testify/assert"
1112
)
1213

1314
func TestNormalAutoUpdate(t *testing.T) {
@@ -21,7 +22,7 @@ func TestNormalAutoUpdate(t *testing.T) {
2122
func Test404AutoUpdate(t *testing.T) {
2223
version = "0.0.1"
2324
dir := "./update"
24-
releaseUrl = releaseUrl + "a"
25+
releaseURL = releaseURL + "a"
2526
autoUpdate()
2627
assert.Equal(t, int64(0), fileCount(dir))
2728
_ = os.RemoveAll(dir)

0 commit comments

Comments
 (0)