Skip to content

Commit

Permalink
Make golangci-lint more strict (#150)
Browse files Browse the repository at this point in the history
* Reformat some YAML files

* Make `golangci-lint` more strict
  • Loading branch information
joshuaspence authored Jun 22, 2023
1 parent 5f0ca38 commit 206f4be
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 76 deletions.
12 changes: 6 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -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'
48 changes: 24 additions & 24 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
14 changes: 7 additions & 7 deletions .github/workflows/generate.yaml
Original file line number Diff line number Diff line change
@@ -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'
61 changes: 42 additions & 19 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -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'
10 changes: 8 additions & 2 deletions fields/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"archive/tar"
"archive/zip"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions fields/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func main() {
panic(err)
}

err = os.MkdirAll(fieldsDir, 0755)
err = os.MkdirAll(fieldsDir, 0o755)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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

Expand All @@ -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 = ""
}
Expand Down
5 changes: 3 additions & 2 deletions fields/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion fields/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"net/http"
"net/url"
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion unifi/ap_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion unifi/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions unifi/sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions unifi/unifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions unifi/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 206f4be

Please sign in to comment.