Skip to content

Commit

Permalink
refactor: re-enable linters, use presets
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Sep 6, 2024
1 parent abb85da commit ecea10c
Show file tree
Hide file tree
Showing 181 changed files with 536 additions and 412 deletions.
108 changes: 31 additions & 77 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,86 +1,40 @@
---
linters:
presets:
- bugs
- error
- import
- metalinter
- module
- unused

enable:
- testifylint

disable:
# preset error
- err113 # Very annoying to define static errors everywhere
- wrapcheck # Very annoying to wrap errors everywhere
# preset import
- depguard

linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/hetznercloud)

gosec:
excludes:
# As of golangci-lint v1.60.3, this rule is still enabled by default, which causes problems.
# We disable it manually for now until a new golangci-lint version is released which fixes this.
# See https://github.com/securego/gosec/issues/1185
- G115

exhaustive:
# Switch cases with a default case should be exhaustive.
default-signifies-exhaustive: true
gomodguard:
blocked:
modules:
- github.com/tj/assert:
recommendations:
- github.com/stretchr/testify/assert
reason: |
One assertion library is enough and we use testify/assert
everywhere.
- gotest.tools/assert:
recommendations:
- github.com/stretchr/testify/assert
reason: |
One assertion library is enough and we use testify/assert
everywhere.
- github.com/magiconair/properties:
recommendations:
- github.com/stretchr/testify/assert
reason: >
We do not currently need to parse properties files. At the same
time this module has an assert package which tends to get
imported by accident. It is therefore blocked.
- github.com/gogo/protobuf:
recommendations:
- google.golang.org/protobuf
reason: >
This is not the protobuf module that we want to import.
- github.com/golang/protobuf:
recommendations:
- google.golang.org/protobuf
reason: >
This is the legacy import location of Google's protobuf module.
Use the new one instead.
misspell:
locale: "US"
revive:
exported: true
import-shadowing: true
indent-error-flow: true

linters:
disable-all: true
# Some of these are temporarily disabled until we fix the numerous issues we have.
enable:
- bodyclose
# - dupl
- errcheck
- exhaustive
- gocritic
# - golint
- gci
- gomodguard
- gosec
- gosimple
- govet
- ineffassign
- misspell
- revive
- rowserrcheck
# - scopelint
# - staticcheck
- typecheck
- unparam
- unused
- whitespace

issues:
include:
- EXC0002 # disable excluding of issues about comments from golint
exclude-rules:
- path: _test\.go
linters:
- dupl
- gosec
- linters:
- gosec
text: "G204:"
gosmopolitan:
# We want to allow the use of time.Local()
allow-time-local: true
5 changes: 3 additions & 2 deletions internal/cmd/all/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/cli/internal/cmd/all"
"github.com/hetznercloud/cli/internal/testutil"
Expand Down Expand Up @@ -237,7 +238,7 @@ ID NAME FINGERPRINT AGE
`

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
Expand Down Expand Up @@ -286,7 +287,7 @@ func TestListAllPaidJSON(t *testing.T) {

jsonOut, errOut, err := fx.Run(cmd, []string{"--paid", "-o=json"})

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.JSONEq(t, allListResponseJSON, jsonOut)
}
2 changes: 1 addition & 1 deletion internal/cmd/base/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (uc *UpdateCmd) Run(s state.State, cmd *cobra.Command, args []string) error
})

if err := uc.Update(s, cmd, resource, flags); err != nil {
return fmt.Errorf("updating %s %s failed: %s", uc.ResourceNameSingular, idOrName, err)
return fmt.Errorf("updating %s %s failed: %w", uc.ResourceNameSingular, idOrName, err)
}

cmd.Printf("%s %v updated\n", uc.ResourceNameSingular, idOrName)
Expand Down
9 changes: 5 additions & 4 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/testutil"
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestCreateManaged(t *testing.T) {

expOut := "Certificate 123 created\n"

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestCreateManagedJSON(t *testing.T) {

expOut := "Certificate 123 created\n"

assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, expOut, out)

assert.JSONEq(t, managedCreateResponseJSON, jsonOut)
Expand Down Expand Up @@ -154,7 +155,7 @@ func TestCreateUploaded(t *testing.T) {

expOut := "Certificate 123 created\n"

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
Expand Down Expand Up @@ -192,7 +193,7 @@ func TestCreateUploadedJSON(t *testing.T) {

expOut := "Certificate 123 created\n"

assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, expOut, out)
assert.JSONEq(t, uploadedCreateResponseJSON, jsonOut)
}
5 changes: 3 additions & 2 deletions internal/cmd/certificate/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/testutil"
Expand Down Expand Up @@ -34,7 +35,7 @@ func TestDelete(t *testing.T) {

expOut := "certificate test deleted\n"

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestDeleteMultiple(t *testing.T) {

out, errOut, err := fx.Run(cmd, names)

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, "certificates test1, test2, test3 deleted\n", out)
}
3 changes: 2 additions & 1 deletion internal/cmd/certificate/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/cmd/util"
Expand Down Expand Up @@ -79,7 +80,7 @@ Used By:
util.Datetime(cert.NotValidBefore), humanize.Time(cert.NotValidBefore),
util.Datetime(cert.NotValidAfter), humanize.Time(cert.NotValidAfter))

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
5 changes: 3 additions & 2 deletions internal/cmd/certificate/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/testutil"
Expand Down Expand Up @@ -32,7 +33,7 @@ func TestLabelAdd(t *testing.T) {

expOut := "Label(s) key added to certificate 123\n"

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestLabelRemove(t *testing.T) {

expOut := "Label(s) key removed from certificate 123\n"

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
3 changes: 2 additions & 1 deletion internal/cmd/certificate/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/testutil"
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestList(t *testing.T) {
123 test managed example.com Wed Aug 20 12:00:00 UTC 2036 20m
`

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
3 changes: 2 additions & 1 deletion internal/cmd/certificate/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/testutil"
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestRetry(t *testing.T) {

out, errOut, err := fx.Run(cmd, []string{"123"})

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, "Retried issuance of certificate my-test-cert\n", out)
}
3 changes: 2 additions & 1 deletion internal/cmd/certificate/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/testutil"
Expand All @@ -30,7 +31,7 @@ func TestUpdateName(t *testing.T) {

expOut := "certificate 123 updated\n"

assert.NoError(t, err)
require.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, expOut, out)
}
3 changes: 2 additions & 1 deletion internal/cmd/config/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

configCmd "github.com/hetznercloud/cli/internal/cmd/config"
"github.com/hetznercloud/cli/internal/state/config"
Expand Down Expand Up @@ -234,7 +235,7 @@ active_context = "test_context"

out, errOut, err := fx.Run(cmd, tt.args)

assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tt.expErr, errOut)
assert.Equal(t, tt.expOut, out)
})
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/config/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

configCmd "github.com/hetznercloud/cli/internal/cmd/config"
"github.com/hetznercloud/cli/internal/state/config"
Expand Down Expand Up @@ -104,9 +105,9 @@ func TestGet(t *testing.T) {
out, errOut, err := fx.Run(cmd, append(tt.args, tt.key))

if tt.err == "" {
assert.NoError(t, err)
require.NoError(t, err)
} else {
assert.EqualError(t, err, tt.err)
require.EqualError(t, err, tt.err)
}
assert.Equal(t, tt.expErr, errOut)
assert.Equal(t, tt.expOut, out)
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/config/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

configCmd "github.com/hetznercloud/cli/internal/cmd/config"
"github.com/hetznercloud/cli/internal/state/config"
Expand Down Expand Up @@ -154,7 +155,7 @@ token super secret token
setTestValues(fx.Config)
out, errOut, err := fx.Run(cmd, tt.args)

assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tt.expErr, errOut)
assert.Equal(t, tt.expOut, out)
})
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/config/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

configCmd "github.com/hetznercloud/cli/internal/cmd/config"
"github.com/hetznercloud/cli/internal/state/config"
Expand Down Expand Up @@ -209,9 +210,9 @@ active_context = "test_context"
out, errOut, err := fx.Run(cmd, tt.args)

if tt.err == "" {
assert.NoError(t, err)
require.NoError(t, err)
} else {
assert.EqualError(t, err, tt.err)
require.EqualError(t, err, tt.err)
}
assert.Equal(t, tt.expErr, errOut)
assert.Equal(t, tt.expOut, out)
Expand Down
Loading

0 comments on commit ecea10c

Please sign in to comment.