From 656dc6b1035c8b6d40c4cc8c2565a15d7785ca50 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Sun, 28 Aug 2022 17:33:35 +0800 Subject: [PATCH] all: fix golangci-lint --- .github/workflows/golint.yml | 6 ------ .golangci.yml | 20 ++++++++------------ coolq/cqcode.go | 8 ++++---- global/net.go | 25 +++++++++++-------------- main.go | 1 + server/http.go | 4 ++-- server/scf.go | 2 +- 7 files changed, 27 insertions(+), 39 deletions(-) diff --git a/.github/workflows/golint.yml b/.github/workflows/golint.yml index fb9cc4d..6ab8830 100644 --- a/.github/workflows/golint.yml +++ b/.github/workflows/golint.yml @@ -19,12 +19,6 @@ jobs: with: version: latest - - name: Static Check - uses: dominikh/staticcheck-action@v1.2.0 - with: - install-go: false - version: "2022.1" - - name: Tests run: | go test $(go list ./...) diff --git a/.golangci.yml b/.golangci.yml index 0a87b86..5f654d6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,10 +21,8 @@ linters: disable-all: true fast: false enable: - #- bodyclose - #- deadcode - #- depguard - #- dogsled + - bodyclose + - durationcheck - gofmt - goimports - errcheck @@ -32,18 +30,16 @@ linters: - exhaustive - bidichk - gocritic - #- gosimple + - gosimple - govet - ineffassign #- nolintlint - #- rowserrcheck - #- staticcheck - - structcheck - #- stylecheck + - staticcheck + - stylecheck - unconvert - #- unparam - #- unused - - varcheck + - usestdlibvars + - unparam + - unused - whitespace - prealloc - predeclared diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 7505576..283213b 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -733,11 +733,11 @@ func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType message case *message.GroupImageElement: img.Flash = flash img.EffectID = id - switch data["subType"].(type) { + switch sub := data["subType"].(type) { case int64: - img.ImageBizType = message.ImageBizType(data["subType"].(int64)) - default: - img.ImageBizType = message.ImageBizType(data["subType"].(uint32)) + img.ImageBizType = message.ImageBizType(sub) + case uint32: + img.ImageBizType = message.ImageBizType(sub) } case *message.FriendImageElement: img.Flash = flash diff --git a/global/net.go b/global/net.go index 6011232..0327a7e 100644 --- a/global/net.go +++ b/global/net.go @@ -5,7 +5,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -52,7 +51,7 @@ func GetBytes(url string) ([]byte, error) { defer func() { _ = reader.Close() }() - return ioutil.ReadAll(reader) + return io.ReadAll(reader) } // DownloadFile 将给定URL对应的文件下载至给定Path @@ -62,7 +61,7 @@ func DownloadFile(url, path string, limit int64, headers map[string]string) erro return err } defer file.Close() - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return err } @@ -112,7 +111,7 @@ func DownloadFileMultiThreading(url, path string, limit int64, threadCount int, } return errUnsupportedMultiThreading } - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return err } @@ -132,23 +131,21 @@ func DownloadFileMultiThreading(url, path string, limit int64, threadCount int, if resp.StatusCode < 200 || resp.StatusCode >= 300 { return errors.New("response status unsuccessful: " + strconv.FormatInt(int64(resp.StatusCode), 10)) } - if resp.StatusCode == 200 { + if resp.StatusCode == http.StatusOK { if limit > 0 && resp.ContentLength > limit { return ErrOverSize } return copyStream(resp.Body) } - if resp.StatusCode == 206 { + if resp.StatusCode == http.StatusPartialContent { contentLength = resp.ContentLength if limit > 0 && resp.ContentLength > limit { return ErrOverSize } - blockSize := func() int64 { - if contentLength > 1024*1024 { - return (contentLength / int64(threadCount)) - 10 - } - return contentLength - }() + blockSize := contentLength + if contentLength > 1024*1024 { + blockSize = (contentLength / int64(threadCount)) - 10 + } if blockSize == contentLength { return copyStream(resp.Body) } @@ -170,7 +167,7 @@ func DownloadFileMultiThreading(url, path string, limit int64, threadCount int, } // 下载分块 downloadBlock := func(block *BlockMetaData) error { - req, _ := http.NewRequest("GET", url, nil) + req, _ := http.NewRequest(http.MethodGet, url, nil) file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o666) if err != nil { return err @@ -291,7 +288,7 @@ func (g *gzipCloser) Close() error { // HTTPGetReadCloser 从 Http url 获取 io.ReadCloser func HTTPGetReadCloser(url string) (io.ReadCloser, error) { - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, err } diff --git a/main.go b/main.go index 0cd6079..fa75ded 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +// Package main package main import ( diff --git a/server/http.go b/server/http.go index 3fd7c37..a78102e 100644 --- a/server/http.go +++ b/server/http.go @@ -356,13 +356,13 @@ func (c *HTTPClient) onBotPushEvent(e *coolq.Event) { for i := uint64(0); i <= c.MaxRetries; i++ { // see https://stackoverflow.com/questions/31337891/net-http-http-contentlength-222-with-body-length-0 // we should create a new request for every single post trial - req, err = http.NewRequest("POST", c.addr, bytes.NewReader(e.JSONBytes())) + req, err = http.NewRequest(http.MethodGet, c.addr, bytes.NewReader(e.JSONBytes())) if err != nil { log.Warnf("上报 Event 数据到 %v 时创建请求失败: %v", c.addr, err) return } req.Header = header - res, err = c.client.Do(req) + res, err = c.client.Do(req) // nolint:bodyclose if err == nil { break } diff --git a/server/scf.go b/server/scf.go index 7d95e7f..517ff1d 100644 --- a/server/scf.go +++ b/server/scf.go @@ -65,7 +65,7 @@ func (l *lambdaResponseWriter) flush() error { Body: body, }) - r, _ := http.NewRequest("POST", cli.responseURL, buffer) + r, _ := http.NewRequest(http.MethodPost, cli.responseURL, buffer) do, err := cli.client.Do(r) if err != nil { return err