Skip to content

Commit

Permalink
refactor: update golangci lint config (#1073)
Browse files Browse the repository at this point in the history
- Use config presets
- Fix new discovered errors
- Auto fix imports
  • Loading branch information
jooola authored Jan 10, 2025
1 parent d5916b8 commit 51b496b
Show file tree
Hide file tree
Showing 93 changed files with 298 additions and 292 deletions.
66 changes: 38 additions & 28 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
---
linters-settings:
errcheck:
exclude-functions:
- (*github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.ResourceData).Set
misspell:
locale: US

linters:
disable-all: true
enable:
- gofmt
- bodyclose
- errcheck
- exportloopref
- gocritic
- goimports
- gosimple
- govet
- ineffassign
- misspell
- prealloc
- revive
- rowserrcheck
- staticcheck
- typecheck
- unparam
presets:
- bugs
- error
- import
- metalinter
- module
- unused
- unconvert
- whitespace

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)

exhaustive:
# Switch cases with a default case should be exhaustive.
default-signifies-exhaustive: true

issues:
exclude-rules:
- path: _test\.go
linters:
- exportloopref
- gosec
- errcheck
- path: testing\.go
text: unused-parameter
- linters:
- testifylint
text: bool-compare
- linters:
- testifylint
text: empty

# Can be removed once we migrated to the plugin framework.
- linters:
- errcheck
text: Error return value of `d.Set` is not checked
15 changes: 6 additions & 9 deletions hcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ import (
"log"
"time"

"github.com/hetznercloud/terraform-provider-hcloud/internal/firewall"
"github.com/hetznercloud/terraform-provider-hcloud/internal/placementgroup"
"github.com/hetznercloud/terraform-provider-hcloud/internal/primaryip"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"

"github.com/hetznercloud/terraform-provider-hcloud/internal/snapshot"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/firewall"
"github.com/hetznercloud/terraform-provider-hcloud/internal/floatingip"
"github.com/hetznercloud/terraform-provider-hcloud/internal/image"
"github.com/hetznercloud/terraform-provider-hcloud/internal/loadbalancer"
"github.com/hetznercloud/terraform-provider-hcloud/internal/network"
"github.com/hetznercloud/terraform-provider-hcloud/internal/placementgroup"
"github.com/hetznercloud/terraform-provider-hcloud/internal/primaryip"
"github.com/hetznercloud/terraform-provider-hcloud/internal/rdns"
"github.com/hetznercloud/terraform-provider-hcloud/internal/server"
"github.com/hetznercloud/terraform-provider-hcloud/internal/snapshot"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
"github.com/hetznercloud/terraform-provider-hcloud/internal/volume"
)

Expand Down
3 changes: 2 additions & 1 deletion hcloud/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package hcloud
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/firewall"
"github.com/hetznercloud/terraform-provider-hcloud/internal/floatingip"
Expand All @@ -15,7 +17,6 @@ import (
"github.com/hetznercloud/terraform-provider-hcloud/internal/server"
"github.com/hetznercloud/terraform-provider-hcloud/internal/snapshot"
"github.com/hetznercloud/terraform-provider-hcloud/internal/volume"
"github.com/stretchr/testify/assert"
)

func TestProvider(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions internal/certificate/certdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func EqualCert(cert1, cert2 string) (bool, error) {

parsed1, err := parseCertificates(cert1)
if err != nil {
return false, fmt.Errorf("%s: cert1: %v", op, err)
return false, fmt.Errorf("%s: cert1: %w", op, err)
}
parsed2, err := parseCertificates(cert2)
if err != nil {
return false, fmt.Errorf("%s: cert2: %v", op, err)
return false, fmt.Errorf("%s: cert2: %w", op, err)
}
if len(parsed1) != len(parsed2) {
return false, nil
Expand Down Expand Up @@ -67,7 +67,7 @@ func parseCertificates(cert string) ([]*x509.Certificate, error) {
}
c, err := x509.ParseCertificate(blk.Bytes)
if err != nil {
return nil, fmt.Errorf("%s: %v", op, err)
return nil, fmt.Errorf("%s: %w", op, err)
}

certs = append(certs, c)
Expand Down
19 changes: 9 additions & 10 deletions internal/certificate/certdiff_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ import (
"strings"
"testing"

"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
)

// TestParseCertificates_CertificateChain tries to parse a chain of PEM
// encoded certificates with interspersed and terminating data.
// See: https://github.com/hetznercloud/terraform-provider-hcloud/issues/359
func TestParseCertificates_CertificateChain(t *testing.T) {
pem1, _, err := testsupport.RandTLSCert("example.com")
assert.NoError(t, err)
require.NoError(t, err)
cert1, err := parseCertificates(pem1)
assert.NoError(t, err)
require.NoError(t, err)

pem2, _, err := testsupport.RandTLSCert("ca.example.com")
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

cert2, err := parseCertificates(pem2)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

// Not really a certificate chain, but enough for our testing purposes
chain := strings.Join([]string{pem1, pem2}, "\nIntermediate data\n") + "Terminating data"
actual, err := parseCertificates(chain)
assert.NoError(t, err)
require.NoError(t, err)

if !assert.Len(t, actual, 2) {
return
Expand Down
27 changes: 11 additions & 16 deletions internal/certificate/certdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"strings"
"testing"

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

"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
"github.com/stretchr/testify/assert"
)

func TestEqualCert_EqualCertificates(t *testing.T) {
Expand All @@ -30,17 +32,13 @@ func TestEqualCert_EqualCertificates(t *testing.T) {
certs := make([]string, len(tt.cns))
for i, cn := range tt.cns {
cert, _, err := testsupport.RandTLSCert(cn)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
certs[i] = cert
}

cert := strings.Join(certs, "\n")
res, err := certificate.EqualCert(cert, cert)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

assert.True(t, res, "Same certificates are expected to be equal")
})
Expand Down Expand Up @@ -79,27 +77,24 @@ func TestEqualCert_DifferentCertificates(t *testing.T) {
certs := make([]string, len(tt.cns))
for i, cn := range tt.cns {
cert, _, err := testsupport.RandTLSCert(cn)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

certs[i] = cert
}

certsAlt := make([]string, len(tt.cnsAlt))
for i, cn := range tt.cnsAlt {
cert, _, err := testsupport.RandTLSCert(cn)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

certsAlt[i] = cert
}

cert := strings.Join(certs, "\n")
certAlt := strings.Join(certsAlt, "\n")
res, err := certificate.EqualCert(cert, certAlt)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

assert.False(t, res, "Different certificates are expected to be unequal")
})
}
Expand Down
1 change: 1 addition & 0 deletions internal/certificate/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
Expand Down
5 changes: 2 additions & 3 deletions internal/certificate/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"fmt"
"testing"

"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/teste2e"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/teste2e"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testtemplate"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/certificate/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/timeutil"
Expand Down
5 changes: 2 additions & 3 deletions internal/certificate/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"os"
"testing"

"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/teste2e"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/teste2e"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testtemplate"
)
Expand Down
4 changes: 2 additions & 2 deletions internal/certificate/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"strconv"
"testing"

"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testtemplate"
)

Expand Down
1 change: 1 addition & 0 deletions internal/deprecation/deprecation_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
resourceschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hetznercloud/hcloud-go/hcloud"
)

Expand Down
23 changes: 12 additions & 11 deletions internal/deprecation/deprecation_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"testing"
"time"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/stretchr/testify/assert"

"github.com/hetznercloud/hcloud-go/hcloud"
)

func TestNewDeprecationModel(t *testing.T) {
Expand All @@ -15,12 +16,12 @@ func TestNewDeprecationModel(t *testing.T) {

{
data, diags := NewDeprecationModel(ctx, hcloud.ServerType{})
assert.Equal(t, diags.HasError(), false)
assert.Equal(t, data.IsDeprecated.ValueBool(), false)
assert.Equal(t, data.DeprecationAnnounced.IsNull(), false)
assert.Equal(t, data.DeprecationAnnounced.ValueString(), "")
assert.Equal(t, data.UnavailableAfter.IsNull(), false)
assert.Equal(t, data.UnavailableAfter.ValueString(), "")
assert.Equal(t, false, diags.HasError())
assert.Equal(t, false, data.IsDeprecated.ValueBool())
assert.Equal(t, false, data.DeprecationAnnounced.IsNull())
assert.Equal(t, "", data.DeprecationAnnounced.ValueString())
assert.Equal(t, false, data.UnavailableAfter.IsNull())
assert.Equal(t, "", data.UnavailableAfter.ValueString())
}

{
Expand All @@ -32,9 +33,9 @@ func TestNewDeprecationModel(t *testing.T) {
},
},
})
assert.Equal(t, diags.HasError(), false)
assert.Equal(t, data.IsDeprecated.ValueBool(), true)
assert.Equal(t, data.DeprecationAnnounced.ValueString(), "2024-09-06T12:00:00Z")
assert.Equal(t, data.UnavailableAfter.ValueString(), "2024-09-06T12:00:00Z")
assert.Equal(t, false, diags.HasError())
assert.Equal(t, true, data.IsDeprecated.ValueBool())
assert.Equal(t, "2024-09-06T12:00:00Z", data.DeprecationAnnounced.ValueString())
assert.Equal(t, "2024-09-06T12:00:00Z", data.UnavailableAfter.ValueString())
}
}
1 change: 1 addition & 0 deletions internal/deprecation/deprecation_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hetznercloud/hcloud-go/hcloud"
)

Expand Down
1 change: 1 addition & 0 deletions internal/firewall/attachment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
)
Expand Down
Loading

0 comments on commit 51b496b

Please sign in to comment.