Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve code golangci config #765

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- uses: actions/setup-go@v3
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.2.0
uses: golangci/golangci-lint-action@v3.3.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: latest
57 changes: 51 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
run:
issues-exit-code: 1 #Default
tests: false #Default
skip-dirs: # 设置要忽略的目录
- test
- .*~
- api/swagger/docs
skip-files: # 设置不需要检查的go源码文件,支持正则匹配
- ".*.my.go$"
- ".*.pb.go$"
- _test.go

linters:
# Disable everything by default so upgrades to not include new "default
Expand All @@ -26,17 +34,20 @@ linters:
- funlen
- gofmt
- goimports
- goconst
- gocritic
- gocyclo
- govet
- godot
- typecheck
- misspell
- revive
# - stylecheck
# - deadcode
# - errcheck
# - gocritic
# - ineffassign
# - gosimple
# - revive
# - staticcheck
# - structcheck
# - unused
Expand Down Expand Up @@ -67,7 +78,7 @@ linters-settings:
ignore-words:
- cancelled
goimports:
local-prefixes: github.com/polarismesh/polaris-go
local-prefixes: github.com/polarismesh/polaris
godot:
# Comments to be checked: `declarations`, `toplevel`, or `all`.
# Default: declarations
Expand All @@ -90,19 +101,53 @@ linters-settings:
- name: atomic
- name: line-length-limit
severity: error
arguments: [195]
arguments: [480]
- name: unhandled-error
arguments : ["fmt.Printf", "myFunction"]
severity: warning
disabled: false
arguments : ["fmt.Printf", "myFunction", "builder.WriteString","buf.WriteByte","buf.WriteRune","buf.WriteString","buf.WriteTo","buf.Write","fmt.Println","iteratorInstancesProc","buffer.Write","buffer.WriteString","h.Write"]
funlen:
# Checks the number of lines in a function.
# If lower than 0, disable the check.
# Default: 60
lines: 120
lines: 210
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里保留为120吧

# Checks the number of statements in a function.
# If lower than 0, disable the check.
# Default: 40
statements: 0
statements: -1
goconst:
# Minimal length of string constant.
# Default: 3
min-len: 2
# Minimum occurrences of constant string count to trigger issue.
# Default: 3
# For subsequent optimization, the value is reduced.
min-occurrences: 72
# Ignore test files.
# Default: false
ignore-tests: true
# Look for existing constants matching the values.
# Default: true
match-constant: false
# Search also for duplicated numbers.
# Default: false
numbers: true
# Minimum value, only works with goconst.numbers
# Default: 3
min: 5
# Maximum value, only works with goconst.numbers
# Default: 3
max: 20
# Ignore when constant is not used as function argument.
# Default: true
ignore-calls: false
gocritic:
disabled-checks:
- ifElseChain
- assignOp
- appendAssign
- singleCaseSwitch
gocyclo:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 60
2 changes: 1 addition & 1 deletion apiserver/httpserver/maintain_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (h *HTTPServer) GetServerConnStats(req *restful.Request, rsp *restful.Respo
ctx := initContext(req)
params := httpcommon.ParseQueryParams(req)

var amount int = 0
var amount int
if amountStr, ok := params["amount"]; ok {
if n, err := strconv.Atoi(amountStr); err == nil {
amount = n
Expand Down
2 changes: 1 addition & 1 deletion apiserver/xdsserverv3/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func dumpSnapShotJSON(snapshot cache.ResourceSnapshot) []byte {
func yamlEncode(any interface{}) []byte {
data, _ := json.Marshal(any)
o := make(map[string]interface{})
json.Unmarshal(data, &o)
_ = json.Unmarshal(data, &o)
data, _ = yaml.Marshal(o)
return data
}
Expand Down
13 changes: 6 additions & 7 deletions apiserver/xdsserverv3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoy_extensions_common_ratelimit_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/common/ratelimit/v3"
Expand Down Expand Up @@ -139,7 +138,7 @@ func (x *XDSServer) Initialize(ctx context.Context, option map[string]interface{
return err
}

x.startSynTask(ctx)
_ = x.startSynTask(ctx)

return nil
}
Expand Down Expand Up @@ -251,7 +250,7 @@ func parseNodeID(nodeID string) (namespace string, uuid string, hostip string) {
}

// ID id 的格式是 namespace/uuid~hostIp
func (PolarisNodeHash) ID(node *envoy_config_core_v3.Node) string {
func (PolarisNodeHash) ID(node *core.Node) string {
if node == nil {
return ""
}
Expand Down Expand Up @@ -781,9 +780,9 @@ func (x *XDSServer) pushRegistryInfoToXDSCache(registryInfo map[string][]*Servic
versionLocal := time.Now().Format(time.RFC3339) + "/" + strconv.FormatUint(x.versionNum.Inc(), 10)

for ns, services := range registryInfo {
x.makeSnapshot(ns, versionLocal, services)
x.makePermissiveSnapshot(ns, versionLocal, services)
x.makeStrictSnapshot(ns, versionLocal, services)
_ = x.makeSnapshot(ns, versionLocal, services)
_ = x.makePermissiveSnapshot(ns, versionLocal, services)
_ = x.makeStrictSnapshot(ns, versionLocal, services)
}
return nil
}
Expand Down Expand Up @@ -1006,7 +1005,7 @@ func (x *XDSServer) startSynTask(ctx context.Context) error {
}

if len(needPush) > 0 {
x.pushRegistryInfoToXDSCache(needPush)
_ = x.pushRegistryInfoToXDSCache(needPush)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cache/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func iteratorInstancesProc(data *sync.Map, iterProc InstanceIterProc) error {

type servicePortsBucket struct {
lock sync.RWMutex
//servicePorts service-id -> []port
// servicePorts service-id -> []port
servicePorts map[string]map[string]struct{}
}

Expand Down
2 changes: 1 addition & 1 deletion cache/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (nsCache *namespaceCache) realUpdate(storeRollbackSec time.Duration) error
return err
}
nsCache.firstUpdate = false
nsCache.setNamespaces(ret)
_ = nsCache.setNamespaces(ret)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion common/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func CompareRoutingV2(a, b *v2.ExtendRoutingConfig) bool {
return a.CreateTime.Before(b.CreateTime)
}

// ConvertRoutingV1ToV2 v1 版本的路由规则转为 v2 版本进行存储
// ConvertRoutingV1ToExtendV2 v1 版本的路由规则转为 v2 版本进行存储
func ConvertRoutingV1ToExtendV2(svcName, svcNamespace string, rule *model.RoutingConfig) ([]*v2.ExtendRoutingConfig, []*v2.ExtendRoutingConfig, error) {
inRet := make([]*v2.ExtendRoutingConfig, 0, 4)
outRet := make([]*v2.ExtendRoutingConfig, 0, 4)
Expand Down
6 changes: 4 additions & 2 deletions common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ var (
BuildDate string
)

const defaultVersion = "v0.1.0"

// Get 获取版本号
func Get() string {
if Version == "" {
return "v0.1.0"
return defaultVersion
}

return Version
Expand All @@ -36,7 +38,7 @@ func Get() string {
// GetRevision 获取完整版本号信息,包括时间戳的
func GetRevision() string {
if Version == "" || BuildDate == "" {
return "v0.1.0"
return defaultVersion
}

return Version + "." + BuildDate
Expand Down
2 changes: 1 addition & 1 deletion config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (s *Server) GetConfigFileRichInfo(ctx context.Context, namespace, group, na
return api.NewConfigFileResponse(api.ExecuteSuccess, configFileBaseInfo)
}

// QueryConfigFilesByGroup querying configuration files
func (s *Server) QueryConfigFilesByGroup(ctx context.Context, namespace, group string,
offset, limit uint32) *api.ConfigBatchQueryResponse {

Expand Down Expand Up @@ -252,7 +253,6 @@ func (s *Server) SearchConfigFile(ctx context.Context, namespace, group, name, t
}

requestID, _ := ctx.Value(utils.StringContext("request-id")).(string)

count, files, err := s.queryConfigFileByTags(ctx, namespace, group, name, offset, limit, tagKVs...)
if err != nil {
log.ConfigScope().Error("[Config][Service] query config file tags error.",
Expand Down
3 changes: 1 addition & 2 deletions config/resource_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ func (s *serverAuthability) After(ctx context.Context, resourceType model.Resour
// onConfigGroupResource
func (s *serverAuthability) onConfigGroupResource(ctx context.Context, res *ResourceEvent) error {
authCtx := ctx.Value(utils.ContextAuthContextKey).(*model.AcquireContext)
ownerId := utils.ParseOwnerID(ctx)

authCtx.SetAttachment(model.ResourceAttachmentKey, map[api.ResourceType][]model.ResourceEntry{
api.ResourceType_ConfigGroups: {
{
ID: strconv.FormatUint(res.ConfigGroup.Id.GetValue(), 10),
Owner: ownerId,
Owner: utils.ParseOwnerID(ctx),
},
},
})
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ require (
go.uber.org/atomic v1.10.0
go.uber.org/automaxprocs v1.4.0
go.uber.org/zap v1.23.0
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/text v0.3.8
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11
golang.org/x/crypto v0.1.0
golang.org/x/net v0.1.0
golang.org/x/sync v0.1.0
golang.org/x/text v0.4.0
golang.org/x/time v0.1.1-0.20221020023724-80b9fac54d29
google.golang.org/grpc v1.50.1
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -74,7 +74,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/goleak v1.1.12 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect
golang.org/x/sys v0.1.0 // indirect
google.golang.org/genproto v0.0.0-20221014213838-99cd37c6964a // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading