Skip to content

Commit

Permalink
fix: ci lint error (go-kratos#1391)
Browse files Browse the repository at this point in the history
* fix lint check

* fix lll lint error

* fix build error

* fix gomnd

* fix shadow declaration

* add make test command

* update
  • Loading branch information
kagaya85 authored Aug 31, 2021
1 parent a1f35ec commit f7588a4
Show file tree
Hide file tree
Showing 71 changed files with 338 additions and 260 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ name: Go

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

jobs:
build:
name: build & test
runs-on: ubuntu-latest
services:
etcd:
Expand Down
13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ linters:
- goconst
- goimports
- gomnd
- gocyclo
- ineffassign
- lll
- prealloc
Expand Down Expand Up @@ -53,3 +54,15 @@ linters-settings:
check-shadowing: true
whitespace:
multi-func: true
lll:
line-length: 160
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks: argument,case,condition,return
goconst:
ignore-tests: true
gocyclo:
# recommend 10-20
min-complexity: 30
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ all:
.PHONY: uninstall
.PHONY: clean
.PHONY: fmt
.PHONY: test
.PHONY: lint

install: all
ifeq ($(user),root)
Expand Down Expand Up @@ -54,4 +56,14 @@ clean:
@echo "clean finished"

fmt:
@gofmt -s -w .
@gofmt -s -w .

test:
@go test ./...

# golangci-lint
LINTER := bin/golangci-lint
$(LINTER):
curl -L https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.42.0
lint: $(LINTER)
@eval '${LINTER} run --timeout=5m'
1 change: 1 addition & 0 deletions api/metadata/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
dpb "google.golang.org/protobuf/types/descriptorpb"
)

//nolint:lll
//go:generate protoc --proto_path=. --proto_path=../../third_party --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. --go-http_out=paths=source_relative:. metadata.proto

// Server is api meta server
Expand Down
17 changes: 9 additions & 8 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ type App struct {

// New create an application lifecycle manager.
func New(opts ...Option) *App {
options := options{
o := options{
ctx: context.Background(),
logger: log.NewHelper(log.DefaultLogger),
sigs: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
registrarTimeout: 10 * time.Second,
}
if id, err := uuid.NewUUID(); err == nil {
options.id = id.String()
o.id = id.String()
}
for _, o := range opts {
o(&options)
for _, opt := range opts {
opt(&o)
}
ctx, cancel := context.WithCancel(options.ctx)
ctx, cancel := context.WithCancel(o.ctx)
return &App{
ctx: ctx,
cancel: cancel,
opts: options,
opts: o,
}
}

Expand Down Expand Up @@ -99,7 +99,8 @@ func (a *App) Run() error {
}
wg.Wait()
if a.opts.registrar != nil {
ctx, cancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout)
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout)
defer cancel()
if err := a.opts.registrar.Register(ctx, instance); err != nil {
return err
Expand Down Expand Up @@ -143,7 +144,7 @@ func (a *App) Stop() error {
}

func (a *App) buildInstance() (*registry.ServiceInstance, error) {
var endpoints []string
endpoints := make([]string, 0) //nolint:gomnd
for _, e := range a.opts.endpoints {
endpoints = append(endpoints, e.String())
}
Expand Down
20 changes: 13 additions & 7 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestApp(t *testing.T) {
Server(hs, gs),
)
time.AfterFunc(time.Second, func() {
app.Stop()
_ = app.Stop()
})
if err := app.Run(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -83,8 +83,10 @@ func TestApp_Endpoint(t *testing.T) {
name string
endpoint []string
metadata map[string]string
}{id: "1", version: "v1", name: "kratos-v1", endpoint: []string{"https://go-kratos.dev", "localhost"},
metadata: map[string]string{}},
}{
id: "1", version: "v1", name: "kratos-v1", endpoint: []string{"https://go-kratos.dev", "localhost"},
metadata: map[string]string{},
},
},
{
id: "2",
Expand All @@ -98,8 +100,10 @@ func TestApp_Endpoint(t *testing.T) {
name string
endpoint []string
metadata map[string]string
}{id: "2", version: "v2", name: "kratos-v2", endpoint: []string{"test"},
metadata: map[string]string{"kratos": "https://github.com/go-kratos/kratos"}},
}{
id: "2", version: "v2", name: "kratos-v2", endpoint: []string{"test"},
metadata: map[string]string{"kratos": "https://github.com/go-kratos/kratos"},
},
},
{
id: "3",
Expand All @@ -113,8 +117,10 @@ func TestApp_Endpoint(t *testing.T) {
name string
endpoint []string
metadata map[string]string
}{id: "3", version: "v3", name: "kratos-v3", endpoint: []string{},
metadata: map[string]string{}},
}{
id: "3", version: "v3", name: "kratos-v3", endpoint: []string{},
metadata: map[string]string{},
},
},
}
for _, tt := range tests {
Expand Down
14 changes: 7 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ type config struct {

// New new a config with options.
func New(opts ...Option) Config {
options := options{
o := options{
logger: log.DefaultLogger,
decoder: defaultDecoder,
resolver: defaultResolver,
}
for _, o := range opts {
o(&options)
for _, opt := range opts {
opt(&o)
}
return &config{
opts: options,
reader: newReader(options),
log: log.NewHelper(options.logger),
opts: o,
reader: newReader(o),
log: log.NewHelper(o.logger),
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ func (c *config) Load() error {
if err != nil {
return err
}
if err := c.reader.Merge(kvs...); err != nil {
if err = c.reader.Merge(kvs...); err != nil {
c.log.Errorf("failed to merge config source: %v", err)
return err
}
Expand Down
26 changes: 13 additions & 13 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ const (

type testConfigStruct struct {
Server struct {
Http struct {
HTTP struct {
Addr string `json:"addr"`
Port int `json:"port"`
Timeout float64 `json:"timeout"`
EnableSSL bool `json:"enable_ssl"`
} `json:"http"`
GRpc struct {
GRPC struct {
Addr string `json:"addr"`
Port int `json:"port"`
Timeout float64 `json:"timeout"`
Expand All @@ -60,17 +60,17 @@ type testConfigStruct struct {
Endpoints []string `json:"endpoints"`
}

type testJsonSource struct {
type testJSONSource struct {
data string
sig chan struct{}
err chan struct{}
}

func newTestJsonSource(data string) *testJsonSource {
return &testJsonSource{data: data, sig: make(chan struct{}), err: make(chan struct{})}
func newTestJSONSource(data string) *testJSONSource {
return &testJSONSource{data: data, sig: make(chan struct{}), err: make(chan struct{})}
}

func (p *testJsonSource) Load() ([]*KeyValue, error) {
func (p *testJSONSource) Load() ([]*KeyValue, error) {
kv := &KeyValue{
Key: "json",
Value: []byte(p.data),
Expand All @@ -79,7 +79,7 @@ func (p *testJsonSource) Load() ([]*KeyValue, error) {
return []*KeyValue{kv}, nil
}

func (p *testJsonSource) Watch() (Watcher, error) {
func (p *testJSONSource) Watch() (Watcher, error) {
return newTestWatcher(p.sig, p.err), nil
}

Expand Down Expand Up @@ -120,15 +120,15 @@ func TestConfig(t *testing.T) {
)

c := New(
WithSource(newTestJsonSource(_testJSON)),
WithSource(newTestJSONSource(_testJSON)),
WithDecoder(defaultDecoder),
WithResolver(defaultResolver),
WithLogger(log.DefaultLogger),
)
err = c.Close()
assert.Nil(t, err)

jSource := newTestJsonSource(_testJSON)
jSource := newTestJSONSource(_testJSON)
opts := options{
sources: []Source{jSource},
decoder: defaultDecoder,
Expand Down Expand Up @@ -156,10 +156,10 @@ func TestConfig(t *testing.T) {
var testConf testConfigStruct
err = cf.Scan(&testConf)
assert.Nil(t, err)
assert.Equal(t, httpAddr, testConf.Server.Http.Addr)
assert.Equal(t, httpTimeout, testConf.Server.Http.Timeout)
assert.Equal(t, true, testConf.Server.Http.EnableSSL)
assert.Equal(t, grpcPort, testConf.Server.GRpc.Port)
assert.Equal(t, httpAddr, testConf.Server.HTTP.Addr)
assert.Equal(t, httpTimeout, testConf.Server.HTTP.Timeout)
assert.Equal(t, true, testConf.Server.HTTP.EnableSSL)
assert.Equal(t, grpcPort, testConf.Server.GRPC.Port)
assert.Equal(t, endpoint1, testConf.Endpoints[0])
assert.Equal(t, 2, len(testConf.Endpoints))
}
2 changes: 1 addition & 1 deletion config/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (e *env) load(envStrings []string) []*config.KeyValue {
var kv []*config.KeyValue
for _, envstr := range envStrings {
var k, v string
subs := strings.SplitN(envstr, "=", 2)
subs := strings.SplitN(envstr, "=", 2) //nolint:gomnd
k = subs[0]
if len(subs) > 1 {
v = subs[1]
Expand Down
8 changes: 4 additions & 4 deletions config/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func TestEnvWithPrefix(t *testing.T) {
data = []byte(_testJSON)
)
defer os.Remove(path)
if err := os.MkdirAll(path, 0700); err != nil {
if err := os.MkdirAll(path, 0o700); err != nil {
t.Error(err)
}
if err := ioutil.WriteFile(filename, data, 0666); err != nil {
if err := ioutil.WriteFile(filename, data, 0o666); err != nil {
t.Error(err)
}

Expand Down Expand Up @@ -148,10 +148,10 @@ func TestEnvWithoutPrefix(t *testing.T) {
data = []byte(_testJSON)
)
defer os.Remove(path)
if err := os.MkdirAll(path, 0700); err != nil {
if err := os.MkdirAll(path, 0o700); err != nil {
t.Error(err)
}
if err := ioutil.WriteFile(filename, data, 0666); err != nil {
if err := ioutil.WriteFile(filename, data, 0o666); err != nil {
t.Error(err)
}

Expand Down
10 changes: 5 additions & 5 deletions config/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ func TestFile(t *testing.T) {
data = []byte(_testJSON)
)
defer os.Remove(path)
if err := os.MkdirAll(path, 0700); err != nil {
if err := os.MkdirAll(path, 0o700); err != nil {
t.Error(err)
}
if err := ioutil.WriteFile(file, data, 0666); err != nil {
if err := ioutil.WriteFile(file, data, 0o666); err != nil {
t.Error(err)
}
testSource(t, file, data)
Expand Down Expand Up @@ -125,7 +125,7 @@ func testWatchFile(t *testing.T, path string) {
assert.Equal(t, string(kvs[0].Value), _testJSONUpdate)

newFilepath := filepath.Join(filepath.Dir(path), "test1.json")
if err := os.Rename(path, newFilepath); err != nil {
if err = os.Rename(path, newFilepath); err != nil {
t.Error(err)
}
kvs, err = watch.Next()
Expand Down Expand Up @@ -181,7 +181,7 @@ func testSource(t *testing.T, path string, data []byte) {
func TestConfig(t *testing.T) {
path := filepath.Join(t.TempDir(), "test_config.json")
defer os.Remove(path)
if err := ioutil.WriteFile(path, []byte(_testJSON), 0666); err != nil {
if err := ioutil.WriteFile(path, []byte(_testJSON), 0o666); err != nil {
t.Error(err)
}
c := config.New(config.WithSource(
Expand Down Expand Up @@ -293,7 +293,7 @@ func testScan(t *testing.T, c config.Config) {
func TestMergeDataRace(t *testing.T) {
path := filepath.Join(t.TempDir(), "test_config.json")
defer os.Remove(path)
if err := ioutil.WriteFile(path, []byte(_testJSON), 0666); err != nil {
if err := ioutil.WriteFile(path, []byte(_testJSON), 0o666); err != nil {
t.Error(err)
}
c := config.New(config.WithSource(
Expand Down
4 changes: 2 additions & 2 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func defaultDecoder(src *KeyValue, target map[string]interface{}) error {
// placeholder format in ${key:default}.
func defaultResolver(input map[string]interface{}) error {
mapper := func(name string) string {
args := strings.SplitN(strings.TrimSpace(name), ":", 2)
args := strings.SplitN(strings.TrimSpace(name), ":", 2) //nolint:gomnd
if v, has := readValue(input, args[0]); has {
s, _ := v.String()
return s
Expand Down Expand Up @@ -127,7 +127,7 @@ func expand(s string, mapping func(string) string) string {
r := regexp.MustCompile(`\${(.*?)}`)
re := r.FindAllStringSubmatch(s, -1)
for _, i := range re {
if len(i) == 2 {
if len(i) == 2 { //nolint:gomnd
s = strings.ReplaceAll(s, i[0], mapping(i[1]))
}
}
Expand Down
4 changes: 2 additions & 2 deletions config/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestDefaultDecoder(t *testing.T) {
Value: []byte("config"),
Format: "",
}
target := make(map[string]interface{}, 0)
target := make(map[string]interface{})
err := defaultDecoder(src, target)
assert.Nil(t, err)
assert.Equal(t, map[string]interface{}{
Expand All @@ -25,7 +25,7 @@ func TestDefaultDecoder(t *testing.T) {
Value: []byte("2233"),
Format: "",
}
target = make(map[string]interface{}, 0)
target = make(map[string]interface{})
err = defaultDecoder(src, target)
assert.Nil(t, err)
assert.Equal(t, map[string]interface{}{
Expand Down
Loading

0 comments on commit f7588a4

Please sign in to comment.