Skip to content

Commit b07dbef

Browse files
committed
Upgrade on 'init' command, support for golangci-lint v2, added binaries conflict check
1 parent 50f6c95 commit b07dbef

File tree

17 files changed

+388
-147
lines changed

17 files changed

+388
-147
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: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,118 @@
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+
gosec:
73+
severity: medium
74+
staticcheck:
75+
checks:
76+
- "all"
77+
- "-QF*"
78+
- "-S1039" # unnecessary use of fmt.Sprintf
79+
# assigning the result of this type assertion to a variable could eliminate type assertions in switch cases
80+
# TODO Tech debt to fix
81+
- "-S*"
82+
exclusions:
83+
generated: lax
84+
presets:
85+
- comments
86+
- common-false-positives
87+
- legacy
88+
- std-error-handling
89+
90+
issues:
91+
max-issues-per-linter: 0
92+
max-same-issues: 0
3693

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
4994

95+
# https://golangci-lint.run/usage/configuration/#severity-configuration
5096
severity:
5197
# - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity
5298
# - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity
5399
# - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
54-
default-severity: error
100+
default: "@linter"
55101
rules:
56102
- linters:
57103
- contextcheck
58104
- noctx
59105
- staticcheck
106+
- recvcheck
60107
severity: warning
61108
- linters:
62109
- errorlint
63110
- exhaustive
64111
- unused
65112
severity: info
66113

67-
issues:
68-
max-issues-per-linter: 0
69-
max-same-issues: 0
114+
# https://golangci-lint.run/usage/configuration/#output-configuration
115+
output:
116+
# path-prefix: "dist"
117+
formats:
118+
text:
119+
print-linter-name: true
120+
print-issued-lines: true
121+
colors: true
122+
sort-order:
123+
- linter
124+
- severity
125+
show-stats: true
70126

127+
# https://golangci-lint.run/usage/configuration/#formatters-configuration
128+
formatters:
129+
exclusions:
130+
generated: lax
71131

cmd/lanai-cli/initcmd/Makefile-Build.tmpl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,23 @@ endif
5757
# lint:
5858
# Invoke "go vet" and other linters
5959
lint:
60-
$(GO) vet ./... 2>&1 | tee $(DESTDIR)/go-vet-report.out
60+
{{- $linterVer := toolMajorVer "**/cmd/golangci-lint" }}
61+
{{- if eq $linterVer 1 }}
62+
$(GO) vet $(PKGS) 2>&1 | tee $(DESTDIR)/go-vet-report.out
6163
golangci-lint -c build/ci/golangci.yml \
62-
--timeout 10m \
63-
--out-format colored-line-number,checkstyle:$(DESTDIR)/golangci-lint-report.xml \
64-
--issues-exit-code 0 run ./...
64+
--timeout 10m \
65+
--out-format colored-line-number,checkstyle:$(DESTDIR)/golangci-lint-report.xml \
66+
--issues-exit-code 0 $(ARGS) run $(PKGS)
67+
{{ else if ge $linterVer 2 }}
68+
$(GO) vet $(PKGS) 2>&1 | tee $(DESTDIR)/go-vet-report.out
69+
golangci-lint -c build/ci/golangci.yml \
70+
--timeout 10m --issues-exit-code 0 \
71+
--output.text.path stdout \
72+
--output.checkstyle.path $(DESTDIR)/golangci-lint-report.xml \
73+
$(ARGS) run $(PKGS)
74+
{{- else }}
75+
echo '"golangci-lint" is not installed via "make init". "lint" skipped...
76+
{{- end }}
6577

6678
# build:
6779
# Generate executable binary and copy resources to $(DESTDIR)

cmd/lanai-cli/initcmd/Makefile-Libs.tmpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,26 @@ report: test
7272
# - ARGS="..."
7373
# e.g. ARGS="--issues-exit-code 0"
7474
# - PKGS="..." Override packages to lint. Default: ./...
75-
# e.g. PKGS="./pkg/... ./teset/..."
75+
# e.g. PKGS="./pkg/... ./test/..."
7676
lint: PKGS ?= "./..."
7777
lint:
78+
{{- $linterVer := toolMajorVer "**/cmd/golangci-lint" }}
79+
{{- if eq $linterVer 1 }}
7880
$(GO) vet $(PKGS) 2>&1 | tee $(DESTDIR)/go-vet-report.out
7981
golangci-lint -c build/ci/golangci.yml \
8082
--timeout 10m \
8183
--out-format colored-line-number,checkstyle:$(DESTDIR)/golangci-lint-report.xml \
8284
--issues-exit-code 0 $(ARGS) run $(PKGS)
85+
{{ else if ge $linterVer 2 }}
86+
$(GO) vet $(PKGS) 2>&1 | tee $(DESTDIR)/go-vet-report.out
87+
golangci-lint -c build/ci/golangci.yml \
88+
--timeout 10m --issues-exit-code 0 \
89+
--output.text.path stdout \
90+
--output.checkstyle.path $(DESTDIR)/golangci-lint-report.xml \
91+
$(ARGS) run $(PKGS)
92+
{{- else }}
93+
echo '"golangci-lint" is not installed via "make init". "lint" skipped...
94+
{{- end }}
8395

8496
# clean:
8597
# Undo previous "build". clean $(DESTDIR) and build cache

cmd/lanai-cli/initcmd/binaries.go

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,32 @@ package initcmd
1919
import (
2020
"context"
2121
"fmt"
22-
"os"
23-
22+
"github.com/bmatcuk/doublestar/v4"
2423
"github.com/cisco-open/go-lanai/cmd/lanai-cli/cmdutils"
24+
"os"
25+
"strings"
2526
)
2627

2728
var defaultBinaries = map[string]string{
28-
"github.com/axw/gocov/gocov": "v1.1.0",
29-
"github.com/AlekSi/gocov-xml": "v1.1.0",
30-
"gotest.tools/gotestsum": "v1.12.0",
31-
"github.com/golangci/golangci-lint/cmd/golangci-lint": "v1.64.8",
29+
"github.com/axw/gocov/gocov": "v1.1.0",
30+
"github.com/AlekSi/gocov-xml": "v1.1.0",
31+
"gotest.tools/gotestsum": "v1.12.0",
32+
//"github.com/golangci/golangci-lint/cmd/golangci-lint": "v1.64.8",
33+
"github.com/golangci/golangci-lint/v2/cmd/golangci-lint": "v2.2.2",
3234
}
3335

34-
func installBinaries(ctx context.Context) error {
35-
opts := []cmdutils.ShCmdOptions{cmdutils.ShellShowCmd(true), cmdutils.ShellUseWorkingDir(), cmdutils.ShellStdOut(os.Stdout)}
36+
var exclusiveBinaries = [][]string{
37+
{"github.com/golangci/golangci-lint/**"},
38+
}
3639

40+
func resolveBinariesFromCache(ctx context.Context) (map[string]string, error) {
41+
if binaries, ok := Module.Data[kDataBinaries].(map[string]string); ok {
42+
return binaries, nil
43+
}
44+
return resolveBinaries(ctx)
45+
}
46+
47+
func resolveBinaries(_ context.Context) (map[string]string, error) {
3748
binaries := make(map[string]string)
3849

3950
for p, v := range defaultBinaries {
@@ -53,11 +64,57 @@ func installBinaries(ctx context.Context) error {
5364
}
5465
}
5566

67+
if conflicts := findConflictingBinaries(binaries); len(conflicts) != 0 {
68+
for _, conflict := range conflicts {
69+
logger.Errorf("Detected conflicting binaries in Module.yml. Following tools should not be installed together:\n - %s", strings.Join(conflict, "\n "))
70+
}
71+
return nil, fmt.Errorf("detected %d group(s) of conflicting binaries. please check Module.yml", len(conflicts))
72+
}
73+
return binaries, nil
74+
}
75+
76+
func installBinaries(ctx context.Context) error {
77+
78+
opts := []cmdutils.ShCmdOptions{cmdutils.ShellShowCmd(true), cmdutils.ShellUseWorkingDir(), cmdutils.ShellStdOut(os.Stdout)}
79+
80+
binaries, e := resolveBinariesFromCache(ctx)
81+
if e != nil {
82+
return e
83+
}
84+
5685
for p, v := range binaries {
5786
installCmd := fmt.Sprintf("go install %s@%s", p, v)
5887
opts = append(opts, cmdutils.ShellCmd(installCmd))
5988
}
6089

61-
_, e := cmdutils.RunShellCommands(ctx, opts...)
90+
_, e = cmdutils.RunShellCommands(ctx, opts...)
6291
return e
6392
}
93+
94+
func findConflictingBinaries(binaries map[string]string) [][]string {
95+
var conflicts [][]string
96+
for _, patterns := range exclusiveBinaries {
97+
found := make([]string, 0, 2)
98+
for pkg := range binaries {
99+
if patternsMatchString(patterns, pkg) {
100+
found = append(found, pkg)
101+
}
102+
}
103+
if len(found) > 1 {
104+
conflicts = append(conflicts, found)
105+
}
106+
}
107+
if len(conflicts) > 0 {
108+
return conflicts
109+
}
110+
return nil
111+
}
112+
113+
func patternsMatchString(patterns []string, str string) bool {
114+
for _, pattern := range patterns {
115+
if ok, e := doublestar.Match(pattern, str); e == nil && ok {
116+
return true
117+
}
118+
}
119+
return false
120+
}

0 commit comments

Comments
 (0)