Skip to content

Commit

Permalink
Enable more linters (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome authored Feb 14, 2024
1 parent 547224c commit a2d622b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 51 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-22.04
permissions:
contents: read
pull-requests: read # for golangci-lint-action
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
Expand All @@ -33,8 +30,6 @@ jobs:

- name: Lint Code
uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0
with:
only-new-issues: true

actionlint:
name: Actionlint
Expand Down
15 changes: 13 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ linters-settings:
- name: unused-parameter
- name: var-declaration
- name: var-naming
govet:
check-shadowing: true
enable-all: true

linters:
enable:
enable:
- asciicheck
- bidichk
- dupword
- errcheck
- errorlint
- gofmt
Expand All @@ -45,15 +50,21 @@ linters:
- misspell
- nilerr
- noctx
- perfsprint
- predeclared
- reassign
- revive
- staticcheck
- tagalign
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
disable-all: true
- whitespace
disable-all: true
issues:
max-issues-per-linter: 0
max-same-issues: 0
Expand Down
16 changes: 3 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,10 @@ repos:
- id: fix-byte-order-marker
- id: detect-private-key

- repo: local
hooks:
- id: golang-diff
name: create-go-diff
entry: bash -c 'git diff -p origin/main > /tmp/diff.patch'
language: system
types: [go]
pass_filenames: false

- repo: https://github.com/golangci/golangci-lint
rev: v1.56.1
hooks:
- id: golangci-lint
args: [--new-from-patch=/tmp/diff.patch]
- id: golangci-lint-full

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
Expand All @@ -59,7 +49,7 @@ repos:
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.12.1
hooks:
- id: markdownlint-cli2
- id: markdownlint-cli2

ci:
skip: [golang-diff, golangci-lint]
skip: [golangci-lint-full]
31 changes: 16 additions & 15 deletions cmd/sync/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -34,18 +35,18 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
if cfg.Region == "self" {
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}

conf, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, err
conf, loadErr := config.LoadDefaultConfig(context.TODO())
if loadErr != nil {
return nil, loadErr
}

client := imds.NewFromConfig(conf, func(o *imds.Options) {
o.HTTPClient = httpClient
})

response, err := client.GetRegion(context.TODO(), &imds.GetRegionInput{})
if err != nil {
return nil, fmt.Errorf("unable to retrieve region from ec2metadata: %w", err)
response, regionErr := client.GetRegion(context.TODO(), &imds.GetRegionInput{})
if regionErr != nil {
return nil, fmt.Errorf("unable to retrieve region from ec2metadata: %w", regionErr)
}
cfg.Region = response.Region
}
Expand Down Expand Up @@ -197,13 +198,13 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
const maxItems = 50
var result []string
keys := reflect.ValueOf(insIDtoIP).MapKeys()
instanceIds := make([]string, len(keys))
instanceIDs := make([]string, len(keys))

for i := 0; i < len(keys); i++ {
instanceIds[i] = keys[i].String()
instanceIDs[i] = keys[i].String()
}

batches := prepareBatches(maxItems, instanceIds)
batches := prepareBatches(maxItems, instanceIDs)
for _, batch := range batches {
params := &autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: batch,
Expand Down Expand Up @@ -249,13 +250,13 @@ type awsConfig struct {
type awsUpstream struct {
Name string
AutoscalingGroup string `yaml:"autoscaling_group"`
Port int
Kind string
MaxConns int `yaml:"max_conns"`
MaxFails int `yaml:"max_fails"`
FailTimeout string `yaml:"fail_timeout"`
SlowStart string `yaml:"slow_start"`
InService bool `yaml:"in_service"`
Port int
MaxConns int `yaml:"max_conns"`
MaxFails int `yaml:"max_fails"`
InService bool `yaml:"in_service"`
}

func validateAWSConfig(cfg *awsConfig) error {
Expand All @@ -264,12 +265,12 @@ func validateAWSConfig(cfg *awsConfig) error {
}

if len(cfg.Upstreams) == 0 {
return fmt.Errorf("there are no upstreams found in the config file")
return errors.New("there are no upstreams found in the config file")
}

for _, ups := range cfg.Upstreams {
if ups.Name == "" {
return fmt.Errorf(upstreamNameErrorMsg)
return errors.New(upstreamNameErrorMsg)
}
if ups.AutoscalingGroup == "" {
return fmt.Errorf(upstreamErrorMsgFormat, "autoscaling_group", ups.Name)
Expand Down
8 changes: 3 additions & 5 deletions cmd/sync/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,11 @@ func areEqualUpstreamsAWS(u1 awsUpstream, u2 Upstream) bool {
func TestPrepareBatches(t *testing.T) {
const maxItems = 3
ids := []string{"i-394ujfs", "i-dfdinf", "i-fsfsf", "i-8hr83hfwif", "i-nsnsnan"}
instanceIds := make([]string, len(ids))
instanceIDs := make([]string, len(ids))

for i := 0; i < len(ids); i++ {
instanceIds[i] = ids[i]
}
copy(instanceIDs, ids)

batches := prepareBatches(maxItems, instanceIds)
batches := prepareBatches(maxItems, instanceIDs)

if len(batches) > len(ids)/maxItems+1 {
t.Error("prepareBatches() didn't split the slice correctly")
Expand Down
11 changes: 6 additions & 5 deletions cmd/sync/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"

"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
Expand Down Expand Up @@ -154,12 +155,12 @@ type azureConfig struct {
type azureUpstream struct {
Name string
VMScaleSet string `yaml:"virtual_machine_scale_set"`
Port int
Kind string
MaxConns int `yaml:"max_conns"`
MaxFails int `yaml:"max_fails"`
FailTimeout string `yaml:"fail_timeout"`
SlowStart string `yaml:"slow_start"`
Port int
MaxConns int `yaml:"max_conns"`
MaxFails int `yaml:"max_fails"`
}

func validateAzureConfig(cfg *azureConfig) error {
Expand All @@ -172,12 +173,12 @@ func validateAzureConfig(cfg *azureConfig) error {
}

if len(cfg.Upstreams) == 0 {
return fmt.Errorf("there are no upstreams found in the config file")
return errors.New("there are no upstreams found in the config file")
}

for _, ups := range cfg.Upstreams {
if ups.Name == "" {
return fmt.Errorf(upstreamNameErrorMsg)
return errors.New(upstreamNameErrorMsg)
}
if ups.VMScaleSet == "" {
return fmt.Errorf(upstreamErrorMsgFormat, "virtual_machine_scale_set", ups.Name)
Expand Down
11 changes: 6 additions & 5 deletions cmd/sync/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"time"

Expand All @@ -10,8 +11,8 @@ import (
// commonConfig stores the configuration parameters common to all providers
type commonConfig struct {
APIEndpoint string `yaml:"api_endpoint"`
SyncIntervalInSeconds time.Duration `yaml:"sync_interval_in_seconds"`
CloudProvider string `yaml:"cloud_provider"`
SyncIntervalInSeconds time.Duration `yaml:"sync_interval_in_seconds"`
}

func parseCommonConfig(data []byte) (*commonConfig, error) {
Expand All @@ -35,7 +36,7 @@ func validateCommonConfig(cfg *commonConfig) error {
}

if cfg.SyncIntervalInSeconds == 0 {
return fmt.Errorf(intervalErrorMsg)
return errors.New(intervalErrorMsg)
}

if cfg.CloudProvider == "" {
Expand All @@ -51,13 +52,13 @@ func validateCommonConfig(cfg *commonConfig) error {

// Upstream is the cloud agnostic representation of an Upstream (eg, common fields for every cloud provider)
type Upstream struct {
MaxConns *int
MaxFails *int
Name string
Port int
ScalingGroup string
Kind string
MaxConns *int
MaxFails *int
FailTimeout string
SlowStart string
Port int
InService bool
}
1 change: 0 additions & 1 deletion cmd/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func main() {
upstream.Name, upstream.ScalingGroup, addedAddresses, removedAddresses, updatedAddresses)
}
}

}

select {
Expand Down

0 comments on commit a2d622b

Please sign in to comment.