Skip to content

Commit 50f5224

Browse files
committed
add linter
1 parent 136e425 commit 50f5224

File tree

14 files changed

+111
-192
lines changed

14 files changed

+111
-192
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,12 @@ jobs:
1414
- name: Checkout code
1515
uses: actions/checkout@v4
1616

17-
- name: Set up Go
18-
uses: actions/setup-go@v4
17+
- uses: actions/setup-go@v5
1918
with:
20-
go-version: '1.26'
21-
22-
- name: Install dependencies
23-
run: go mod download
19+
go-version: stable
2420

2521
- name: Run tests
26-
run: go test -v -race -coverprofile=coverage.out ./...
27-
28-
- name: Upload coverage to Codecov
29-
uses: codecov/codecov-action@v3
30-
with:
31-
file: ./coverage.out
32-
fail_ci_if_error: false
22+
run: go test ./...
3323

3424
build:
3525
name: Build
@@ -39,9 +29,9 @@ jobs:
3929
uses: actions/checkout@v4
4030

4131
- name: Set up Go
42-
uses: actions/setup-go@v4
32+
uses: actions/setup-go@v5
4333
with:
44-
go-version: '1.26'
34+
go-version: stable
4535

4636
- name: Install dependencies
4737
run: go mod download
@@ -64,61 +54,21 @@ jobs:
6454
# Build for macOS ARM64 (Apple Silicon)
6555
GOOS=darwin GOARCH=arm64 go build -o bin/serveradmin-go-darwin-arm64 .
6656
67-
- name: Upload build artifacts
68-
uses: actions/upload-artifact@v4
69-
with:
70-
name: serveradmin-go-builds
71-
path: bin/
72-
73-
lint:
74-
name: Lint
57+
format-check:
58+
name: Format Check
7559
runs-on: ubuntu-latest
7660
steps:
7761
- name: Checkout code
7862
uses: actions/checkout@v4
7963

80-
- name: Set up Go
81-
uses: actions/setup-go@v4
64+
- uses: actions/setup-go@v5
8265
with:
83-
go-version: '1.26'
66+
go-version: stable
8467

8568
- name: Run golangci-lint
86-
uses: golangci/golangci-lint-action@v4
69+
uses: golangci/golangci-lint-action@v8
8770
with:
8871
version: latest
89-
args: --enable-all --timeout=5m --verbose
90-
working-directory: .
91-
only-new-issues: false
92-
fail-on-error: true
93-
94-
format-check:
95-
name: Format Check
96-
runs-on: ubuntu-latest
97-
steps:
98-
- name: Checkout code
99-
uses: actions/checkout@v4
100-
101-
- name: Set up Go
102-
uses: actions/setup-go@v4
103-
with:
104-
go-version: '1.26'
105-
106-
- name: Check gofmt
107-
run: |
108-
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
109-
echo "Code is not formatted. Please run 'gofmt -s -w .'"
110-
gofmt -s -l .
111-
exit 1
112-
fi
113-
114-
- name: Check goimports
115-
run: |
116-
go install golang.org/x/tools/cmd/goimports@latest
117-
if [ "$(goimports -l . | wc -l)" -gt 0 ]; then
118-
echo "Imports are not properly formatted. Please run 'goimports -w .'"
119-
goimports -l .
120-
exit 1
121-
fi
12272

12373
- name: Check go mod tidy
12474
run: |

.github/workflows/golangci-lint.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.golangci.yml

Lines changed: 62 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,71 @@
1-
run:
2-
timeout: 5m
3-
go: "1.24"
4-
modules-download-mode: readonly
5-
allow-parallel-runners: true
6-
allow-serial-runners: false
7-
1+
version: "2"
82
linters:
93
enable:
10-
- gofmt
11-
- goimports
12-
- govet
13-
- errcheck
14-
- staticcheck
15-
- unused
16-
- gosimple
17-
- structcheck
18-
- varcheck
19-
- ineffassign
20-
- deadcode
21-
- typecheck
22-
- gosec
23-
- gocyclo
4+
- asasalint
5+
- asciicheck
6+
- bodyclose
7+
- copyloopvar
8+
- dogsled
249
- dupl
25-
- misspell
26-
- unparam
27-
- nakedret
28-
- prealloc
10+
- errname
11+
- errorlint
12+
- exhaustive
13+
- fatcontext
14+
- gocheckcompilerdirectives
15+
- gochecknoinits
2916
- gocritic
30-
- gomnd
31-
- gomodguard
17+
- gocyclo
3218
- goprintffuncname
19+
- gosec
20+
- importas
21+
- intrange
22+
- loggercheck
23+
- makezero
24+
- mirror
25+
- misspell
26+
- musttag
27+
- nakedret
28+
- nilerr
3329
- noctx
3430
- nolintlint
31+
- perfsprint
32+
- prealloc
33+
- predeclared
34+
- revive
35+
- rowserrcheck
36+
- staticcheck
37+
- tagalign
38+
- testifylint
39+
- thelper
3540
- unconvert
41+
- unparam
42+
- usestdlibvars
43+
- wastedassign
3644
- whitespace
37-
38-
linters-settings:
39-
gocyclo:
40-
min-complexity: 15
41-
dupl:
42-
threshold: 100
43-
gocritic:
44-
enabled-tags:
45-
- diagnostic
46-
- experimental
47-
- opinionated
48-
- performance
49-
- style
50-
gomnd:
51-
checks: argument,case,condition,operation,return,assign
52-
gomodguard:
53-
allowed:
54-
modules:
55-
- gopkg.in/yaml.v3
56-
- github.com/stretchr/testify
57-
- golang.org/x/crypto
58-
- golang.org/x/sys
59-
blocked:
60-
modules:
61-
- github.com/ugorji/go/codec
62-
- github.com/ugorji/go
63-
64-
issues:
65-
exclude-rules:
66-
- path: _test\.go
67-
linters:
68-
- gomnd
69-
- gocyclo
70-
- dupl
71-
- path: testdata/
72-
linters:
73-
- all
74-
max-issues-per-linter: 0
75-
max-same-issues: 0
76-
exclude-use-default: false
77-
new: false
78-
new-from-rev: ""
79-
new-from-patch: false
45+
exclusions:
46+
generated: lax
47+
presets:
48+
- comments
49+
- common-false-positives
50+
- legacy
51+
- std-error-handling
52+
rules:
53+
- linters:
54+
- errcheck
55+
- perfsprint
56+
- usestdlibvars
57+
path: _test\.go
58+
paths:
59+
- third_party$
60+
- builtin$
61+
- examples$
62+
formatters:
63+
enable:
64+
- gci
65+
- gofumpt
66+
exclusions:
67+
generated: lax
68+
paths:
69+
- third_party$
70+
- builtin$
71+
- examples$

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ test-coverage:
1111
go tool cover -html=coverage.out -o coverage.html
1212

1313
linter:
14-
golangci-lint run --enable-all
14+
golangci-lint run

adminapi/api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"context"
77
"crypto/hmac"
88
"crypto/rand"
9-
"crypto/sha1"
9+
"crypto/sha1" //nolint:gosec // SHA1 is required by the protocol
1010
"encoding/base64"
1111
"encoding/hex"
1212
"encoding/json"
@@ -54,8 +54,8 @@ func (s ServerObject) GetString(attribute string) any {
5454
return nil
5555
}
5656

57-
// ObjectId returns the "object_id" attribute of the ServerObject
58-
func (s ServerObject) ObjectId() int {
57+
// ObjectID returns the "object_id" attribute of the ServerObject
58+
func (s ServerObject) ObjectID() int {
5959
return s.Get("object_id").(int)
6060
}
6161

@@ -66,7 +66,7 @@ func sendRequest(endpoint string, postData any) (*http.Response, error) {
6666
}
6767

6868
postStr, _ := json.Marshal(postData)
69-
req, err := http.NewRequestWithContext(context.Background(), "GET", config.baseURL+endpoint, bytes.NewBuffer(postStr))
69+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, config.baseURL+endpoint, bytes.NewBuffer(postStr))
7070
if err != nil {
7171
return nil, fmt.Errorf("failed to create request: %w", err)
7272
}
@@ -152,7 +152,7 @@ func calcMessage(timestamp int64, data []byte) []byte {
152152

153153
// calcAppID computes SHA-1 hash of the auth token
154154
func calcAppID(authToken []byte) string {
155-
hash := sha1.Sum(authToken)
155+
hash := sha1.Sum(authToken) //nolint:gosec // SHA1 is required by the protocol
156156

157157
return hex.EncodeToString(hash[:])
158158
}

adminapi/api_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func TestFakeServer(t *testing.T) {
@@ -35,20 +36,20 @@ func TestFakeServer(t *testing.T) {
3536
query.SetAttributes([]string{"hostname"})
3637

3738
servers, err := query.All()
38-
assert.NoError(t, err)
39+
require.NoError(t, err)
3940
assert.Len(t, servers, 1)
4041

4142
object := servers[0]
4243
assert.Equal(t, "foo.bar.local", object.Get("hostname"))
4344
assert.Equal(t, "foo.bar.local", object.GetString("hostname"))
4445
assert.Equal(t, 483903, object.Get("object_id"))
45-
assert.Equal(t, 483903, object.ObjectId())
46-
assert.Equal(t, nil, object.GetString("object_id"))
46+
assert.Equal(t, 483903, object.ObjectID())
47+
assert.Nil(t, object.GetString("object_id"))
4748
assert.Nil(t, object.Get("nope"))
4849
assert.Nil(t, object.GetString("nope"))
4950

5051
one, err := query.One()
51-
assert.NoError(t, err)
52+
require.NoError(t, err)
5253
assert.Equal(t, 483903, one.Get("object_id"))
5354
}
5455

adminapi/config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package adminapi
22

33
import (
44
"crypto/rand"
5+
"errors"
56
"fmt"
67
"net"
78
"os"
@@ -33,11 +34,11 @@ var loadConfig = func() (config, error) {
3334
apiVersion: version,
3435
}
3536

36-
baseUrl := os.Getenv("SERVERADMIN_BASE_URL")
37-
if baseUrl == "" {
38-
return cfg, fmt.Errorf("env var SERVERADMIN_BASE_URL not set")
37+
baseURL := os.Getenv("SERVERADMIN_BASE_URL")
38+
if baseURL == "" {
39+
return cfg, errors.New("env var SERVERADMIN_BASE_URL not set")
3940
}
40-
cfg.baseURL = strings.TrimRight(baseUrl, "/api")
41+
cfg.baseURL = strings.TrimRight(baseURL, "/api")
4142

4243
if privateKeyPath, ok := os.LookupEnv("SERVERADMIN_KEY_PATH"); ok {
4344
keyBytes, err := os.ReadFile(privateKeyPath)
@@ -72,7 +73,7 @@ var loadConfig = func() (config, error) {
7273
}
7374

7475
if len(cfg.authToken) == 0 && cfg.sshSigner == nil {
75-
return cfg, fmt.Errorf("no authentication method found: set SERVERADMIN_TOKEN/SERVERADMIN_KEY_PATH/SSH_AUTH_SOCK")
76+
return cfg, errors.New("no authentication method found: set SERVERADMIN_TOKEN/SERVERADMIN_KEY_PATH/SSH_AUTH_SOCK")
7677
}
7778

7879
return cfg, nil

0 commit comments

Comments
 (0)