Skip to content

Commit 18bf9e0

Browse files
authored
upgrade golangci-lint tool and enable more tools (#214)
Also fixed another 21 issues identified Signed-off-by: Leslie Qi Wang <qiwa@pensando.io>
1 parent b291e34 commit 18bf9e0

19 files changed

Lines changed: 179 additions & 147 deletions

File tree

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
run: go mod download
3535

3636
- name: Run golangci-lint
37-
uses: golangci/golangci-lint-action@v4
37+
uses: golangci/golangci-lint-action@v8
3838
with:
3939
version: latest
4040
args: --timeout=5m

.golangci.yml

Lines changed: 107 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,131 @@
1+
version: "2"
12
run:
2-
timeout: 5m
3-
go: '1.21'
4-
5-
linters-settings:
6-
govet:
7-
enable:
8-
- shadow
9-
revive:
10-
min-confidence: 0.8
11-
gocyclo:
12-
min-complexity: 15
13-
dupl:
14-
threshold: 100
15-
goconst:
16-
min-len: 2
17-
min-occurrences: 2
18-
misspell:
19-
locale: US
20-
lll:
21-
line-length: 140
22-
goimports:
23-
local-prefixes: github.com/ChristopherHX/github-act-runner
24-
gocritic:
25-
enabled-tags:
26-
- diagnostic
27-
- experimental
28-
- opinionated
29-
- performance
30-
- style
31-
disabled-checks:
32-
- dupImport # https://github.com/go-critic/go-critic/issues/845
33-
- ifElseChain
34-
- octalLiteral
35-
- whyNoLint
36-
- wrapperFunc
37-
3+
go: "1.21"
384
linters:
395
enable:
6+
- asciicheck
7+
- bidichk
408
- bodyclose
41-
# - depguard # Disabled for now as it's too restrictive
9+
- copyloopvar
4210
- dogsled
4311
- dupl
44-
- errcheck
45-
- copyloopvar # replaces exportloopref for Go 1.22+
12+
- durationcheck
13+
- errname
14+
- errorlint
4615
- exhaustive
16+
- gocheckcompilerdirectives
4717
- goconst
4818
- gocritic
49-
- gofmt
50-
- goimports
51-
- revive # replaces golint
52-
- mnd # replaces gomnd
5319
- goprintffuncname
5420
- gosec
55-
- gosimple
56-
- govet
57-
- ineffassign
21+
- importas
5822
- lll
23+
- makezero
5924
- misspell
25+
- mnd
6026
- nakedret
27+
- nilerr
6128
- noctx
6229
- nolintlint
30+
- prealloc
31+
- revive
6332
- rowserrcheck
6433
- staticcheck
65-
- stylecheck
66-
- typecheck
34+
- tparallel
6735
- unconvert
6836
- unparam
69-
- unused
37+
- usestdlibvars
7038
- whitespace
71-
39+
settings:
40+
dupl:
41+
threshold: 100
42+
goconst:
43+
min-len: 2
44+
min-occurrences: 2
45+
gocritic:
46+
disabled-checks:
47+
- dupImport
48+
- ifElseChain
49+
- octalLiteral
50+
- whyNoLint
51+
- wrapperFunc
52+
enabled-tags:
53+
- diagnostic
54+
- experimental
55+
- opinionated
56+
- performance
57+
- style
58+
gocyclo:
59+
min-complexity: 15
60+
govet:
61+
enable:
62+
- shadow
63+
lll:
64+
line-length: 140
65+
misspell:
66+
locale: US
67+
revive:
68+
confidence: 0.8
69+
exclusions:
70+
generated: lax
71+
rules:
72+
- linters:
73+
- gocritic
74+
- gosec
75+
- mnd
76+
path: _test\.go
77+
- linters:
78+
- mnd
79+
path: main\.go
80+
- linters:
81+
- revive
82+
path: protocol/
83+
- linters:
84+
- gosec
85+
text: weak cryptographic primitive
86+
- linters:
87+
- gosec
88+
text: Use of weak random number generator
89+
- linters:
90+
- staticcheck
91+
text: at least one file in a package should have a package comment
92+
- linters:
93+
- staticcheck
94+
text: should have a package comment
95+
- linters:
96+
- revive
97+
text: 'package-comments: should have a package comment'
98+
- linters:
99+
- revive
100+
text: exported function .* should have comment or be unexported
101+
- linters:
102+
- revive
103+
text: exported method .* should have comment or be unexported
104+
- linters:
105+
- revive
106+
text: exported type .* should have comment or be unexported
107+
- linters:
108+
- revive
109+
text: exported var .* should have comment or be unexported
110+
paths:
111+
- third_party$
112+
- builtin$
113+
- examples$
72114
issues:
73-
exclude-rules:
74-
- path: _test\.go
75-
linters:
76-
- mnd
77-
- gocritic
78-
- gosec
79-
- path: main\.go
80-
linters:
81-
- mnd
82-
- path: protocol/
83-
linters:
84-
- revive # Protocol has many generated-like structs
85-
- text: "weak cryptographic primitive"
86-
linters:
87-
- gosec
88-
- text: "Use of weak random number generator"
89-
linters:
90-
- gosec
91-
- text: "at least one file in a package should have a package comment"
92-
linters:
93-
- stylecheck
94-
- text: "should have a package comment"
95-
linters:
96-
- stylecheck
97-
- text: "package-comments: should have a package comment"
98-
linters:
99-
- revive
100-
- text: "exported function .* should have comment or be unexported"
101-
linters:
102-
- revive
103-
- text: "exported method .* should have comment or be unexported"
104-
linters:
105-
- revive
106-
- text: "exported type .* should have comment or be unexported"
107-
linters:
108-
- revive
109-
- text: "exported var .* should have comment or be unexported"
110-
linters:
111-
- revive
112-
113-
exclude-use-default: false
114115
max-issues-per-linter: 0
115116
max-same-issues: 0
117+
formatters:
118+
enable:
119+
- gofmt
120+
- gofumpt
121+
- goimports
122+
settings:
123+
goimports:
124+
local-prefixes:
125+
- github.com/ChristopherHX/github-act-runner
126+
exclusions:
127+
generated: lax
128+
paths:
129+
- third_party$
130+
- builtin$
131+
- examples$

actionsdotnetactcompat/act_runner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ type ActRunner struct {
1010
}
1111

1212
func (arunner *ActRunner) ExecWorker(run *actionsrunner.RunRunner, wc actionsrunner.WorkerContext,
13-
jobreq *protocol.AgentJobRequestMessage, src []byte) error {
13+
jobreq *protocol.AgentJobRequestMessage, src []byte,
14+
) error {
1415
if len(arunner.WorkerArgs) == 0 {
1516
ExecWorker(jobreq, wc)
1617
return nil

actionsdotnetactcompat/act_worker.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ const (
3838
maxRedirects = 10
3939
jobTimeout = 5 * time.Minute
4040
// File permissions
41-
directoryPermissions = 0777
41+
directoryPermissions = 0o777
4242
// Stage constants
4343
mainStage = "Main"
44+
// Log prefix constants
45+
debugPrefix = "##[debug]"
46+
warnPrefix = "##[warn]"
47+
errorPrefix = "##[error]"
48+
panicPrefix = "##[panic]"
49+
fatalPrefix = "##[fatal]"
4450
)
4551

4652
type ghaFormatter struct {
@@ -54,11 +60,14 @@ type ghaFormatter struct {
5460
}
5561

5662
func flushInternal(rec *protocol.TimelineRecord, res *model.StepResult) {
57-
if res.Conclusion == model.StepStatusSuccess {
63+
switch res.Conclusion {
64+
case model.StepStatusSuccess:
5865
rec.Complete("Succeeded")
59-
} else if res.Conclusion == model.StepStatusSkipped {
66+
case model.StepStatusSkipped:
6067
rec.Complete("Skipped")
61-
} else {
68+
case model.StepStatusFailure:
69+
rec.Complete("Failed")
70+
default:
6271
rec.Complete("Failed")
6372
}
6473
}
@@ -176,12 +185,21 @@ func (f *ghaFormatter) Format(entry *logrus.Entry) ([]byte, error) {
176185
}
177186

178187
prefix := entry.Time.UTC().Format(protocol.TimestampOutputFormat) + " "
179-
if entry.Level == logrus.DebugLevel {
180-
prefix += "##[debug]"
181-
} else if entry.Level == logrus.WarnLevel {
182-
prefix += "##[warning]"
183-
} else if entry.Level == logrus.ErrorLevel {
184-
prefix += "##[error]"
188+
switch entry.Level {
189+
case logrus.DebugLevel:
190+
prefix += debugPrefix
191+
case logrus.WarnLevel:
192+
prefix += warnPrefix
193+
case logrus.ErrorLevel:
194+
prefix += errorPrefix
195+
case logrus.PanicLevel:
196+
prefix += panicPrefix
197+
case logrus.FatalLevel:
198+
prefix += fatalPrefix
199+
case logrus.InfoLevel:
200+
// No special prefix for info level
201+
case logrus.TraceLevel:
202+
prefix += debugPrefix
185203
}
186204
entry.Message = f.linefeedregex.ReplaceAllString(prefix+strings.Trim(entry.Message, "\r\n"), "\n"+prefix)
187205

@@ -639,7 +657,7 @@ func downloadAndExtractAction(
639657
if contextLogger != nil {
640658
contextLogger.Infof("Downloading action %v/%v (sha:%v) from %v", owner, name, resolvedSha, tarURL)
641659
}
642-
req, err := http.NewRequestWithContext(ctx, "GET", tarURL, http.NoBody)
660+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, tarURL, http.NoBody)
643661
if err != nil {
644662
return err
645663
}

actionsrunner/logger.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ type BasicLogger interface {
88
Printf(format string, a ...interface{})
99
}
1010

11-
type ConsoleLogger struct {
12-
}
11+
type ConsoleLogger struct{}
1312

1413
func (*ConsoleLogger) Printf(format string, a ...interface{}) {
1514
fmt.Printf(format, a...)

actionsrunner/runner.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ type RunnerEnvironment interface {
6161
ExecWorker(run *RunRunner, wc WorkerContext, jobreq *protocol.AgentJobRequestMessage, src []byte) error
6262
}
6363

64-
//nolint:revive // context-as-argument: API compatibility requirement - cannot change parameter order
6564
func (run *RunRunner) Run(runnerenv RunnerEnvironment, listenerctx, corectx context.Context) error {
6665
settings := run.Settings
6766
for i := 0; i < len(settings.Instances); i++ {
@@ -448,17 +447,16 @@ type RunnerJobRequestRef struct {
448447
RunServiceURL string `json:"run_service_url"`
449448
}
450449

451-
type plainTextFormatter struct {
452-
}
450+
type plainTextFormatter struct{}
453451

454452
func (f *plainTextFormatter) Format(entry *logrus.Entry) ([]byte, error) {
455453
return []byte(entry.Time.UTC().Format(protocol.TimestampOutputFormat) + " " + entry.Message + "\n"), nil
456454
}
457455

458-
//nolint:revive // context-as-argument: Legacy function signature for compatibility
459456
func runJob(runnerenv RunnerEnvironment, joblock *sync.Mutex, vssConnection *protocol.VssConnection, run *RunRunner,
460457
cancel context.CancelFunc, cancelJob context.CancelFunc, finishJob context.CancelFunc, jobExecCtx context.Context, jobctx context.Context,
461-
session *protocol.AgentMessageConnection, message protocol.TaskAgentMessage, instance *runnerconfiguration.RunnerInstance) {
458+
session *protocol.AgentMessageConnection, message protocol.TaskAgentMessage, instance *runnerconfiguration.RunnerInstance,
459+
) {
462460
go func() {
463461
plogger := &PrefixConsoleLogger{
464462
Parent: runnerenv,

actionsrunner/worker_runner_environment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const (
1616
messageIDSize = 4
1717
cancelRequestCmd = 2
1818
// File permissions
19-
filePermissions = 0664
19+
filePermissions = 0o664
2020
)
2121

2222
type WorkerRunnerEnvironment struct {
@@ -57,7 +57,7 @@ func (arunner *WorkerRunnerEnvironment) ExecWorker(
5757
return fmt.Errorf("missing WorkerArgs to execute an external worker")
5858
}
5959
//nolint:gosec // WorkerArgs are configured by the administrator, not user input
60-
worker := exec.Command(arunner.WorkerArgs[0], arunner.WorkerArgs[1:]...)
60+
worker := exec.CommandContext(jobExecCtx, arunner.WorkerArgs[0], arunner.WorkerArgs[1:]...)
6161
in, err := worker.StdinPipe()
6262
if err != nil {
6363
return err

common/io.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
const (
99
// File permissions
10-
filePermissions = 0664
10+
filePermissions = 0o664
1111
)
1212

1313
func WriteJSON(path string, value interface{}) error {

0 commit comments

Comments
 (0)