Skip to content

Add more linters #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Lint Code
uses: golangci/golangci-lint-action@v2.5.2
uses: golangci/golangci-lint-action@v2
with:
args: --timeout ${{ env.GOLANGCI_TIMEOUT }}
27 changes: 22 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@ linters-settings:

linters:
enable:
- asciicheck
- deadcode
- errcheck
- errorlint
- gofmt
- gofumpt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- makezero
- misspell
- gofmt
- unparam
- unconvert
- nilerr
- noctx
- predeclared
- staticcheck
- structcheck
- errcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign
disable-all: true

issues:
max-issues-per-linter: 0
max-same-issues: 0
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test:

.PHONY: lint
lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint run
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

.PHONY: build
build:
Expand Down
8 changes: 4 additions & 4 deletions cmd/sync/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
awsClient := &AWSClient{}
cfg, err := parseAWSConfig(data)
if err != nil {
return nil, fmt.Errorf("error validating config: %v", err)
return nil, fmt.Errorf("error validating config: %w", err)
}

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

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

err = awsClient.configure()
if err != nil {
return nil, fmt.Errorf("error configuring AWS Client: %v", err)
return nil, fmt.Errorf("error configuring AWS Client: %w", err)
}

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

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

return len(response.Reservations) > 0, nil
Expand Down
5 changes: 2 additions & 3 deletions cmd/sync/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ func TestValidateAWSConfigValid(t *testing.T) {

err := validateAWSConfig(cfg)
if err != nil {
t.Errorf("validateAWSConfig() failed for the valid config: %v", err)
t.Errorf("validateAWSConfig() failed for the valid config: %w", err)
}
}

func TestGetUpstreamsAWS(t *testing.T) {
cfg := getValidAWSConfig()
var upstreams = []awsUpstream{
upstreams := []awsUpstream{
{
Name: "127.0.0.1",
Port: 80,
Expand Down Expand Up @@ -184,5 +184,4 @@ func TestPrepareBatches(t *testing.T) {
t.Errorf("prepareBatches() returned a batch with len > %v", maxItems)
}
}

}
7 changes: 3 additions & 4 deletions cmd/sync/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func NewAzureClient(data []byte) (*AzureClient, error) {
azureClient := &AzureClient{}
cfg, err := parseAzureConfig(data)
if err != nil {
return nil, fmt.Errorf("error validating config: %v", err)
return nil, fmt.Errorf("error validating config: %w", err)
}

azureClient.config = cfg

err = azureClient.configure()
if err != nil {
return nil, fmt.Errorf("error configuring Azure Client: %v", err)
return nil, fmt.Errorf("error configuring Azure Client: %w", err)
}

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

return vmss.ID != nil, nil
}

func (client *AzureClient) configure() error {
authorizer, err := auth.NewAuthorizerFromEnvironment()

if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/sync/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestValidateAzureConfigValid(t *testing.T) {

err := validateAzureConfig(cfg)
if err != nil {
t.Errorf("validateAzureConfig() failed for the valid config: %v", err)
t.Errorf("validateAzureConfig() failed for the valid config: %w", err)
}
}

Expand All @@ -117,7 +117,7 @@ func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
primaryFalse := false
primaryTrue := true
var tests = []struct {
tests := []struct {
ipConfig network.InterfaceIPConfiguration
msg string
}{
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {

func TestGetUpstreamsAzure(t *testing.T) {
cfg := getValidAzureConfig()
var upstreams = []azureUpstream{
upstreams := []azureUpstream{
{
Name: "127.0.0.1",
Port: 80,
Expand Down
26 changes: 14 additions & 12 deletions cmd/sync/errormessages.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

const errorMsgFormat = "The mandatory field %v is either empty or missing in the config file"
const intervalErrorMsg = "The mandatory field sync_interval_in_seconds is either 0 or missing in the config file"
const cloudProviderErrorMsg = "The field cloud_provider has invalid value %v in the config file"
const defaultCloudProvider = "AWS"
const upstreamNameErrorMsg = "The mandatory field name is either empty or missing for an upstream in the config file"
const upstreamErrorMsgFormat = "The mandatory field %v is either empty or missing for the upstream %v in the config file"
const upstreamPortErrorMsgFormat = "The mandatory field port is either zero or missing for the upstream %v in the config file"
const upstreamKindErrorMsgFormat = "The mandatory field kind is either not equal to http or tcp or missing for the upstream %v in the config file"
const upstreamMaxConnsErrorMsgFmt = "The field max_conns has invalid value %v in the config file"
const upstreamMaxFailsErrorMsgFmt = "The field max_fails has invalid value %v in the config file"
const upstreamFailTimeoutErrorMsgFmt = "The field fail_timeout has invalid value %v in the config file"
const upstreamSlowStartErrorMsgFmt = "The field slow_start has invalid value %v in the config file"
const (
errorMsgFormat = "The mandatory field %v is either empty or missing in the config file"
intervalErrorMsg = "The mandatory field sync_interval_in_seconds is either 0 or missing in the config file"
cloudProviderErrorMsg = "The field cloud_provider has invalid value %v in the config file"
defaultCloudProvider = "AWS"
upstreamNameErrorMsg = "The mandatory field name is either empty or missing for an upstream in the config file"
upstreamErrorMsgFormat = "The mandatory field %v is either empty or missing for the upstream %v in the config file"
upstreamPortErrorMsgFormat = "The mandatory field port is either zero or missing for the upstream %v in the config file"
upstreamKindErrorMsgFormat = "The mandatory field kind is either not equal to http or tcp or missing for the upstream %v in the config file"
upstreamMaxConnsErrorMsgFmt = "The field max_conns has invalid value %v in the config file"
upstreamMaxFailsErrorMsgFmt = "The field max_fails has invalid value %v in the config file"
upstreamFailTimeoutErrorMsgFmt = "The field fail_timeout has invalid value %v in the config file"
upstreamSlowStartErrorMsgFmt = "The field slow_start has invalid value %v in the config file"
)
2 changes: 1 addition & 1 deletion cmd/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
flag.Parse()

if *logFile != "" {
logF, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
logF, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
log.Printf("Couldn't open the log file: %v", err)
os.Exit(10)
Expand Down
6 changes: 4 additions & 2 deletions cmd/sync/parameters.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

const defaultFailTimeout = "10s"
const defaultSlowStart = "0s"
const (
defaultFailTimeout = "10s"
defaultSlowStart = "0s"
)

func getFailTimeoutOrDefault(failTimeout string) string {
if failTimeout == "" {
Expand Down
6 changes: 4 additions & 2 deletions cmd/sync/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ var validTimeSuffixes = []string{
"y",
}

var durationEscaped = strings.Join(validTimeSuffixes, "|")
var validNginxTime = regexp.MustCompile(`^([0-9]+([` + durationEscaped + `]?){0,1} *)+$`)
var (
durationEscaped = strings.Join(validTimeSuffixes, "|")
validNginxTime = regexp.MustCompile(`^([0-9]+([` + durationEscaped + `]?){0,1} *)+$`)
)

func isValidTime(time string) bool {
if time == "" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/sync/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "testing"

func TestIsValidTime(t *testing.T) {
var testsWithValidInput = []string{"1", "1m10s", "11 11", "5m 30s", "1s", "100m", "5w", "15m", "11M", "3h", "100y", "600"}
var invalidInput = []string{"ss", "rM", "m0m", "s1s", "-5s", "1L"}
testsWithValidInput := []string{"1", "1m10s", "11 11", "5m 30s", "1s", "100m", "5w", "15m", "11M", "3h", "100y", "600"}
invalidInput := []string{"ss", "rM", "m0m", "s1s", "-5s", "1L"}

for _, test := range testsWithValidInput {
valid := isValidTime(test)
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ require (
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/aws/aws-sdk-go v1.38.38
github.com/golangci/golangci-lint v1.41.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/nginxinc/nginx-plus-go-client v0.8.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading