Skip to content

Commit

Permalink
chore: fix gocritic linter (scaleway#1292)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone committed Jun 17, 2022
1 parent 35b42c5 commit f7f0939
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 46 deletions.
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ linters:
- forbidigo # Forbids identifiers [fast: true, auto-fix: false]
- gci # Gci controls golang package import order and makes it always deterministic. [fast: true, auto-fix: false]
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
- gocritic # Provides diagnostics that check for bugs, performance and style issues. [fast: false, auto-fix: false]
- gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false]
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
- gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true]
Expand Down Expand Up @@ -66,12 +67,10 @@ linters:
- dupl # Tool for code clone detection [fast: true, auto-fix: false]
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false]
- exhaustive # check exhaustiveness of enum switch statements [fast: false, auto-fix: false]
- forcetypeassert # finds forced type assertions [fast: true, auto-fix: false]
- funlen # Tool for detection of long functions [fast: true, auto-fix: false]
- gochecknoglobals # check that no global variables exist [fast: true, auto-fix: false]
- gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false]
- gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false]
- gocritic # Provides diagnostics that check for bugs, performance and style issues. [fast: false, auto-fix: false]
- godot # Check if comments end in a period [fast: true, auto-fix: true]
- godox # Tool for detection of FIXME, TODO and other comment keywords [fast: true, auto-fix: false]
- goerr113 # Golang linter to check the errors handling expressions [fast: false, auto-fix: false]
Expand Down
23 changes: 11 additions & 12 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scaleway/scaleway-sdk-go/namegenerator"
"github.com/scaleway/scaleway-sdk-go/scw"
"golang.org/x/xerrors"
)

// DefaultWaitRetryInterval is used to set the retry interval to 0 during acceptance tests
Expand All @@ -42,7 +41,7 @@ func newRegionalID(region scw.Region, id string) RegionalID {

func expandRegionalID(id interface{}) RegionalID {
regionalID := RegionalID{}
tab := strings.SplitN(id.(string), "/", -1)
tab := strings.Split(id.(string), "/")
if len(tab) != 2 {
regionalID.ID = id.(string)
} else {
Expand Down Expand Up @@ -73,7 +72,7 @@ func newZonedID(zone scw.Zone, id string) ZonedID {

func expandZonedID(id interface{}) ZonedID {
zonedID := ZonedID{}
tab := strings.SplitN(id.(string), "/", -1)
tab := strings.Split(id.(string), "/")
if len(tab) != 2 {
zonedID.ID = id.(string)
} else {
Expand All @@ -86,8 +85,8 @@ func expandZonedID(id interface{}) ZonedID {
}

// parseLocalizedID parses a localizedID and extracts the resource locality and id.
func parseLocalizedID(localizedID string) (locality string, ID string, err error) {
tab := strings.SplitN(localizedID, "/", -1)
func parseLocalizedID(localizedID string) (locality string, id string, err error) {
tab := strings.Split(localizedID, "/")
if len(tab) != 2 {
return "", localizedID, fmt.Errorf("cant parse localized id: %s", localizedID)
}
Expand All @@ -96,7 +95,7 @@ func parseLocalizedID(localizedID string) (locality string, ID string, err error

// parseLocalizedNestedID parses a localizedNestedID and extracts the resource locality, the inner and outer id.
func parseLocalizedNestedID(localizedID string) (locality string, innerID, outerID string, err error) {
tab := strings.SplitN(localizedID, "/", -1)
tab := strings.Split(localizedID, "/")
if len(tab) != 3 {
return "", "", localizedID, fmt.Errorf("cant parse localized id: %s", localizedID)
}
Expand Down Expand Up @@ -217,7 +216,7 @@ func isHTTPCodeError(err error, statusCode int) bool {
}

responseError := &scw.ResponseError{}
if xerrors.As(err, &responseError) && responseError.StatusCode == statusCode {
if errors.As(err, &responseError) && responseError.StatusCode == statusCode {
return true
}
return false
Expand All @@ -226,25 +225,25 @@ func isHTTPCodeError(err error, statusCode int) bool {
// is404Error returns true if err is an HTTP 404 error
func is404Error(err error) bool {
notFoundError := &scw.ResourceNotFoundError{}
return isHTTPCodeError(err, http.StatusNotFound) || xerrors.As(err, &notFoundError)
return isHTTPCodeError(err, http.StatusNotFound) || errors.As(err, &notFoundError)
}

func is412Error(err error) bool {
preConditionFailedError := &scw.PreconditionFailedError{}
return isHTTPCodeError(err, http.StatusPreconditionFailed) || xerrors.As(err, &preConditionFailedError)
return isHTTPCodeError(err, http.StatusPreconditionFailed) || errors.As(err, &preConditionFailedError)
}

// is403Error returns true if err is an HTTP 403 error
func is403Error(err error) bool {
permissionsDeniedError := &scw.PermissionsDeniedError{}
return isHTTPCodeError(err, http.StatusForbidden) || xerrors.As(err, &permissionsDeniedError)
return isHTTPCodeError(err, http.StatusForbidden) || errors.As(err, &permissionsDeniedError)
}

// is409Error return true is err is an HTTP 409 error
func is409Error(err error) bool {
// check transient error
transientStateError := &scw.TransientStateError{}
return isHTTPCodeError(err, http.StatusConflict) || xerrors.As(err, &transientStateError)
return isHTTPCodeError(err, http.StatusConflict) || errors.As(err, &transientStateError)
}

// organizationIDSchema returns a standard schema for a organization_id
Expand Down Expand Up @@ -558,7 +557,7 @@ func diffSuppressFuncIgnoreCase(k, oldValue, newValue string, d *schema.Resource
}

func diffSuppressFuncIgnoreCaseAndHyphen(k, oldValue, newValue string, d *schema.ResourceData) bool {
return strings.Replace(strings.ToLower(oldValue), "-", "_", -1) == strings.Replace(strings.ToLower(newValue), "-", "_", -1)
return strings.ReplaceAll(strings.ToLower(oldValue), "-", "_") == strings.ReplaceAll(strings.ToLower(newValue), "-", "_")
}

// diffSuppressFuncLocality is a SuppressDiffFunc to remove the locality from an ID when checking diff.
Expand Down
4 changes: 2 additions & 2 deletions scaleway/helpers_apple_silicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func asAPIWithZoneAndID(m interface{}, id string) (*applesilicon.API, scw.Zone,
return asAPI, zone, ID, nil
}

func waitForAppleSiliconServer(ctx context.Context, api *applesilicon.API, zone scw.Zone, ID string, timeout time.Duration) (*applesilicon.Server, error) {
func waitForAppleSiliconServer(ctx context.Context, api *applesilicon.API, zone scw.Zone, serverID string, timeout time.Duration) (*applesilicon.Server, error) {
retryInterval := defaultAppleSiliconServerRetryInterval
if DefaultWaitRetryInterval != nil {
retryInterval = *DefaultWaitRetryInterval
}

server, err := api.WaitForServer(&applesilicon.WaitForServerRequest{
ServerID: ID,
ServerID: serverID,
Zone: zone,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
Expand Down
8 changes: 4 additions & 4 deletions scaleway/helpers_baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,31 @@ func flattenBaremetalIPs(ips []*baremetal.IP) interface{} {
return flattendIPs
}

func waitForBaremetalServer(ctx context.Context, api *baremetal.API, zone scw.Zone, ID string, timeout time.Duration) (*baremetal.Server, error) {
func waitForBaremetalServer(ctx context.Context, api *baremetal.API, zone scw.Zone, serverID string, timeout time.Duration) (*baremetal.Server, error) {
retryInterval := baremetalRetryInterval
if DefaultWaitRetryInterval != nil {
retryInterval = *DefaultWaitRetryInterval
}

server, err := api.WaitForServer(&baremetal.WaitForServerRequest{
Zone: zone,
ServerID: ID,
ServerID: serverID,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))

return server, err
}

func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone scw.Zone, ID string, timeout time.Duration) (*baremetal.Server, error) {
func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone scw.Zone, serverID string, timeout time.Duration) (*baremetal.Server, error) {
retryInterval := baremetalRetryInterval
if DefaultWaitRetryInterval != nil {
retryInterval = *DefaultWaitRetryInterval
}

server, err := api.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
Zone: zone,
ServerID: ID,
ServerID: serverID,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
Expand Down
22 changes: 11 additions & 11 deletions scaleway/helpers_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,18 @@ func expandPrivateNetworks(data interface{}, lbID string) ([]*lbSDK.ZonedAPIAtta
return res, nil
}

func isPrivateNetworkEqual(A, B interface{}) bool {
func isPrivateNetworkEqual(a, b interface{}) bool {
// Find out the diff Private Network or not
if _, ok := A.(*lbSDK.PrivateNetwork); ok {
if _, ok := B.(*lbSDK.PrivateNetwork); ok {
if A.(*lbSDK.PrivateNetwork).PrivateNetworkID == B.(*lbSDK.PrivateNetwork).PrivateNetworkID {
if _, ok := a.(*lbSDK.PrivateNetwork); ok {
if _, ok := b.(*lbSDK.PrivateNetwork); ok {
if a.(*lbSDK.PrivateNetwork).PrivateNetworkID == b.(*lbSDK.PrivateNetwork).PrivateNetworkID {
// if both has dhcp config should not update
if A.(*lbSDK.PrivateNetwork).DHCPConfig != nil && B.(*lbSDK.PrivateNetwork).DHCPConfig != nil {
if a.(*lbSDK.PrivateNetwork).DHCPConfig != nil && b.(*lbSDK.PrivateNetwork).DHCPConfig != nil {
return true
}
// check static config
aConfig := A.(*lbSDK.PrivateNetwork).StaticConfig
bConfig := B.(*lbSDK.PrivateNetwork).StaticConfig
aConfig := a.(*lbSDK.PrivateNetwork).StaticConfig
bConfig := b.(*lbSDK.PrivateNetwork).StaticConfig
if aConfig != nil && bConfig != nil {
// check if static config is different
return reflect.DeepEqual(aConfig.IPAddress, bConfig.IPAddress)
Expand Down Expand Up @@ -437,14 +437,14 @@ func expandLbPrivateNetworkDHCPConfig(raw interface{}) *lbSDK.PrivateNetworkDHCP
return &lbSDK.PrivateNetworkDHCPConfig{}
}

func waitForLB(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, LbID string, timeout time.Duration) (*lbSDK.LB, error) {
func waitForLB(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) (*lbSDK.LB, error) {
retryInterval := defaultWaitLBRetryInterval
if DefaultWaitRetryInterval != nil {
retryInterval = *DefaultWaitRetryInterval
}

loadBalancer, err := lbAPI.WaitForLb(&lbSDK.ZonedAPIWaitForLBRequest{
LBID: LbID,
LBID: lbID,
Zone: zone,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
Expand All @@ -469,14 +469,14 @@ func waitForLbInstances(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zon
return loadBalancer, err
}

func waitForLBPN(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, LbID string, timeout time.Duration) ([]*lbSDK.PrivateNetwork, error) {
func waitForLBPN(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) ([]*lbSDK.PrivateNetwork, error) {
retryInterval := defaultWaitLBRetryInterval
if DefaultWaitRetryInterval != nil {
retryInterval = *DefaultWaitRetryInterval
}

privateNetworks, err := lbAPI.WaitForLBPN(&lbSDK.ZonedAPIWaitForLBPNRequest{
LBID: LbID,
LBID: lbID,
Zone: zone,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
Expand Down
10 changes: 5 additions & 5 deletions scaleway/helpers_rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ func newEndPointPrivateNetworkDetails(id, ip, locality string) (*rdb.EndpointPri
}, nil
}

func isEndPointEqual(A, B interface{}) bool {
func isEndPointEqual(a, b interface{}) bool {
// Find out the diff Private Network or not
if _, ok := A.(*rdb.EndpointPrivateNetworkDetails); ok {
if _, ok := B.(*rdb.EndpointPrivateNetworkDetails); ok {
detailsA := A.(*rdb.EndpointPrivateNetworkDetails)
detailsB := B.(*rdb.EndpointPrivateNetworkDetails)
if _, ok := a.(*rdb.EndpointPrivateNetworkDetails); ok {
if _, ok := b.(*rdb.EndpointPrivateNetworkDetails); ok {
detailsA := a.(*rdb.EndpointPrivateNetworkDetails)
detailsB := b.(*rdb.EndpointPrivateNetworkDetails)
return reflect.DeepEqual(detailsA, detailsB)
}
}
Expand Down
2 changes: 1 addition & 1 deletion scaleway/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func getTestFilePath(t *testing.T, suffix string) string {
specialChars := regexp.MustCompile(`[\\?%*:|"<>. ]`)

// Replace nested tests separators.
fileName := strings.Replace(t.Name(), "/", "-", -1)
fileName := strings.ReplaceAll(t.Name(), "/", "-")

fileName = strcase.ToBashArg(fileName)

Expand Down
2 changes: 1 addition & 1 deletion scaleway/resource_domain_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func resourceScalewayDomainRecordRead(ctx context.Context, d *schema.ResourceDat
currentData := d.Get("data")
// check if this is an inline import. Like: "terraform import scaleway_domain_record.www subdomain.domain.tld/11111111-1111-1111-1111-111111111111"
if strings.Contains(d.Id(), "/") {
tab := strings.SplitN(d.Id(), "/", -1)
tab := strings.Split(d.Id(), "/")
if len(tab) != 2 {
return diag.FromErr(fmt.Errorf("cant parse record id: %s", d.Id()))
}
Expand Down
4 changes: 2 additions & 2 deletions scaleway/resource_instance_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ func resourceScalewayInstanceServerRead(ctx context.Context, d *schema.ResourceD
// if key != "cloud-init" {
userData[key] = string(userDataValue)
// } else {
//_ = d.Set("cloud_init", string(userDataValue))
//}
// _ = d.Set("cloud_init", string(userDataValue))
// }
}
if len(userData) > 0 {
_ = d.Set("user_data", userData)
Expand Down
11 changes: 5 additions & 6 deletions scaleway/resource_lb_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ func resourceScalewayLbFrontendRead(ctx context.Context, d *schema.ResourceData,
return nil
}

func flattenLBACLs(ACLs []*lbSDK.ACL) interface{} {
sort.Slice(ACLs, func(i, j int) bool {
return ACLs[i].Index < ACLs[j].Index
func flattenLBACLs(acls []*lbSDK.ACL) interface{} {
sort.Slice(acls, func(i, j int) bool {
return acls[i].Index < acls[j].Index
})
rawACLs := make([]interface{}, 0, len(ACLs))
for _, apiACL := range ACLs {
rawACLs := make([]interface{}, 0, len(acls))
for _, apiACL := range acls {
rawACLs = append(rawACLs, flattenLbACL(apiACL))
}
return rawACLs
Expand Down Expand Up @@ -403,7 +403,6 @@ func resourceScalewayLbFrontendUpdate(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

// update acl
diagnostics := resourceScalewayLbFrontendUpdateACL(ctx, d, lbAPI, zone, ID)
if diagnostics != nil {
return diagnostics
Expand Down

0 comments on commit f7f0939

Please sign in to comment.