Skip to content

Commit

Permalink
feat: new linting system.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored and traefiker committed Mar 4, 2019
1 parent fb61704 commit ebded2c
Show file tree
Hide file tree
Showing 24 changed files with 129 additions and 256 deletions.
82 changes: 52 additions & 30 deletions .golangci.toml
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
[run]
deadline = "5m"

deadline = "10m"
skip-files = [
"^old/.*",
"^acme/.*",
"^cluster/.*",
"^cmd/convert/.*",
"^h2c/.*",
"^integration/.*",

# "^cmd/traefik/.*",
# "^anonymize/.*",
# "^provider/.*",
# "^tracing/.*",
# "^safe/.*",
# "^h2c/.*",
# "^healthcheck/.*",
# "^middlewares/.*",
# "^server/.*",
]

[linters-settings]

[linters-settings.govet]
check-shadowing = true
check-shadowing = false

[linters-settings.golint]
min-confidence = 0.0

[linters-settings.gocyclo]
min-complexity = 22.0
min-complexity = 14.0

[linters-settings.maligned]
suggest-new = true
Expand All @@ -44,25 +28,63 @@
[linters]
enable-all = true
disable = [
"gocyclo", # FIXME must be fixed
"gosec",
"dupl",
"maligned",
"lll",
"gas",
"dupl",
"unparam",
"prealloc",
"scopelint",
"gochecknoinits",
"gochecknoglobals",
"unparam",
"scopelint",
"goimports",
]

[issues]
exclude-use-default = false
max-per-linter = 0
max-same = 0
max-same-issues = 0
exclude = [
"field `(foo|fuu)` is unused",
"(.+) is deprecated:",
"cyclomatic complexity (\\d+) of func `\\(\\*Builder\\)\\.buildConstructor` is high", #alt/server/middleware/middlewares.go
"`logger` can be `github.com/containous/traefik/vendor/github.com/stretchr/testify/assert.TestingT`", # alt/middlewares/recovery/recovery.go:
"`fn` can be `net/http.Handler`", # alt/server/alice/chain.go
"SA1019: http.CloseNotifier is deprecated: the CloseNotifier interface predates Go's context package. New code should use Request.Context instead.", # FIXME must be fixed
"Error return value of .((os\\.)?std(out|err)\\..*|.*Close|.*Flush|os\\.Remove(All)?|.*printf?|os\\.(Un)?Setenv). is not checked",
"should have a package comment, unless it's in another file for this package",
]
[[issues.exclude-rules]]
path = ".+_test.go"
linters = ["goconst"]
[[issues.exclude-rules]]
path = "provider/label/internal/.+_test.go"
text = "U1000: field `(foo|fuu)` is unused"
[[issues.exclude-rules]]
path = "middlewares/recovery/recovery.go"
text = "`logger` can be `github.com/containous/traefik/vendor/github.com/stretchr/testify/assert.TestingT`"
[[issues.exclude-rules]]
path = "integration/.+_test.go"
text = "Error return value of `cmd\\.Process\\.Kill` is not checked"
[[issues.exclude-rules]]
path = "integration/(consul_catalog_test|constraint_test).go"
text = "Error return value of `(s.deregisterService|s.deregisterAgentService)` is not checked"
[[issues.exclude-rules]]
path = "integration/grpc_test.go"
text = "Error return value of `closer` is not checked"
[[issues.exclude-rules]]
path = "provider/kubernetes/builder_(endpoint|service)_test.go"
text = "(U1000: func )?`(.+)` is unused"
[[issues.exclude-rules]]
path = "provider/docker/builder_test.go"
text = "(U1000: func )?`(.+)` is unused"
[[issues.exclude-rules]]
path = "cmd/configuration.go"
text = "string `traefik` has (\\d) occurrences, make it a constant"
[[issues.exclude-rules]]
path = "h2c/h2c.go"
text = "Error return value of `rw.Write` is not checked"
[[issues.exclude-rules]]
path = "server/service/bufferpool.go"
text = "SA6002: argument should be pointer-like to avoid allocations"
[[issues.exclude-rules]] # FIXME must be fixed
path = "acme/.+.go"
text = "(assignment copies lock value to domainsCerts|literal copies lock value from)"
[[issues.exclude-rules]] # FIXME must be fixed
path = "cmd/context.go"
text = "S1000: should use a simple channel send/receive instead of `select` with a single case"
42 changes: 0 additions & 42 deletions .gometalinter.json

This file was deleted.

10 changes: 0 additions & 10 deletions .pre-commit-config.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test-integration: build ## run the integration tests
TEST_HOST=1 ./script/make.sh test-integration

validate: build ## validate code, vendor and autogen
$(DOCKER_RUN_TRAEFIK) ./script/make.sh validate-gofmt validate-golint validate-misspell validate-vendor validate-autogen
$(DOCKER_RUN_TRAEFIK) ./script/make.sh generate validate-lint validate-misspell validate-vendor validate-autogen

build: dist
docker build $(DOCKER_BUILD_ARGS) -t "$(TRAEFIK_DEV_IMAGE)" -f build.Dockerfile .
Expand Down Expand Up @@ -114,7 +114,7 @@ generate-webui: build-webui
fi

lint:
script/validate-golint
script/validate-lint

fmt:
gofmt -s -l -w $(SRCS)
Expand Down
18 changes: 14 additions & 4 deletions acme/acme.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ func (a *ACME) AddRoutes(router *mux.Router) {
}
tokenValue := a.challengeHTTPProvider.getTokenValue(token, domain)
if len(tokenValue) > 0 {
rw.WriteHeader(http.StatusOK)
rw.Write(tokenValue)
_, err := rw.Write(tokenValue)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
return
}
}
Expand All @@ -131,7 +133,11 @@ func (a *ACME) CreateClusterConfig(leadership *cluster.Leadership, tlsConfig *tl

listener := func(object cluster.Object) error {
account := object.(*Account)
account.Init()
err := account.Init()
if err != nil {
return err
}

if !leadership.IsLeader() {
a.client, err = a.buildACMEClient(account)
if err != nil {
Expand Down Expand Up @@ -187,7 +193,11 @@ func (a *ACME) leadershipListener(elected bool) error {
}

account := object.(*Account)
account.Init()
err = account.Init()
if err != nil {
return err
}

// Reset Account values if caServer changed, thus registration URI can be updated
if account != nil && account.Registration != nil && !isAccountMatchingCaServer(account.Registration.URI, a.CAServer) {
log.Info("Account URI does not match the current CAServer. The account will be reset")
Expand Down
12 changes: 8 additions & 4 deletions acme/acme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/containous/traefik/tls/generate"
"github.com/containous/traefik/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDomainsSet(t *testing.T) {
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestDomainsSet(t *testing.T) {
t.Parallel()

domains := types.Domains{}
domains.Set(test.input)
_ = domains.Set(test.input)
assert.Exactly(t, test.expected, domains)
})
}
Expand Down Expand Up @@ -110,7 +111,7 @@ func TestDomainsSetAppend(t *testing.T) {
for _, test := range testCases {
t.Run(test.input, func(t *testing.T) {

domains.Set(test.input)
_ = domains.Set(test.input)
assert.Exactly(t, test.expected, domains)
})
}
Expand Down Expand Up @@ -237,7 +238,9 @@ func TestRemoveDuplicates(t *testing.T) {
},
},
}
domainsCertificates.Init()

err := domainsCertificates.Init()
require.NoError(t, err)

if len(domainsCertificates.Certs) != 2 {
t.Errorf("Expected domainsCertificates length %d %+v\nGot %+v", 2, domainsCertificates.Certs, len(domainsCertificates.Certs))
Expand Down Expand Up @@ -790,7 +793,8 @@ func TestRemoveEmptyCertificates(t *testing.T) {
t.Parallel()

a := &Account{DomainsCertificate: *test.dc}
a.Init()
err := a.Init()
require.NoError(t, err)

assert.Equal(t, len(test.expectedDc.Certs), len(a.DomainsCertificate.Certs))
sort.Sort(&a.DomainsCertificate)
Expand Down
4 changes: 1 addition & 3 deletions acme/challenge_http_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ func (c *challengeHTTPProvider) CleanUp(domain, token, keyAuth string) error {

account := object.(*Account)
if _, ok := account.HTTPChallenge[token]; ok {
if _, domainOk := account.HTTPChallenge[token][domain]; domainOk {
delete(account.HTTPChallenge[token], domain)
}
delete(account.HTTPChallenge[token], domain)
if len(account.HTTPChallenge[token]) == 0 {
delete(account.HTTPChallenge, token)
}
Expand Down
8 changes: 6 additions & 2 deletions acme/challenge_tls_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func (c *challengeTLSProvider) getCertificate(domain string) (cert *tls.Certific
return nil, false
}

account.Init()
err := account.Init()
if err != nil {
log.Errorf("Unable to init ACME Account: %v", err)
return nil, false
}

var result *tls.Certificate
operation := func() error {
Expand All @@ -58,7 +62,7 @@ func (c *challengeTLSProvider) getCertificate(domain string) (cert *tls.Certific
ebo := backoff.NewExponentialBackOff()
ebo.MaxElapsedTime = 60 * time.Second

err := backoff.RetryNotify(safe.OperationWithRecover(operation), ebo, notify)
err = backoff.RetryNotify(safe.OperationWithRecover(operation), ebo, notify)
if err != nil {
log.Errorf("Error getting cert: %v", err)
return nil, false
Expand Down
3 changes: 2 additions & 1 deletion acme/localStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func TestGet(t *testing.T) {
fileContent, err := ioutil.ReadFile(acmeFile)
assert.NoError(t, err)

tmpFile.Write(fileContent)
_, err = tmpFile.Write(fileContent)
assert.NoError(t, err)

localStore := NewLocalStore(tmpFile.Name())
account, err := localStore.Get()
Expand Down
4 changes: 2 additions & 2 deletions build.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ RUN apk --update upgrade \
&& apk --no-cache --no-progress add git mercurial bash gcc musl-dev curl tar \
&& rm -rf /var/cache/apk/*

RUN go get golang.org/x/lint/golint \
&& go get github.com/client9/misspell/cmd/misspell
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.15.0 \
&& go get github.com/client9/misspell/cmd/misspell

# Which docker version to test on
ARG DOCKER_VERSION=17.03.2
Expand Down
23 changes: 14 additions & 9 deletions cmd/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,34 @@ func main() {
log.SetOutput(os.Stdout)
log.SetLevel(logrus.DebugLevel)

oldconfig := &types.Configuration{}
toml.Decode(oldvalue, oldconfig)
oldConfig := &types.Configuration{}
_, err := toml.Decode(oldvalue, oldConfig)
if err != nil {
log.Fatal(err)
}

newconfig := config.Configuration{
newConfig := config.Configuration{
Routers: make(map[string]*config.Router),
Middlewares: make(map[string]*config.Middleware),
Services: make(map[string]*config.Service),
}

for frontendName, frontend := range oldconfig.Frontends {
newconfig.Routers[replaceFrontend(frontendName)] = convertFrontend(frontend)
for frontendName, frontend := range oldConfig.Frontends {
newConfig.Routers[replaceFrontend(frontendName)] = convertFrontend(frontend)
if frontend.PassHostHeader {
log.Warn("ignore PassHostHeader")
}
}

for backendName, backend := range oldconfig.Backends {
newconfig.Services[replaceBackend(backendName)] = convertBackend(backend)
for backendName, backend := range oldConfig.Backends {
newConfig.Services[replaceBackend(backendName)] = convertBackend(backend)
}

encoder := toml.NewEncoder(os.Stdout)
encoder.Encode(newconfig)
err = encoder.Encode(newConfig)
if err != nil {
log.Fatal(err)
}
}

func replaceBackend(name string) string {
Expand Down Expand Up @@ -139,7 +145,6 @@ func convertBackend(backend *types.Backend) *config.Service {
Headers: backend.HealthCheck.Headers,
}
}

}

return service
Expand Down
Loading

0 comments on commit ebded2c

Please sign in to comment.