Make golangci-lint more strict (#150)

* Reformat some YAML files

* Make `golangci-lint` more strict
This commit is contained in:
Joshua Spence
2023-06-23 09:00:12 +10:00
committed by GitHub
parent 5f0ca38414
commit 206f4be940
13 changed files with 105 additions and 76 deletions

View File

@@ -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'

View File

@@ -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'

View File

@@ -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'

View File

@@ -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'

View File

@@ -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)
}

View File

@@ -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 = ""
}

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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) {