Skip to content

Commit 5cfc4e7

Browse files
authored
Add more linters (#107)
1 parent 8c1710e commit 5cfc4e7

File tree

15 files changed

+67
-1071
lines changed

15 files changed

+67
-1071
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ jobs:
2828
- name: Checkout Repository
2929
uses: actions/checkout@v2
3030
- name: Lint Code
31-
uses: golangci/golangci-lint-action@v2.5.2
31+
uses: golangci/golangci-lint-action@v2
3232
with:
3333
args: --timeout ${{ env.GOLANGCI_TIMEOUT }}

.golangci.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,31 @@ linters-settings:
44

55
linters:
66
enable:
7+
- asciicheck
8+
- deadcode
9+
- errcheck
10+
- errorlint
11+
- gofmt
12+
- gofumpt
713
- goimports
14+
- gosec
815
- gosimple
916
- govet
17+
- ineffassign
18+
- makezero
1019
- misspell
11-
- gofmt
12-
- unparam
13-
- unconvert
20+
- nilerr
21+
- noctx
22+
- predeclared
23+
- staticcheck
1424
- structcheck
15-
- errcheck
25+
- typecheck
26+
- unconvert
27+
- unparam
28+
- unused
29+
- varcheck
30+
- wastedassign
1631
disable-all: true
17-
32+
issues:
33+
max-issues-per-linter: 0
34+
max-same-issues: 0

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test:
1010

1111
.PHONY: lint
1212
lint:
13-
go run github.com/golangci/golangci-lint/cmd/golangci-lint run
13+
docker run --pull always --rm -v $(shell pwd):/nginx-asg-sync -w /nginx-asg-sync -v $(shell go env GOCACHE):/cache/go -e GOCACHE=/cache/go -e GOLANGCI_LINT_CACHE=/cache/go -v $(shell go env GOPATH)/pkg:/go/pkg golangci/golangci-lint:latest golangci-lint --color always run
1414

1515
.PHONY: build
1616
build:

cmd/sync/aws.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
2828
awsClient := &AWSClient{}
2929
cfg, err := parseAWSConfig(data)
3030
if err != nil {
31-
return nil, fmt.Errorf("error validating config: %v", err)
31+
return nil, fmt.Errorf("error validating config: %w", err)
3232
}
3333

3434
if cfg.Region == "self" {
@@ -47,7 +47,7 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
4747

4848
region, err := metaClient.Region()
4949
if err != nil {
50-
return nil, fmt.Errorf("unable to retrieve region from ec2metadata: %v", err)
50+
return nil, fmt.Errorf("unable to retrieve region from ec2metadata: %w", err)
5151
}
5252
cfg.Region = region
5353
}
@@ -56,7 +56,7 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
5656

5757
err = awsClient.configure()
5858
if err != nil {
59-
return nil, fmt.Errorf("error configuring AWS Client: %v", err)
59+
return nil, fmt.Errorf("error configuring AWS Client: %w", err)
6060
}
6161

6262
return awsClient, nil
@@ -129,7 +129,7 @@ func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
129129

130130
response, err := client.svcEC2.DescribeInstances(params)
131131
if err != nil {
132-
return false, fmt.Errorf("couldn't check if an AutoScaling group exists: %v", err)
132+
return false, fmt.Errorf("couldn't check if an AutoScaling group exists: %w", err)
133133
}
134134

135135
return len(response.Reservations) > 0, nil

cmd/sync/aws_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ func TestValidateAWSConfigValid(t *testing.T) {
8989

9090
err := validateAWSConfig(cfg)
9191
if err != nil {
92-
t.Errorf("validateAWSConfig() failed for the valid config: %v", err)
92+
t.Errorf("validateAWSConfig() failed for the valid config: %w", err)
9393
}
9494
}
9595

9696
func TestGetUpstreamsAWS(t *testing.T) {
9797
cfg := getValidAWSConfig()
98-
var upstreams = []awsUpstream{
98+
upstreams := []awsUpstream{
9999
{
100100
Name: "127.0.0.1",
101101
Port: 80,
@@ -184,5 +184,4 @@ func TestPrepareBatches(t *testing.T) {
184184
t.Errorf("prepareBatches() returned a batch with len > %v", maxItems)
185185
}
186186
}
187-
188187
}

cmd/sync/azure.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func NewAzureClient(data []byte) (*AzureClient, error) {
2222
azureClient := &AzureClient{}
2323
cfg, err := parseAzureConfig(data)
2424
if err != nil {
25-
return nil, fmt.Errorf("error validating config: %v", err)
25+
return nil, fmt.Errorf("error validating config: %w", err)
2626
}
2727

2828
azureClient.config = cfg
2929

3030
err = azureClient.configure()
3131
if err != nil {
32-
return nil, fmt.Errorf("error configuring Azure Client: %v", err)
32+
return nil, fmt.Errorf("error configuring Azure Client: %w", err)
3333
}
3434

3535
return azureClient, nil
@@ -106,15 +106,14 @@ func (client *AzureClient) CheckIfScalingGroupExists(name string) (bool, error)
106106
ctx := context.TODO()
107107
vmss, err := client.vMSSClient.Get(ctx, client.config.ResourceGroupName, name)
108108
if err != nil {
109-
return false, fmt.Errorf("couldn't check if a Virtual Machine Scale Set exists: %v", err)
109+
return false, fmt.Errorf("couldn't check if a Virtual Machine Scale Set exists: %w", err)
110110
}
111111

112112
return vmss.ID != nil, nil
113113
}
114114

115115
func (client *AzureClient) configure() error {
116116
authorizer, err := auth.NewAuthorizerFromEnvironment()
117-
118117
if err != nil {
119118
return err
120119
}

cmd/sync/azure_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestValidateAzureConfigValid(t *testing.T) {
9595

9696
err := validateAzureConfig(cfg)
9797
if err != nil {
98-
t.Errorf("validateAzureConfig() failed for the valid config: %v", err)
98+
t.Errorf("validateAzureConfig() failed for the valid config: %w", err)
9999
}
100100
}
101101

@@ -117,7 +117,7 @@ func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
117117
func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
118118
primaryFalse := false
119119
primaryTrue := true
120-
var tests = []struct {
120+
tests := []struct {
121121
ipConfig network.InterfaceIPConfiguration
122122
msg string
123123
}{
@@ -159,7 +159,7 @@ func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
159159

160160
func TestGetUpstreamsAzure(t *testing.T) {
161161
cfg := getValidAzureConfig()
162-
var upstreams = []azureUpstream{
162+
upstreams := []azureUpstream{
163163
{
164164
Name: "127.0.0.1",
165165
Port: 80,

cmd/sync/errormessages.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package main
22

3-
const errorMsgFormat = "The mandatory field %v is either empty or missing in the config file"
4-
const intervalErrorMsg = "The mandatory field sync_interval_in_seconds is either 0 or missing in the config file"
5-
const cloudProviderErrorMsg = "The field cloud_provider has invalid value %v in the config file"
6-
const defaultCloudProvider = "AWS"
7-
const upstreamNameErrorMsg = "The mandatory field name is either empty or missing for an upstream in the config file"
8-
const upstreamErrorMsgFormat = "The mandatory field %v is either empty or missing for the upstream %v in the config file"
9-
const upstreamPortErrorMsgFormat = "The mandatory field port is either zero or missing for the upstream %v in the config file"
10-
const upstreamKindErrorMsgFormat = "The mandatory field kind is either not equal to http or tcp or missing for the upstream %v in the config file"
11-
const upstreamMaxConnsErrorMsgFmt = "The field max_conns has invalid value %v in the config file"
12-
const upstreamMaxFailsErrorMsgFmt = "The field max_fails has invalid value %v in the config file"
13-
const upstreamFailTimeoutErrorMsgFmt = "The field fail_timeout has invalid value %v in the config file"
14-
const upstreamSlowStartErrorMsgFmt = "The field slow_start has invalid value %v in the config file"
3+
const (
4+
errorMsgFormat = "The mandatory field %v is either empty or missing in the config file"
5+
intervalErrorMsg = "The mandatory field sync_interval_in_seconds is either 0 or missing in the config file"
6+
cloudProviderErrorMsg = "The field cloud_provider has invalid value %v in the config file"
7+
defaultCloudProvider = "AWS"
8+
upstreamNameErrorMsg = "The mandatory field name is either empty or missing for an upstream in the config file"
9+
upstreamErrorMsgFormat = "The mandatory field %v is either empty or missing for the upstream %v in the config file"
10+
upstreamPortErrorMsgFormat = "The mandatory field port is either zero or missing for the upstream %v in the config file"
11+
upstreamKindErrorMsgFormat = "The mandatory field kind is either not equal to http or tcp or missing for the upstream %v in the config file"
12+
upstreamMaxConnsErrorMsgFmt = "The field max_conns has invalid value %v in the config file"
13+
upstreamMaxFailsErrorMsgFmt = "The field max_fails has invalid value %v in the config file"
14+
upstreamFailTimeoutErrorMsgFmt = "The field fail_timeout has invalid value %v in the config file"
15+
upstreamSlowStartErrorMsgFmt = "The field slow_start has invalid value %v in the config file"
16+
)

cmd/sync/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func main() {
2727
flag.Parse()
2828

2929
if *logFile != "" {
30-
logF, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
30+
logF, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600)
3131
if err != nil {
3232
log.Printf("Couldn't open the log file: %v", err)
3333
os.Exit(10)

cmd/sync/parameters.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

3-
const defaultFailTimeout = "10s"
4-
const defaultSlowStart = "0s"
3+
const (
4+
defaultFailTimeout = "10s"
5+
defaultSlowStart = "0s"
6+
)
57

68
func getFailTimeoutOrDefault(failTimeout string) string {
79
if failTimeout == "" {

0 commit comments

Comments
 (0)