Skip to content

Commit

Permalink
add staticheck into ci
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue committed Sep 7, 2023
1 parent 0dce4b5 commit e1168c2
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 55 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ jobs:
go-version: '1.10.x'
- name: Format
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
working-directory: src/github.com/qiniu/go-sdk
- name: Run unit cases
run: |
set -e
GOPATH=$GITHUB_WORKSPACE go get github.com/qiniu/x
GOPATH=$GITHUB_WORKSPACE go get golang.org/x/sync/singleflight
GOPATH=$GITHUB_WORKSPACE go get github.com/qiniu/dyn
Expand Down Expand Up @@ -69,8 +71,16 @@ jobs:
go-version: ${{ matrix.go_version }}
- name: Format
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
- name: Golint
run: |
if [ "${{ matrix.go_version }}" = "1.20.x" ]; then
set -e
go install honnef.co/go/tools/cmd/staticcheck@latest
make staticcheck
fi
- name: Run unit cases
run: |
set -e
make unittest
env:
GO111MODULE: 'on'
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ test:

unittest:
go test -tags=unit -failfast -v -coverprofile=coverage.txt `go list ./... | egrep -v 'examples|sms'`

staticcheck:
staticcheck -go 1.10 `go list ./... | egrep -v 'examples|sms'`
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func parseError(e *ErrorInfo, r io.Reader) {
e.Err = string(body)
}

func ResponseError(resp *http.Response) (err error) {
func ResponseError(resp *http.Response) error {

e := &ErrorInfo{
Reqid: resp.Header.Get("X-Reqid"),
Expand All @@ -257,9 +257,9 @@ func ResponseError(resp *http.Response) (err error) {
if ok && strings.HasPrefix(ct[0], "application/json") {
parseError(e, resp.Body)
} else {
bs, rErr := ioutil.ReadAll(resp.Body)
if rErr != nil {
err = rErr
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
e.Err = strings.TrimRight(string(bs), "\n")
}
Expand Down
3 changes: 2 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package api

/*
包 github.com/qiniu/go-sdk 是七牛 Go 语言 SDK v7.x 版本。
Expand All @@ -7,4 +9,3 @@ Go SDK 中主要包含几个包:
auth 包提供鉴权相关方法,conf 包提供配置相关方法,cdn包提供CDN相关的功能,storage包提供存储相关的功能。
*/
package api
3 changes: 1 addition & 2 deletions err.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

// QINIU SDK error type

// QError QINIU SDK error type
// 可以根据Code判断是何种类型错误
type QError struct {
Code string
Expand Down
15 changes: 8 additions & 7 deletions internal/clientv2/interceptor_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package clientv2
import (
"crypto/tls"
"fmt"
clientV1 "github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/internal/log"
"net/http"
"net/http/httptrace"
"net/http/httputil"

clientV1 "github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/internal/log"
)

type DebugLevel int
Expand Down Expand Up @@ -147,10 +148,10 @@ func (interceptor *debugInterceptor) printRequestTrace(label string, req *http.R
log.Debug(label + fmt.Sprintf("PutIdleConn, err:%v \n", err))
},
GotFirstResponseByte: func() {
log.Debug(label + fmt.Sprint("GotFirstResponseByte \n"))
log.Debug(label + "GotFirstResponseByte \n")
},
Got100Continue: func() {
log.Debug(label + fmt.Sprint("Got100Continue \n"))
log.Debug(label + "Got100Continue \n")
},
DNSStart: func(info httptrace.DNSStartInfo) {
log.Debug(label + fmt.Sprintf("DNSStart, host:%s \n", info.Host))
Expand All @@ -165,7 +166,7 @@ func (interceptor *debugInterceptor) printRequestTrace(label string, req *http.R
log.Debug(label + fmt.Sprintf("ConnectDone, network:%s ip:%s err:%v \n", network, addr, err))
},
TLSHandshakeStart: func() {
log.Debug(label + fmt.Sprint("TLSHandshakeStart \n"))
log.Debug(label + "TLSHandshakeStart \n")
},
TLSHandshakeDone: func(state tls.ConnectionState, err error) {
log.Debug(label + fmt.Sprintf("TLSHandshakeDone, state:%+v err:%s \n", state, err))
Expand All @@ -175,10 +176,10 @@ func (interceptor *debugInterceptor) printRequestTrace(label string, req *http.R
// log.Debug(label + fmt.Sprintf("WroteHeaderField, key:%s value:%s \n", key, value))
//},
WroteHeaders: func() {
log.Debug(label + fmt.Sprint("WroteHeaders \n"))
log.Debug(label + "WroteHeaders \n")
},
Wait100Continue: func() {
log.Debug(label + fmt.Sprint("Wait100Continue \n"))
log.Debug(label + "Wait100Continue \n")
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
log.Debug(label + fmt.Sprintf("WroteRequest, err:%v \n", info.Err))
Expand Down
4 changes: 2 additions & 2 deletions linking/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ type Device struct {
// 0 不录制
// -1 永久
// -2 继承app配置
SegmentExpireDays int `json:"segmentExpireDays,has,omitempty"`
SegmentExpireDays int `json:"segmentExpireDays,omitempty"`

// -1 继承app配置
// 0 遵循设备端配置
// 1 强制持续上传
// 2 强制关闭上传
UploadMode int `json:"uploadMode,has,omitempty"`
UploadMode int `json:"uploadMode,omitempty"`

State int `json:"state,omitempty"`

Expand Down
2 changes: 1 addition & 1 deletion rtc/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func callReq(httpClient *http.Client, req *http.Request, mac *auth.Credentials,
info.Code = resp.StatusCode
reqid := getReqid(&resp.Header)
rebuildErr := func(msg string) error {
return fmt.Errorf("Code: %v, Reqid: %v, %v", info.Code, reqid, msg)
return fmt.Errorf("code: %v, reqid: %v, %v", info.Code, reqid, msg)
}

if resp.ContentLength > 2*1024*1024 {
Expand Down
4 changes: 2 additions & 2 deletions storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ func (m *BucketManager) Batch(operations []string) ([]BatchOpRet, error) {
return nil, errors.New("can't get one bucket from operations")
}

return m.BatchWithContext(nil, bucket, operations)
return m.BatchWithContext(context.Background(), bucket, operations)
}

// BatchWithContext 接口提供了资源管理的批量操作,支持 stat,copy,move,delete,chgm,chtype,deleteAfterDays几个接口
Expand Down Expand Up @@ -1053,7 +1053,7 @@ func MakePublicURLv2WithQuery(domain, key string, query url.Values) string {
}

// MakePublicURLv2WithQueryString 用来生成公开空间资源下载链接,并且该方法确保 key 将会被 escape,并在 URL 后直接追加查询参数
func makePublicURLv2WithQueryString(domain, key, query string) string {
func MakePublicURLv2WithQueryString(domain, key, query string) string {
return makePublicURLv2WithRawQuery(domain, key, urlEncodeQuery(query))
}

Expand Down
2 changes: 1 addition & 1 deletion storage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ func TestMakeURL(t *testing.T) {
t.Fatalf("TestMakeURL: %q\n", s)
}

s = makePublicURLv2WithQueryString("http://abc.com:123/", "123/def?@#|", "123/def?@#|")
s = MakePublicURLv2WithQueryString("http://abc.com:123/", "123/def?@#|", "123/def?@#|")
if s != "http://abc.com:123/123/def%3F@%23%7C?123/def%3F%40%23|" {
t.Fatalf("TestMakeURL: %q\n", s)
}
Expand Down
2 changes: 1 addition & 1 deletion storage/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ var (
ErrBucketNotExist = errors.New("bucket not exist")

// ErrNoSuchFile 文件已经存在
ErrNoSuchFile = errors.New("No such file or directory")
ErrNoSuchFile = errors.New("no such file or directory")
)
4 changes: 0 additions & 4 deletions storage/form_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,6 @@ func (r *crc32Reader) Read(p []byte) (int, error) {
return r.r.Read(p)
}

func (r crc32Reader) length() (length int64) {
return int64(len(r.nlDashBoundaryNl+r.header)) + r.crc32PadLen
}

func (p *FormUploader) UpHost(ak, bucket string) (upHost string, err error) {
return getUpHost(p.Cfg, 0, 0, ak, bucket)
}
Expand Down
11 changes: 6 additions & 5 deletions storage/pfop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package storage
import (
"context"
"fmt"
"net/http"

"github.com/qiniu/go-sdk/v7/auth"
"github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/conf"
"net/http"
)

// OperationManager 提供了数据处理相关的方法
Expand Down Expand Up @@ -66,16 +67,16 @@ type PrefopRet struct {
func (r *PrefopRet) String() string {
strData := fmt.Sprintf("Id: %s\r\nCode: %d\r\nDesc: %s\r\n", r.ID, r.Code, r.Desc)
if r.InputBucket != "" {
strData += fmt.Sprintln(fmt.Sprintf("InputBucket: %s", r.InputBucket))
strData += fmt.Sprintf("InputBucket: %s\n", r.InputBucket)
}
if r.InputKey != "" {
strData += fmt.Sprintln(fmt.Sprintf("InputKey: %s", r.InputKey))
strData += fmt.Sprintf("InputKey: %s\n", r.InputKey)
}
if r.Pipeline != "" {
strData += fmt.Sprintln(fmt.Sprintf("Pipeline: %s", r.Pipeline))
strData += fmt.Sprintf("Pipeline: %s\n", r.Pipeline)
}
if r.Reqid != "" {
strData += fmt.Sprintln(fmt.Sprintf("Reqid: %s", r.Reqid))
strData += fmt.Sprintf("Reqid: %s\n", r.Reqid)
}

strData = fmt.Sprintln(strData)
Expand Down
7 changes: 5 additions & 2 deletions storage/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package storage
import (
"context"
"fmt"
"strings"
"time"

"github.com/qiniu/go-sdk/v7/auth"
"github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
"strings"
"time"
)

// 存储所在的地区,例如华东,华南,华北
Expand Down Expand Up @@ -277,6 +278,8 @@ func GetRegionWithOptions(ak, bucket string, options UCApiOptions) (*Region, err
}

// 使用 v4
//
//lint:ignore U1000 Used by test case
func getRegionGroup(ak, bucket string) (*RegionGroup, error) {
return getRegionByV4(ak, bucket, DefaultUCApiOptions())
}
Expand Down
9 changes: 5 additions & 4 deletions storage/region_uc_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"golang.org/x/sync/singleflight"
"os"
"path/filepath"
"sync"
"time"

"github.com/qiniu/go-sdk/v7/internal/clientv2"
"golang.org/x/sync/singleflight"
)

// 此处废弃,但为了兼容老版本,单独放置一个文件
Expand Down Expand Up @@ -52,12 +53,12 @@ func (uc *UcQueryRet) setup() {

uc.Io = make(map[string]map[string][]string)
ioSrc := uc.IoInfo["src"].toMapWithoutInfo()
if ioSrc != nil && len(ioSrc) > 0 {
if len(ioSrc) > 0 {
uc.Io["src"] = ioSrc
}

ioOldSrc := uc.IoInfo["old_src"].toMapWithoutInfo()
if ioOldSrc != nil && len(ioOldSrc) > 0 {
if len(ioOldSrc) > 0 {
uc.Io["old_src"] = ioOldSrc
}
}
Expand Down
7 changes: 4 additions & 3 deletions storage/region_uc_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"golang.org/x/sync/singleflight"
"math"
"os"
"path/filepath"
"sync"
"time"

"github.com/qiniu/go-sdk/v7/internal/clientv2"
"golang.org/x/sync/singleflight"
)

type ucQueryV4Ret struct {
Expand Down Expand Up @@ -165,7 +166,7 @@ func getRegionByV4(ak, bucket string, options UCApiOptions) (*RegionGroup, error
}

ttl := math.MaxInt32
regions := make([]*Region, 0, 0)
regions := make([]*Region, 0)
for _, host := range ret.Hosts {
if ttl > host.TTL {
ttl = host.TTL
Expand Down
7 changes: 4 additions & 3 deletions storage/resume_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
"hash/crc32"
"io"
"os"
"path/filepath"
"sort"
"sync"

"github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
)

// ResumeUploader 表示一个分片上传的对象
Expand Down Expand Up @@ -467,7 +468,7 @@ func (impl *resumeUploaderImpl) save(ctx context.Context) {
return
}

err = impl.extra.Recorder.Set(impl.recorderKey, recoveredData)
_ = impl.extra.Recorder.Set(impl.recorderKey, recoveredData)
}

func (impl *resumeUploaderImpl) resumeUploaderAPIs() *resumeUploaderAPIs {
Expand Down
6 changes: 0 additions & 6 deletions storage/resume_uploader_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ type (
reader io.ReaderAt
}

// 代表一块分片的上传错误信息
chunkError struct {
chunk
err error
}

// 通用分片上传接口,同时适用于分片上传 v1 和 v2 接口
resumeUploaderBase interface {
// 开始上传前调用一次用于初始化,在 v1 中该接口不做任何事情,而在 v2 中该接口对应 initParts
Expand Down
5 changes: 3 additions & 2 deletions storage/uploader_base.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package storage

import (
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
"time"

"github.com/qiniu/go-sdk/v7/internal/hostprovider"
)

// retryMax: 为 0,使用默认值,每个域名只请求一次
Expand Down Expand Up @@ -41,7 +42,7 @@ func getUpHostProvider(config *Config, retryMax int, hostFreezeDuration time.Dur
}
}

hosts := make([]string, 0, 0)
hosts := make([]string, 0)
if config.UseCdnDomains && len(region.CdnUpHosts) > 0 {
hosts = append(hosts, region.CdnUpHosts...)
} else if len(region.SrcUpHosts) > 0 {
Expand Down
9 changes: 5 additions & 4 deletions storage/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package storage

import (
"context"
"strings"
"time"

api "github.com/qiniu/go-sdk/v7"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
"strings"
"time"
)

// ParsePutTime 提供了将PutTime转换为 time.Time 的功能
Expand Down Expand Up @@ -126,7 +127,7 @@ func removeRepeatStringItem(slc []string) []string {
}

func removeHostScheme(host string) string {
host = strings.TrimLeft(host, "http://")
host = strings.TrimLeft(host, "https://")
host = strings.TrimPrefix(host, "http://")
host = strings.TrimPrefix(host, "https://")
return host
}
Loading

0 comments on commit e1168c2

Please sign in to comment.