From 206f4be940b6f581dff7ad70fd9c49f5e6d782d2 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Fri, 23 Jun 2023 09:00:12 +1000 Subject: [PATCH] Make `golangci-lint` more strict (#150) * Reformat some YAML files * Make `golangci-lint` more strict --- .github/dependabot.yml | 12 +++---- .github/workflows/ci.yaml | 48 +++++++++++++------------- .github/workflows/generate.yaml | 14 ++++---- .golangci.yaml | 61 +++++++++++++++++++++++---------- fields/extract.go | 10 ++++-- fields/main.go | 14 ++++---- fields/main_test.go | 5 +-- fields/version.go | 3 +- unifi/ap_group.go | 2 +- unifi/device.go | 1 - unifi/sites.go | 3 +- unifi/unifi.go | 4 +-- unifi/user.go | 4 +-- 13 files changed, 105 insertions(+), 76 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 4c589e7..e7406ac 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,11 +1,11 @@ --- version: 2 updates: - - package-ecosystem: "gomod" - directory: "/" + - package-ecosystem: 'github-actions' + directory: '/' schedule: - interval: "daily" - - package-ecosystem: "github-actions" - directory: "/" + interval: 'daily' + - package-ecosystem: 'gomod' + directory: '/' schedule: - interval: "daily" + interval: 'daily' diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7d364c8..bdfc7c7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -3,47 +3,47 @@ on: pull_request: {} push: branches: - - "main" + - 'main' tags: - - "v*" + - 'v*' jobs: build: - runs-on: "ubuntu-latest" + runs-on: 'ubuntu-latest' steps: - - uses: "actions/checkout@v3.3.0" - - uses: "actions/setup-go@v4" - - run: "go build ./..." + - uses: 'actions/checkout@v3.3.0' + - uses: 'actions/setup-go@v4' + - run: 'go build ./...' generate: - needs: "build" - runs-on: "ubuntu-latest" + needs: 'build' + runs-on: 'ubuntu-latest' steps: - - uses: "actions/checkout@v3.3.0" - - uses: "actions/setup-go@v4" + - uses: 'actions/checkout@v3.3.0' + - uses: 'actions/setup-go@v4' - - run: "go generate unifi/device.go" - - run: "git diff --compact-summary --exit-code" + - run: 'go generate unifi/device.go' + - run: 'git diff --compact-summary --exit-code' lint: - runs-on: "ubuntu-latest" + runs-on: 'ubuntu-latest' steps: - - uses: "actions/checkout@v3.3.0" - - uses: "actions/setup-go@v4" - - uses: "golangci/golangci-lint-action@v3" + - uses: 'actions/checkout@v3.3.0' + - uses: 'actions/setup-go@v4' + - uses: 'golangci/golangci-lint-action@v3' with: skip-pkg-cache: true test: - needs: "build" - runs-on: "ubuntu-latest" + needs: 'build' + runs-on: 'ubuntu-latest' steps: - - uses: "actions/checkout@v3.3.0" - - uses: "actions/setup-go@v4" - - run: "go test ./..." + - uses: 'actions/checkout@v3.3.0' + - uses: 'actions/setup-go@v4' + - run: 'go test ./...' yamllint: - runs-on: "ubuntu-latest" + runs-on: 'ubuntu-latest' steps: - - uses: "actions/checkout@v3.3.0" - - uses: "ibiqlik/action-yamllint@v3" + - uses: 'actions/checkout@v3.3.0' + - uses: 'ibiqlik/action-yamllint@v3' diff --git a/.github/workflows/generate.yaml b/.github/workflows/generate.yaml index 0d51b04..0ca384f 100644 --- a/.github/workflows/generate.yaml +++ b/.github/workflows/generate.yaml @@ -1,19 +1,19 @@ --- on: schedule: - - cron: "0 0 * * *" + - cron: '0 0 * * *' workflow_dispatch: {} jobs: fields: - runs-on: "ubuntu-latest" + runs-on: 'ubuntu-latest' steps: - - uses: "actions/checkout@v3.3.0" - - uses: "actions/setup-go@v4" + - uses: 'actions/checkout@v3.3.0' + - uses: 'actions/setup-go@v4' # TODO: Automatically merge the PR if tests pass. - - run: "go generate unifi/fields.go" - - uses: "peter-evans/create-pull-request@v5" + - run: 'go generate unifi/fields.go' + - uses: 'peter-evans/create-pull-request@v5' with: delete-branch: true - title: "Update to latest controller version" + title: 'Update to latest controller version' diff --git a/.golangci.yaml b/.golangci.yaml index 28bc372..c841774 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,21 +1,44 @@ --- linters: - disable-all: true - enable: - - "deadcode" - - "errcheck" - - "errorlint" - - "gofmt" - - "gosimple" - - "govet" - - "ineffassign" - - "makezero" - - "misspell" - - "nakedret" - - "nilerr" - - "staticcheck" - - "structcheck" - - "unconvert" - - "unparam" - - "unused" - - "varcheck" + enable-all: true + disable: + - 'depguard' + - 'tagliatelle' + + # Temporary + - 'cyclop' + - 'dupl' + - 'exhaustruct' + - 'forbidigo' + - 'funlen' + - 'gochecknoglobals' + - 'gocognit' + - 'goconst' + - 'gocritic' + - 'gocyclo' + - 'godox' + - 'goerr113' + - 'gomnd' + - 'gosec' + - 'lll' + - 'maintidx' + - 'nestif' + - 'nlreturn' + - 'paralleltest' + - 'revive' + - 'stylecheck' + - 'varnamelen' + - 'wrapcheck' + - 'wsl' + + # Deprecated + - 'deadcode' + - 'exhaustivestruct' + - 'golint' + - 'ifshort' + - 'interfacer' + - 'maligned' + - 'nosnakecase' + - 'scopelint' + - 'structcheck' + - 'varcheck' diff --git a/fields/extract.go b/fields/extract.go index 451df5e..4481ebf 100644 --- a/fields/extract.go +++ b/fields/extract.go @@ -3,6 +3,7 @@ package main import ( "archive/tar" "archive/zip" + "context" "encoding/json" "errors" "fmt" @@ -20,7 +21,12 @@ import ( ) func downloadJar(url *url.URL, outputDir string) (string, error) { - debResp, err := http.Get(url.String()) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url.String(), nil) + if err != nil { + return "", fmt.Errorf("unable to download deb: %w", err) + } + + debResp, err := http.DefaultClient.Do(req) if err != nil { return "", fmt.Errorf("unable to download deb: %w", err) } @@ -147,7 +153,7 @@ func extractJSON(jarFile, fieldsDir string) error { return fmt.Errorf("unable to marshal setting %q: %w", k, err) } - err = os.WriteFile(filepath.Join(fieldsDir, fileName), data, 0755) + err = os.WriteFile(filepath.Join(fieldsDir, fileName), data, 0o755) if err != nil { return fmt.Errorf("unable to write new settings file: %w", err) } diff --git a/fields/main.go b/fields/main.go index ee5e6eb..e777041 100644 --- a/fields/main.go +++ b/fields/main.go @@ -247,7 +247,7 @@ func main() { panic(err) } - err = os.MkdirAll(fieldsDir, 0755) + err = os.MkdirAll(fieldsDir, 0o755) if err != nil { panic(err) } @@ -434,7 +434,7 @@ func main() { } _ = os.Remove(filepath.Join(outDir, goFile)) - if err := os.WriteFile(filepath.Join(outDir, goFile), ([]byte)(code), 0644); err != nil { + if err := os.WriteFile(filepath.Join(outDir, goFile), ([]byte)(code), 0o644); err != nil { panic(err) } } @@ -453,7 +453,7 @@ const UnifiVersion = %q panic(err) } - if err := os.WriteFile(filepath.Join(outDir, "version.generated.go"), versionGo, 0644); err != nil { + if err := os.WriteFile(filepath.Join(outDir, "version.generated.go"), versionGo, 0o644); err != nil { panic(err) } @@ -476,17 +476,18 @@ func (r *Resource) processFields(fields map[string]interface{}) { } } -func (r *Resource) fieldInfoFromValidation(name string, validation interface{}) (fieldInfo *FieldInfo, err error) { +func (r *Resource) fieldInfoFromValidation(name string, validation interface{}) (*FieldInfo, error) { fieldName := strcase.ToCamel(name) fieldName = cleanName(fieldName, fieldReps) empty := &FieldInfo{} + var fieldInfo *FieldInfo switch validation := validation.(type) { case []interface{}: if len(validation) == 0 { fieldInfo = NewFieldInfo(fieldName, name, "string", "", false, true, "") - err = r.FieldProcessor(fieldName, fieldInfo) + err := r.FieldProcessor(fieldName, fieldInfo) return fieldInfo, err } if len(validation) > 1 { @@ -519,7 +520,7 @@ func (r *Resource) fieldInfoFromValidation(name string, validation interface{}) result.Fields[child.FieldName] = child } - err = r.FieldProcessor(fieldName, result) + err := r.FieldProcessor(fieldName, result) r.Types[typeName] = result return result, err @@ -535,7 +536,6 @@ func (r *Resource) fieldInfoFromValidation(name string, validation interface{}) return fieldInfo, r.FieldProcessor(fieldName, fieldInfo) default: if _, err := strconv.ParseFloat(normalized, 64); err == nil { - if normalized == "09" || normalized == "09.09" { fieldValidation = "" } diff --git a/fields/main_test.go b/fields/main_test.go index 58e79d4..561b754 100644 --- a/fields/main_test.go +++ b/fields/main_test.go @@ -2,8 +2,9 @@ package main import ( "fmt" - assert "github.com/stretchr/testify/assert" "testing" + + assert "github.com/stretchr/testify/assert" ) func TestFieldInfoFromValidation(t *testing.T) { @@ -37,7 +38,7 @@ func TestFieldInfoFromValidation(t *testing.T) { } fieldInfo, err := resource.fieldInfoFromValidation("fieldName", c.validation) - //actualType, actualComment, actualOmitEmpty, err := fieldInfoFromValidation(c.validation) + // actualType, actualComment, actualOmitEmpty, err := fieldInfoFromValidation(c.validation) if err != nil { t.Fatal(err) } diff --git a/fields/version.go b/fields/version.go index 88080d3..db1ddc1 100644 --- a/fields/version.go +++ b/fields/version.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "net/http" "net/url" @@ -19,7 +20,7 @@ func latestUnifiVersion() (*version.Version, *url.URL, error) { query.Add("filter", firmwareUpdateApiFilter("product", unifiControllerProduct)) url.RawQuery = query.Encode() - req, err := http.NewRequest(http.MethodGet, url.String(), nil) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url.String(), nil) if err != nil { return nil, nil, err } diff --git a/unifi/ap_group.go b/unifi/ap_group.go index 34e3f8d..5e02d34 100644 --- a/unifi/ap_group.go +++ b/unifi/ap_group.go @@ -5,7 +5,7 @@ import ( "fmt" ) -// just to fix compile issues with the import +// just to fix compile issues with the import. var ( _ fmt.Formatter _ context.Context diff --git a/unifi/device.go b/unifi/device.go index 3e78dbf..d3ecfa6 100644 --- a/unifi/device.go +++ b/unifi/device.go @@ -45,7 +45,6 @@ func (c *Client) UpdateDevice(ctx context.Context, site string, d *Device) (*Dev func (c *Client) GetDevice(ctx context.Context, site, id string) (*Device, error) { devices, err := c.ListDevice(ctx, site) - if err != nil { return nil, err } diff --git a/unifi/sites.go b/unifi/sites.go index eec4b08..2ac5d82 100644 --- a/unifi/sites.go +++ b/unifi/sites.go @@ -16,7 +16,7 @@ type Site struct { Name string `json:"name"` Description string `json:"desc"` - //Role string `json:"role"` + // Role string `json:"role"` } func (c *Client) ListSites(ctx context.Context) ([]Site, error) { @@ -35,7 +35,6 @@ func (c *Client) ListSites(ctx context.Context) ([]Site, error) { func (c *Client) GetSite(ctx context.Context, id string) (*Site, error) { sites, err := c.ListSites(ctx) - if err != nil { return nil, err } diff --git a/unifi/unifi.go b/unifi/unifi.go index ef2a881..8688c40 100644 --- a/unifi/unifi.go +++ b/unifi/unifi.go @@ -95,7 +95,7 @@ func (c *Client) setAPIUrlStyle(ctx context.Context) error { // see https://github.com/unifi-poller/unifi/blob/4dc44f11f61a2e08bf7ec5b20c71d5bced837b5d/unifi.go#L101-L104 // and https://github.com/unifi-poller/unifi/commit/43a6b225031a28f2b358f52d03a7217c7b524143 - req, err := http.NewRequestWithContext(ctx, "GET", c.baseURL.String(), nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL.String(), nil) if err != nil { return err } @@ -243,7 +243,7 @@ func (c *Client) do(ctx context.Context, method, relativeURL string, reqBody int c.csrf = resp.Header.Get("x-csrf-token") } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { errBody := struct { Meta meta `json:"meta"` Data []struct { diff --git a/unifi/user.go b/unifi/user.go index e42cfa4..06106c5 100644 --- a/unifi/user.go +++ b/unifi/user.go @@ -65,9 +65,9 @@ func (c *Client) CreateUser(ctx context.Context, site string, d *User) (*User, e return nil, &NotFoundError{} } - new := respBody.Data[0].Data[0] + user := respBody.Data[0].Data[0] - return &new, nil + return &user, nil } func (c *Client) stamgr(ctx context.Context, site, cmd string, data map[string]interface{}) ([]User, error) {