Skip to content

Commit a6e73f2

Browse files
authored
Merge pull request #436 from cisco-open/feat-upgrade-202506
Maintenance update 2025-06
2 parents 87e9352 + 2755e7e commit a6e73f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1201
-353
lines changed

Module.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ generates:
1818
- path: "pkg/swagger"
1919

2020
binaries:
21+
- package: github.com/golangci/golangci-lint/v2/cmd/golangci-lint
22+
version: "v2.2.2"
2123
- package: github.com/golangci/golangci-lint/cmd/golangci-lint
22-
version: v1.59.1
24+
version: ""

build/ci/golangci-v1.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2023 Cisco Systems, Inc. and its affiliates
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
# Deprecated. This file is kept for reference. Will be removed in the future
18+
# Ref: https://golangci-lint.run/usage/configuration/
19+
run:
20+
go: '1.22'
21+
tests: false
22+
modules-download-mode: readonly
23+
24+
output:
25+
sort-results: true
26+
27+
# https://golangci-lint.run/usage/linters/
28+
linters:
29+
# enable-all: true
30+
# disable-all: true
31+
presets:
32+
- bugs
33+
# enable:
34+
# - asciicheck
35+
# - ...
36+
disable:
37+
38+
linters-settings:
39+
# https://golangci-lint.run/usage/linters/#exhaustive:
40+
exhaustive:
41+
default-signifies-exhaustive: true
42+
# https://golangci-lint.run/usage/linters/#errorlint
43+
errorlint:
44+
# we don't enforce to use %w. devs should do it using their own judgement
45+
errorf: false
46+
# error constants is still very common in many libraries. e.g. context.Cancelled
47+
# So we allow plain error switching for now.
48+
# TODO we should revise this setting when patterns are more consistent within golang communities
49+
comparison: false
50+
51+
severity:
52+
# - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity
53+
# - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity
54+
# - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
55+
default-severity: error
56+
rules:
57+
- linters:
58+
- contextcheck
59+
- noctx
60+
- staticcheck
61+
severity: warning
62+
- linters:
63+
- errorlint
64+
- exhaustive
65+
- unused
66+
severity: info
67+
68+
issues:
69+
max-issues-per-linter: 0
70+
max-same-issues: 0
71+
72+

build/ci/golangci.yml

Lines changed: 103 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,130 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17+
# yaml-language-server: $schema=https://golangci-lint.run/jsonschema/golangci.jsonschema.json
18+
1719
# Ref: https://golangci-lint.run/usage/configuration/
20+
version: "2"
1821
run:
19-
go: '1.22'
20-
tests: false
22+
relative-path-mode: gomod
2123
modules-download-mode: readonly
24+
allow-parallel-runners: true
25+
tests: false
2226

23-
output:
24-
sort-results: true
25-
26-
# https://golangci-lint.run/usage/linters/
27+
# https://golangci-lint.run/usage/configuration/#linters-configuration
2728
linters:
28-
# enable-all: true
29-
# disable-all: true
30-
presets:
31-
- bugs
32-
# enable:
33-
# - asciicheck
34-
# - ...
35-
disable:
29+
# https://golangci-lint.run/usage/linters/
30+
default: none
31+
enable:
32+
# Default ones
33+
- errcheck
34+
- govet
35+
- ineffassign
36+
- staticcheck
37+
# - unused
38+
# Previously "bugs" preset
39+
- asasalint
40+
- asciicheck
41+
- bidichk
42+
- bodyclose
43+
- contextcheck
44+
- durationcheck
45+
- errchkjson
46+
- errorlint
47+
- exhaustive
48+
- gocheckcompilerdirectives
49+
- gochecksumtype
50+
- gosec
51+
- gosmopolitan
52+
- loggercheck
53+
- makezero
54+
- musttag
55+
- nilerr
56+
- nilnesserr
57+
- noctx
58+
- protogetter
59+
- reassign
60+
- recvcheck
61+
- rowserrcheck
62+
- spancheck
63+
- sqlclosecheck
64+
- testifylint
65+
- zerologlint
66+
settings:
67+
errorlint:
68+
errorf: false
69+
comparison: false
70+
exhaustive:
71+
default-signifies-exhaustive: true
72+
recvcheck:
73+
disable-builtin: false
74+
exclusions:
75+
- "*.UnmarshalText"
76+
- "*.UnmarshalJSON"
77+
- "*.UnmarshalYAML"
78+
- "*.UnmarshalXML"
79+
- "*.UnmarshalBinary"
80+
- "*.GobDecode"
81+
- "*.String"
82+
- "*.Scan"
83+
staticcheck:
84+
checks:
85+
- "all"
86+
# Following are too verbose. Re-enable if we could control output based on severities
87+
- "-QF*" # Quickfixes
88+
- "-ST1*" # Stylistic issues.
89+
# TODO Tech debt to fix
90+
- "-S*" # Any other code simplication issues. Too many places (~100)
91+
- "-S1034" # "could eliminate this type assertion". Too many places (~100)
92+
- "-SA1019" # "Using a deprecated function, variable, constant or field". Re-enable when OPA is migrated
93+
- "-SA1006" # "printf-style function without additional arguments". We probably won't fix these
94+
exclusions:
95+
generated: lax
96+
presets:
97+
- comments
98+
- common-false-positives
99+
- legacy
100+
- std-error-handling
36101

37-
linters-settings:
38-
# https://golangci-lint.run/usage/linters/#exhaustive:
39-
exhaustive:
40-
default-signifies-exhaustive: true
41-
# https://golangci-lint.run/usage/linters/#errorlint
42-
errorlint:
43-
# we don't enforce to use %w. devs should do it using their own judgement
44-
errorf: false
45-
# error constants is still very common in many libraries. e.g. context.Cancelled
46-
# So we allow plain error switching for now.
47-
# TODO we should revise this setting when patterns are more consistent within golang communities
48-
comparison: false
102+
issues:
103+
max-issues-per-linter: 0
104+
max-same-issues: 0
49105

106+
107+
# https://golangci-lint.run/usage/configuration/#severity-configuration
50108
severity:
51109
# - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity
52110
# - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity
53111
# - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
54-
default-severity: error
112+
default: "@linter"
55113
rules:
56114
- linters:
57115
- contextcheck
58116
- noctx
59-
- staticcheck
117+
- recvcheck
60118
severity: warning
61119
- linters:
62120
- errorlint
63121
- exhaustive
122+
- staticcheck
64123
- unused
65124
severity: info
66125

67-
issues:
68-
max-issues-per-linter: 0
69-
max-same-issues: 0
126+
# https://golangci-lint.run/usage/configuration/#output-configuration
127+
output:
128+
# path-prefix: "dist"
129+
formats:
130+
text:
131+
print-linter-name: true
132+
print-issued-lines: true
133+
colors: true
134+
sort-order:
135+
- linter
136+
- severity
137+
show-stats: true
70138

139+
# https://golangci-lint.run/usage/configuration/#formatters-configuration
140+
formatters:
141+
exclusions:
142+
generated: lax
71143

cmd/lanai-cli/cmdtest/cmd.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package cmdtest
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/cisco-open/go-lanai/cmd/lanai-cli/cmdutils"
7+
"github.com/cisco-open/go-lanai/test"
8+
"github.com/spf13/cobra"
9+
"os"
10+
"path/filepath"
11+
"testing"
12+
)
13+
14+
const (
15+
TestTmpDir = ".tmp"
16+
TestOutputDir = "output"
17+
)
18+
19+
var (
20+
testRootCmd = &cobra.Command{
21+
Use: "lanai-cli-test",
22+
Short: "lanai-cli for test",
23+
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
24+
PersistentPreRunE: cmdutils.MergeRunE(
25+
cmdutils.EnsureGlobalDirectories(),
26+
cmdutils.PrintEnvironment(),
27+
),
28+
}
29+
)
30+
31+
func init() {
32+
cmdutils.PersistentFlags(testRootCmd, &cmdutils.GlobalArgs)
33+
}
34+
35+
func SetupResetPackageVars() test.SetupFunc {
36+
return func(ctx context.Context, t *testing.T) (context.Context, error) {
37+
cmdutils.ResetGoCmd()
38+
return ctx, nil
39+
}
40+
}
41+
42+
// DryRunCobraCommand Run given cobra.Command in given work dir relative to "testdata"
43+
func DryRunCobraCommand(ctx context.Context, wd string, cmd *cobra.Command, handler TestDryRunHandler, args ...string) error {
44+
// prepare global args
45+
originalArgs := cmdutils.GlobalArgs
46+
defer func() { cmdutils.GlobalArgs = originalArgs }()
47+
48+
cmdutils.GlobalArgs = cmdutils.Global{
49+
WorkingDir: PathRelativeToTestdata(wd),
50+
TmpDir: PathRelativeToTestdata(wd, TestTmpDir),
51+
OutputDir: PathRelativeToTestdata(wd, TestOutputDir),
52+
Verbose: true,
53+
DryRun: true,
54+
}
55+
56+
// setup dry run handler
57+
if handler != nil {
58+
cmdutils.GlobalArgs.DryRunFunc = handler.Handle
59+
} else {
60+
cmdutils.GlobalArgs.DryRunFunc = originalArgs.DryRunFunc
61+
}
62+
63+
// clean up FS
64+
if e := os.RemoveAll(cmdutils.GlobalArgs.OutputDir); e != nil {
65+
return fmt.Errorf("unable to start command with clean FS: %w", e)
66+
}
67+
68+
// run command
69+
if len(args) == 0 {
70+
args = []string{}
71+
}
72+
cmdCopy, _ := CopyCommandChain(cmd, args...)
73+
return cmdCopy.ExecuteContext(ctx)
74+
}
75+
76+
// CopyCommandChain do following things:
77+
// - Make copy of given command and its parents/ancestors.
78+
// - Fix arguments of its ancestors.
79+
// - Attach the given command chain to a copy of predefined test root command (to mimic how main() function works)
80+
// This function returns copied command and a copy of its root command. Two values may be same if they are the root
81+
func CopyCommandChain(cmd *cobra.Command, args...string) (cmdCpy, rootCpy *cobra.Command) {
82+
cmdCpy = copyCmd(cmd)
83+
var cpy, prev *cobra.Command
84+
for cpy, prev = cmdCpy, nil; cpy != nil; cpy = copyCmd(cpy.Parent()) {
85+
if prev != nil {
86+
args = append([]string{prev.Name()}, args...)
87+
cpy.ResetCommands()
88+
cpy.AddCommand(prev)
89+
}
90+
cpy.SetArgs(args)
91+
prev = cpy
92+
}
93+
rootCpy = copyCmd(testRootCmd)
94+
if prev != nil {
95+
args = append([]string{prev.Name()}, args...)
96+
rootCpy.ResetCommands()
97+
rootCpy.AddCommand(prev)
98+
}
99+
rootCpy.SetArgs(args)
100+
return
101+
}
102+
103+
func copyCmd(cmd *cobra.Command) *cobra.Command {
104+
if cmd == nil {
105+
return nil
106+
}
107+
vCopy := *cmd
108+
return &vCopy
109+
}
110+
111+
func PathRelativeToTestdata(pathComponents ...string) string {
112+
if base, e := os.Getwd(); e != nil {
113+
pathComponents = append([]string{"testdata"}, pathComponents...)
114+
} else {
115+
pathComponents = append([]string{base, "testdata"}, pathComponents...)
116+
}
117+
return filepath.Join(pathComponents...)
118+
}

0 commit comments

Comments
 (0)