Skip to content

Commit d4fc13a

Browse files
committed
fix another 39 errors by upgrading golangci-lint
Signed-off-by: Leslie Qi Wang <qiwa@pensando.io>
1 parent 62fc68f commit d4fc13a

12 files changed

Lines changed: 49 additions & 38 deletions

File tree

.golangci.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ run:
44

55
linters-settings:
66
govet:
7-
check-shadowing: true
7+
enable:
8+
- shadow
89
revive:
910
min-confidence: 0.8
1011
gocyclo:
@@ -41,14 +42,14 @@ linters:
4142
- dogsled
4243
- dupl
4344
- errcheck
44-
- exportloopref
45+
- copyloopvar # replaces exportloopref for Go 1.22+
4546
- exhaustive
4647
- goconst
4748
- gocritic
4849
- gofmt
4950
- goimports
5051
- revive # replaces golint
51-
- gomnd
52+
- mnd # replaces gomnd
5253
- goprintffuncname
5354
- gosec
5455
- gosimple
@@ -72,12 +73,12 @@ issues:
7273
exclude-rules:
7374
- path: _test\.go
7475
linters:
75-
- gomnd
76+
- mnd
7677
- gocritic
7778
- gosec
7879
- path: main\.go
7980
linters:
80-
- gomnd
81+
- mnd
8182
- path: protocol/
8283
linters:
8384
- revive # Protocol has many generated-like structs

actionsdotnetactcompat/act_worker.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"fmt"
99
"io"
10+
"math"
1011
"net/http"
1112
"os"
1213
"path/filepath"
@@ -163,7 +164,7 @@ func (f *ghaFormatter) Format(entry *logrus.Entry) ([]byte, error) {
163164
}
164165
if f.rqt.Variables != nil {
165166
for _, v := range f.rqt.Variables {
166-
if v.IsSecret && len(v.Value) > 0 && !strings.EqualFold(v.Value, "true") &&
167+
if v.IsSecret && v.Value != "" && !strings.EqualFold(v.Value, "true") &&
167168
!strings.EqualFold(v.Value, "false") && !strings.EqualFold(v.Value, "0") && !strings.EqualFold(v.Value, "1") {
168169
entry.Message = strings.ReplaceAll(entry.Message, v.Value, "***")
169170
}
@@ -260,17 +261,17 @@ func ExecWorker(rqt *protocol.AgentJobRequestMessage, wc actionsrunner.WorkerCon
260261
env["ACTIONS_RUNTIME_URL"] = vssConnection.TenantURL
261262
env["ACTIONS_RUNTIME_TOKEN"] = vssConnection.Token
262263

263-
if cacheURL, cacheOk := vssConnectionData["CacheServerUrl"]; cacheOk && len(cacheURL) > 0 {
264+
if cacheURL, cacheOk := vssConnectionData["CacheServerUrl"]; cacheOk && cacheURL != "" {
264265
env["ACTIONS_CACHE_URL"] = cacheURL
265266
}
266-
if idTokenURL, idTokenOk := vssConnectionData["GenerateIdTokenUrl"]; idTokenOk && len(idTokenURL) > 0 {
267+
if idTokenURL, idTokenOk := vssConnectionData["GenerateIdTokenUrl"]; idTokenOk && idTokenURL != "" {
267268
env["ACTIONS_ID_TOKEN_REQUEST_URL"] = idTokenURL
268269
env["ACTIONS_ID_TOKEN_REQUEST_TOKEN"] = vssConnection.Token
269270
}
270-
if resultsServiceURL, resultsOk := vssConnectionData["ResultsServiceUrl"]; resultsOk && len(resultsServiceURL) > 0 {
271+
if resultsServiceURL, resultsOk := vssConnectionData["ResultsServiceUrl"]; resultsOk && resultsServiceURL != "" {
271272
env["ACTIONS_RESULTS_URL"] = resultsServiceURL
272273
}
273-
if pipelinesServiceURL, pipelinesOk := vssConnectionData["PipelinesServiceUrl"]; pipelinesOk && len(pipelinesServiceURL) > 0 {
274+
if pipelinesServiceURL, pipelinesOk := vssConnectionData["PipelinesServiceUrl"]; pipelinesOk && pipelinesServiceURL != "" {
274275
env["ACTIONS_RUNTIME_URL"] = pipelinesServiceURL
275276
}
276277
if usesCacheServiceV2, cacheV2Ok := rqt.Variables["actions_uses_cache_service_v2"]; cacheV2Ok &&
@@ -542,7 +543,12 @@ func ExecWorker(rqt *protocol.AgentJobRequestMessage, wc actionsrunner.WorkerCon
542543
if canEvaluateNow {
543544
rec.Name = eval.Interpolate(jobExecCtx, rec.Name)
544545
}
545-
jlogger.Append(&rec).Order = int32(i + len(steps) + 1)
546+
// Check for integer overflow before conversion
547+
order := i + len(steps) + 1
548+
if order > math.MaxInt32 { // int32 max value
549+
order = math.MaxInt32
550+
}
551+
jlogger.Append(&rec).Order = int32(order) //nolint:gosec // bounds checked above
546552
}
547553

548554
logrus.SetLevel(actLogger.GetLevel())

actionsdotnetactcompat/step_converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func ConvertSteps(jobSteps []protocol.ActionStep) ([]*model.Step, error) {
121121
uses = step.Reference.Path
122122
} else {
123123
uses = step.Reference.Name
124-
if len(step.Reference.Path) > 0 {
124+
if step.Reference.Path != "" {
125125
uses = uses + "/" + step.Reference.Path
126126
}
127127
uses = uses + "@" + step.Reference.Ref

actionsrunner/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ func runJob(runnerenv RunnerEnvironment, joblock *sync.Mutex, vssConnection *pro
605605

606606
logger.Log(logrus.InfoLevel, "Runner Name: "+instance.Agent.Name)
607607
logger.Log(logrus.InfoLevel, "Runner OSDescription: github-act-runner "+runtime.GOOS+"/"+runtime.GOARCH)
608-
if len(run.Version) > 0 {
608+
if run.Version != "" {
609609
logger.Log(logrus.InfoLevel, "Runner Version: "+run.Version)
610610
}
611611

actionsrunner/worker_runner_environment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (arunner *WorkerRunnerEnvironment) ExecWorker(
8383
if err != nil {
8484
fmt.Printf("failed to write new job: %s", err)
8585
}
86-
binary.BigEndian.PutUint32(mid, uint32(len(src)))
86+
binary.BigEndian.PutUint32(mid, uint32(len(src))) //nolint:gosec
8787
_, err = in.Write(mid)
8888
if err != nil {
8989
fmt.Printf("failed to write new job: %s", err)
@@ -102,7 +102,7 @@ func (arunner *WorkerRunnerEnvironment) ExecWorker(
102102
if err != nil {
103103
fmt.Printf("failed to write cancel job: %s", err)
104104
}
105-
binary.BigEndian.PutUint32(mid, uint32(len(src)))
105+
binary.BigEndian.PutUint32(mid, uint32(len(src))) //nolint:gosec
106106
_, err = in.Write(mid)
107107
if err != nil {
108108
fmt.Printf("failed to write cancel job length: %s", err)

main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func main() {
255255
Use: "configure",
256256
Short: "Configure your self-hosted runner",
257257
Args: cobra.MaximumNArgs(0),
258-
Run: func(cmd *cobra.Command, args []string) {
258+
Run: func(_ *cobra.Command, _ []string) {
259259
config.ReadFromEnvironment()
260260
settings := &runnerconfiguration.RunnerSettings{}
261261
if !printJITConfig {
@@ -322,7 +322,7 @@ func main() {
322322
Use: "run",
323323
Short: "Run your self-hosted runner",
324324
Args: cobra.MaximumNArgs(0),
325-
Run: func(cmd *cobra.Command, args []string) {
325+
Run: func(_ *cobra.Command, _ []string) {
326326
os.Exit(run.Run())
327327
},
328328
}
@@ -339,7 +339,7 @@ func main() {
339339
Use: "remove",
340340
Short: "Remove your self-hosted runner",
341341
Args: cobra.MaximumNArgs(0),
342-
Run: func(cmd *cobra.Command, args []string) {
342+
Run: func(_ *cobra.Command, _ []string) {
343343
remove.ReadFromEnvironment()
344344
var settings *runnerconfiguration.RunnerSettings
345345
var err error
@@ -396,7 +396,7 @@ func main() {
396396
Use: "worker",
397397
Short: "Run as self-hosted runner worker, can be used to create ephemeral worker without exposing other job requests",
398398
Args: cobra.MaximumNArgs(0),
399-
Run: func(cmd *cobra.Command, args []string) {
399+
Run: func(_ *cobra.Command, _ []string) {
400400
ccontext, cancelccontext := context.WithCancel(context.Background())
401401
go func() {
402402
execcontext, cancelExec := context.WithCancel(context.Background())
@@ -459,7 +459,7 @@ func main() {
459459
svcRun := &cobra.Command{
460460
Use: "run",
461461
Short: "Used as service entrypoint",
462-
RunE: func(cmd *cobra.Command, args []string) error {
462+
RunE: func(_ *cobra.Command, _ []string) error {
463463
err := os.Chdir(wd)
464464
if err != nil {
465465
return err
@@ -492,7 +492,7 @@ func main() {
492492
svcInstall := &cobra.Command{
493493
Use: "install",
494494
Short: "Install the service may require admin privileges",
495-
RunE: func(cmd *cobra.Command, args []string) error {
495+
RunE: func(_ *cobra.Command, _ []string) error {
496496
svc, err := service.New(&RunRunnerSvc{}, getSvcConfig(wd, envFile))
497497

498498
if err != nil {
@@ -510,7 +510,7 @@ func main() {
510510
svcUninstall := &cobra.Command{
511511
Use: "uninstall",
512512
Short: "Uninstall the service may require admin privileges",
513-
RunE: func(cmd *cobra.Command, args []string) error {
513+
RunE: func(_ *cobra.Command, _ []string) error {
514514
svc, err := service.New(&RunRunnerSvc{}, getSvcConfig(wd, envFile))
515515

516516
if err != nil {
@@ -522,7 +522,7 @@ func main() {
522522
svcStart := &cobra.Command{
523523
Use: "start",
524524
Short: "Start the service may require admin privileges",
525-
RunE: func(cmd *cobra.Command, args []string) error {
525+
RunE: func(_ *cobra.Command, _ []string) error {
526526
svc, err := service.New(&RunRunnerSvc{}, getSvcConfig(wd, envFile))
527527

528528
if err != nil {
@@ -534,7 +534,7 @@ func main() {
534534
svcStop := &cobra.Command{
535535
Use: "stop",
536536
Short: "Stop the service may require admin privileges",
537-
RunE: func(cmd *cobra.Command, args []string) error {
537+
RunE: func(_ *cobra.Command, _ []string) error {
538538
svc, err := service.New(&RunRunnerSvc{}, getSvcConfig(wd, envFile))
539539

540540
if err != nil {

protocol/connection.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (vssConnection *VssConnection) requestWithContextNoAuth(
216216
if err != nil {
217217
return 0, err
218218
}
219-
if len(apiversion) > 0 {
219+
if apiversion != "" {
220220
// vssservice always needs a version, even if there is no content
221221
if parsedURL, parseErr := url.Parse(requesturl); parseErr == nil {
222222
query := parsedURL.Query()
@@ -242,7 +242,7 @@ func (vssConnection *VssConnection) requestWithContextNoAuth(
242242
header.Set(acceptHeader, "application/json")
243243
}
244244
}
245-
if len(apiversion) > 0 {
245+
if apiversion != "" {
246246
// vssservice does only accept contenttype in a single line
247247
if len(header[contentTypeHeader]) > 0 {
248248
header[contentTypeHeader][0] += apiVersionSuffix + apiversion
@@ -254,9 +254,9 @@ func (vssConnection *VssConnection) requestWithContextNoAuth(
254254
header["X-TFS-FedAuthRedirect"] = []string{"Suppress"}
255255
header["X-TFS-Session"] = []string{uuid.NewString()}
256256
}
257-
if len(vssConnection.Token) > 0 {
257+
if vssConnection.Token != "" {
258258
header["Authorization"] = []string{"bearer " + vssConnection.Token}
259-
} else if len(vssConnection.AuthHeader) > 0 {
259+
} else if vssConnection.AuthHeader != "" {
260260
header["Authorization"] = []string{vssConnection.AuthHeader}
261261
}
262262
if vssConnection.Trace {

protocol/logger/job_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (logger *WebsocketLiveloggerWithFallback) InitializeVssLogger() {
113113

114114
func (logger *WebsocketLiveloggerWithFallback) Initialize() {
115115
_ = logger.Close() // Ignore error for cleanup
116-
if len(logger.FeedStreamURL) > 0 {
116+
if logger.FeedStreamURL != "" {
117117
wslogger := &WebsocketLivelogger{
118118
JobRequest: logger.JobRequest,
119119
Connection: logger.Connection,

protocol/template_token.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package protocol
33
import (
44
"encoding/json"
55
"fmt"
6+
"math"
67
"regexp"
78
"strings"
89

@@ -407,10 +408,13 @@ func (converter *TemplateTokenConverter) ToYamlNode(token *TemplateToken) (ret *
407408
func (converter *TemplateTokenConverter) FromYamlNode(node *yaml.Node) (ret *TemplateToken, err error) {
408409
defer func() {
409410
if ret != nil && (node.Column != 0 || node.Line != 0) {
410-
column := int32(node.Column)
411-
line := int32(node.Line)
412-
ret.Column = &column
413-
ret.Line = &line
411+
// Check for integer overflow before conversion
412+
if node.Column <= math.MaxInt32 && node.Line <= math.MaxInt32 && node.Column >= math.MinInt32 && node.Line >= math.MinInt32 {
413+
column := int32(node.Column) //nolint:gosec // bounds checked above
414+
line := int32(node.Line) //nolint:gosec // bounds checked above
415+
ret.Column = &column
416+
ret.Line = &line
417+
}
414418
}
415419
}()
416420
retNil := func() *TemplateToken {

runnerconfiguration/add.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (config *ConfigureRunner) Configure(
9292
if len(taskAgentPools) == 0 {
9393
return nil, fmt.Errorf("failed to configure runner, no self-hosted runner group available")
9494
}
95-
if len(config.RunnerGroup) > 0 {
95+
if config.RunnerGroup != "" {
9696
taskAgentPool = config.RunnerGroup
9797
} else {
9898
taskAgentPool = taskAgentPools[0]
@@ -115,7 +115,7 @@ func (config *ConfigureRunner) Configure(
115115

116116
taskAgent := &protocol.TaskAgent{}
117117
bs := make([]byte, rsaExponentBytes)
118-
ui := uint32(key.E)
118+
ui := uint32(key.E) //nolint:gosec
119119
binary.BigEndian.PutUint32(bs, ui)
120120
expof := 0
121121
for ; expof < 3 && bs[expof] == 0; expof++ { //nolint:revive // empty-block: intentionally empty loop to find non-zero bytes
@@ -136,7 +136,7 @@ func (config *ConfigureRunner) Configure(
136136
}
137137
}
138138
if !config.Unattended && len(config.Labels) == 0 {
139-
if res := survey.GetInput("Please enter custom labels of your new runner (case insensitive, separated by ','):", ""); len(res) > 0 {
139+
if res := survey.GetInput("Please enter custom labels of your new runner (case insensitive, separated by ','):", ""); res != "" {
140140
config.Labels = strings.Split(res, ",")
141141
}
142142
}

0 commit comments

Comments
 (0)