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

Linter improvements #117

Merged
merged 2 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
66 changes: 66 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
run:
concurrency: 4
timeout: 5m
issues-exit-code: 1
tests: true

skip-dirs:
- vendor

modules-download-mode: vendor

# list of build tags, all linters use it. Default is empty list
build-tags:
- integration

# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true

# print linter name in the end of issue text, default is true
print-linter-name: true

uniq-by-line: false

linters:
disable-all: true
enable:
- golint
- gofmt
- gosec
- unparam
- goconst
- prealloc
- stylecheck
- unconvert
- staticcheck
- gosec
- tparallel
- whitespace
- revive
- godot
- errorlint
- gocritic
- errname
- govet
- predeclared
- exhaustive
- tenv
- gofumpt
- forcetypeassert
- nilerr
- errcheck
# - promlinter this is a very nice linter, but it will most probably break things...
# - nestif
fast: false

issues:
exclude-rules:
# Exclude some staticcheck messages
- linters:
- staticcheck
text: "SA1019:"
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"

lint: fmtcheck
docker run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.28.1 golangci-lint run --enable golint,gofmt,unparam,goconst,prealloc,stylecheck,unconvert --exclude-use-default=false --deadline=5m --build-tags integration
$(DOCKER) run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.44.2 golangci-lint -v run

deeplint: fmtcheck
docker run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.28.1 golangci-lint run --exclude-use-default=false --enable-all -D dupl --build-tags integration
$(DOCKER) run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.44.2 golangci-lint run --exclude-use-default=false --enable-all -D dupl --build-tags integration

deps:
docker container inspect harvester-consul > /dev/null 2>&1 || docker run -d --rm -p 8500:8500 -p 8600:8600/udp --name=harvester-consul consul:1.4.3 agent -server -ui -node=server-1 -bootstrap-expect=1 -client=0.0.0.0 -http-port 8500 -log-level=err
Expand Down
11 changes: 8 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ type Field struct {
}

// newField constructor.
func newField(prefix string, fld reflect.StructField, val reflect.Value, chNotify chan<- ChangeNotification) *Field {
func newField(prefix string, fld reflect.StructField, val reflect.Value, chNotify chan<- ChangeNotification) (*Field, error) {
sf, ok := val.Addr().Interface().(CfgType)
if !ok {
return nil, errors.New("failed to type assert to CfgType")
}

f := &Field{
name: prefix + fld.Name,
tp: fld.Type.Name(),
version: 0,
structField: val.Addr().Interface().(CfgType),
structField: sf,
sources: make(map[Source]string),
chNotify: chNotify,
}
Expand All @@ -75,7 +80,7 @@ func newField(prefix string, fld reflect.StructField, val reflect.Value, chNotif
}
}

return f
return f, nil
}

// Name getter.
Expand Down
6 changes: 5 additions & 1 deletion config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ func (p *parser) getFields(prefix string, tp reflect.Type, val reflect.Value, ch
return nil, err
}
ff = append(ff, nested...)
case typeInvalid:
}
}
return ff, nil
}

func (p *parser) createField(prefix string, f reflect.StructField, val reflect.Value, chNotify chan<- ChangeNotification) (*Field, error) {
fld := newField(prefix, f, val, chNotify)
fld, err := newField(prefix, f, val, chNotify)
if err != nil {
return nil, err
}

value, ok := fld.Sources()[SourceConsul]
if ok {
Expand Down
1 change: 1 addition & 0 deletions harvester_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build integration
// +build integration

package harvester
Expand Down
1 change: 0 additions & 1 deletion harvester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ func TestWithConsulFolderPrefixMonitor(t *testing.T) {
assert.Equal(t, test.ExpectedKeyLocation, filepath.Join(builder.monitorConsulCfg.folderPrefix, "key1"))
})
}

}

func TestCreate_NoConsulOrRedis(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions log/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (l consul) Log(level hclog.Level, msg string, args ...interface{}) {
warnf(msg, args)
case hclog.Error:
errorf(msg, args)
case hclog.Off:
}
}

Expand Down
1 change: 1 addition & 0 deletions monitor/consul/watcher_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build integration
// +build integration

package consul
Expand Down
6 changes: 3 additions & 3 deletions monitor/redis/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package redis

import (
"context"
"crypto/md5"
"crypto/md5" //nolint:gosec
"encoding/hex"
"errors"
"time"
Expand Down Expand Up @@ -79,7 +79,7 @@ func (w *Watcher) getValues(ctx context.Context, ch chan<- []*change.Change) {
continue
}
if strCmd.Err() != nil {
if strCmd.Err() != redis.Nil {
if !errors.Is(strCmd.Err(), redis.Nil) {
log.Errorf("failed to get value for key %s: %s", key, strCmd.Err())
}
continue
Expand Down Expand Up @@ -115,6 +115,6 @@ func (w *Watcher) getValues(ctx context.Context, ch chan<- []*change.Change) {
}

func (w *Watcher) hash(value string) string {
hash := md5.Sum([]byte(value))
hash := md5.Sum([]byte(value)) // nolint:gosec
return hex.EncodeToString(hash[:])
}
1 change: 0 additions & 1 deletion monitor/redis/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func (c *clientStub) Get(_ context.Context, key string) *redis.StringCmd {
}

return redis.NewStringResult("", redis.Nil)

}

func (c *clientStub) rollInternalRedisState() {
Expand Down
2 changes: 1 addition & 1 deletion seed/consul/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Getter struct {
token string
}

// New constructor. Timeout is set to 60s when 0 is provided
// New constructor. Timeout is set to 60s when 0 is provided.
func New(addr, dc, token string, timeout time.Duration) (*Getter, error) {
if addr == "" {
return nil, errors.New("address is empty")
Expand Down
1 change: 1 addition & 0 deletions seed/consul/getter_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build integration
// +build integration

package consul
Expand Down
1 change: 1 addition & 0 deletions seed/redis/getter_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build integration
// +build integration

package redis
Expand Down
2 changes: 1 addition & 1 deletion seed/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (s *Seeder) Seed(cfg *config.Config) error {
// the parsing won't stop, and we make sure we try to parse every flag passed when running the command.
for _, arg := range os.Args[1:] {
if err := flagSet.Parse([]string{arg}); err != nil {
// Simply log errors that can happen, such as parsing unexpected flags. We want this to be silent
// Simply log errors that can happen, such as parsing unexpected flags. We want this to be silent,
Copy link
Contributor

Choose a reason for hiding this comment

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

Very naive comment, but would it be better to move the comment after the period (.) to the next line ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no guideline for that. Personally I am optimizing for vertical space and only go to the next line if the content does not fit the width. More can be displayed this way, l;less scrolling.

// and we won't want to stop the execution.
log.Errorf("could not parse flagSet: %v", err)
}
Expand Down