Skip to content

Commit ef31848

Browse files
committed
Address merge conflicts
Signed-off-by: Thomas Stromberg <[email protected]>
2 parents 2a6a466 + 9072441 commit ef31848

File tree

5 files changed

+86
-54
lines changed

5 files changed

+86
-54
lines changed

.golangci.yml

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ linters-settings:
1818
min-occurrences: 5
1919
ignore-tests: true
2020

21+
gosec:
22+
excludes:
23+
- G107 # Potential HTTP request made with variable url
24+
- G204 # Subprocess launched with function call as argument or cmd arguments
25+
- G404 # Use of weak random number generator (math/rand instead of crypto/rand
26+
2127
errorlint:
2228
# these are still common in Go: for instance, exit errors.
2329
asserts: false
@@ -108,6 +114,8 @@ linters:
108114
- gocritic
109115
- godot
110116
- gofmt
117+
- gofumpt
118+
- gosec
111119
- goheader
112120
- goimports
113121
- goprintffuncname
@@ -142,45 +150,45 @@ linters:
142150
- wastedassign
143151
- whitespace
144152

145-
# Disabled linters, due to being misaligned with Go practices
146-
# - exhaustivestruct
147-
# - gochecknoglobals
148-
# - gochecknoinits
149-
# - goconst
150-
# - godox
151-
# - goerr113
152-
# - gomnd
153-
# - lll
154-
# - nlreturn
155-
# - testpackage
156-
# - wsl
157-
# Disabled linters, due to not being relevant to our code base:
158-
# - maligned
159-
# - prealloc "For most programs usage of prealloc will be a premature optimization."
160-
# Disabled linters due to bad error messages or bugs
161-
# - gofumpt
162-
# - gosec
163-
# - tagliatelle
153+
# Disabled linters, due to being misaligned with Go practices
154+
# - exhaustivestruct
155+
# - gochecknoglobals
156+
# - gochecknoinits
157+
# - goconst
158+
# - godox
159+
# - goerr113
160+
# - gomnd
161+
# - lll
162+
# - nlreturn
163+
# - testpackage
164+
# - wsl
165+
# Disabled linters, due to not being relevant to our code base:
166+
# - maligned
167+
# - prealloc "For most programs usage of prealloc will be a premature optimization."
168+
# Disabled linters due to bad error messages or bugs
169+
# - tagliatelle
164170

165171
issues:
166172
# Excluding configuration per-path, per-linter, per-text and per-source
167173
exclude-rules:
168174
- path: _test\.go
169175
linters:
170-
- gocyclo
171-
- errcheck
172176
- dupl
177+
- errcheck
178+
- forcetypeassert
179+
- gocyclo
173180
- gosec
181+
- noctx
174182

175-
- path: cmd.*
183+
- path: .*cmd.*
176184
linters:
177185
- noctx
178186

179187
- path: main\.go
180188
linters:
181189
- noctx
182190

183-
- path: cmd.*
191+
- path: .*cmd.*
184192
text: "deep-exit"
185193

186194
- path: main\.go

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ We're so glad you're here.
99

1010
### Code of Conduct
1111

12-
Please read and understand the code of conduct found [here](https://github.com/tinkerbell/.github/blob/master/CODE_OF_CONDUCT.md).
12+
Please read and understand the code of conduct found [here](https://github.com/tinkerbell/.github/blob/main/CODE_OF_CONDUCT.md).
1313

1414
### DCO Sign Off
1515

Makefile

100755100644
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ SHELLCHECK_VERSION ?= v0.7.2
88
YAMLLINT_VERSION ?= 1.26.3
99
LINT_OS := $(shell uname)
1010
LINT_ARCH := $(shell uname -m)
11+
LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
1112

1213
# shellcheck and hadolint lack arm64 native binaries: rely on x86-64 emulation
1314
ifeq ($(LINT_OS),Darwin)
@@ -17,15 +18,19 @@ ifeq ($(LINT_OS),Darwin)
1718
endif
1819

1920
LINT_LOWER_OS = $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]')
20-
GOLINT_CONFIG = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))/.golangci.yml
21+
GOLINT_CONFIG = $(LINT_ROOT)/.golangci.yml
2122
YAMLLINT_ROOT = out/linters/yamllint-$(YAMLLINT_VERSION)
2223

2324
lint: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) $(YAMLLINT_ROOT)/bin/yamllint
2425
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run
25-
out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) -t info $(shell find . -name "*Dockerfile")
26+
out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) $(shell find . -name "*Dockerfile")
2627
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh")
2728
PYTHONPATH=$(YAMLLINT_ROOT)/lib $(YAMLLINT_ROOT)/bin/yamllint .
2829

30+
fix: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
31+
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run --fix
32+
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh") -f diff | git apply -p2 -
33+
2934
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck:
3035
mkdir -p out/linters
3136
curl -sSfL https://github.com/koalaman/shellcheck/releases/download/$(SHELLCHECK_VERSION)/shellcheck-$(SHELLCHECK_VERSION).$(LINT_LOWER_OS).$(LINT_ARCH).tar.xz | tar -C out/linters -xJf -
@@ -44,5 +49,5 @@ out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH):
4449
$(YAMLLINT_ROOT)/bin/yamllint:
4550
mkdir -p $(YAMLLINT_ROOT)/lib
4651
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
47-
cd $(YAMLLINT_ROOT) && PYTHONPATH=lib python setup.py install --prefix . --install-lib lib
52+
cd $(YAMLLINT_ROOT) && PYTHONPATH=lib python setup.py -q install --prefix . --install-lib lib
4853
# END: lint-install .

Makefile.tmpl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{{ if .YAML}}YAMLLINT_VERSION ?= 1.26.3{{ end }}
99
LINT_OS := $(shell uname)
1010
LINT_ARCH := $(shell uname -m)
11+
LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
1112

1213
# shellcheck and hadolint lack arm64 native binaries: rely on x86-64 emulation
1314
ifeq ($(LINT_OS),Darwin)
@@ -17,11 +18,15 @@ ifeq ($(LINT_OS),Darwin)
1718
endif
1819

1920
{{ if .Shell }}LINT_LOWER_OS = $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]'){{ end }}
20-
{{ if .Go }}GOLINT_CONFIG = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))/.golangci.yml{{ end }}
21+
{{ if .Go }}GOLINT_CONFIG = $(LINT_ROOT)/.golangci.yml{{ end }}
2122
{{ if .YAML }}YAMLLINT_ROOT = out/linters/yamllint-$(YAMLLINT_VERSION){{ end }}
2223

2324
lint: {{ if .Shell }}out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck {{ end }}{{ if .Dockerfile }}out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) {{ end }}{{ if .Go}}out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) {{ end }}{{ if .YAML}}$(YAMLLINT_ROOT)/bin/yamllint{{ end }}
24-
{{- range .Commands }}
25+
{{- range .LintCommands }}
26+
{{ .}}{{ end}}
27+
28+
fix: {{ if .Shell }}out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck {{ end }}{{ if .Go}}out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH){{ end }}
29+
{{- range .FixCommands }}
2530
{{ .}}{{ end}}
2631

2732
{{ if .Shell -}}
@@ -50,7 +55,7 @@ out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH):
5055
$(YAMLLINT_ROOT)/bin/yamllint:
5156
mkdir -p $(YAMLLINT_ROOT)/lib
5257
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
53-
cd $(YAMLLINT_ROOT) && PYTHONPATH=lib python setup.py install --prefix . --install-lib lib
58+
cd $(YAMLLINT_ROOT) && PYTHONPATH=lib python setup.py -q install --prefix . --install-lib lib
5459
{{ end -}}
5560

5661
# END: lint-install {{.Args}}

lint-install.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ const (
4141
)
4242

4343
type Config struct {
44-
Makefile string
45-
Args string
46-
Go string
47-
Dockerfile string
48-
Shell string
49-
YAML string
50-
Commands []string
44+
Makefile string
45+
Args string
46+
Go string
47+
Dockerfile string
48+
Shell string
49+
YAML string
50+
LintCommands []string
51+
FixCommands []string
5152
}
5253

5354
// applicableLinters returns a list of languages with known linters within a given directory.
@@ -126,14 +127,17 @@ func updateMakefile(root string, cfg Config, dryRun bool) (string, error) {
126127
proposed = append(proposed, newRules.Bytes()...)
127128
}
128129

129-
// trim any accidental trailing newlines
130+
// Trim extraneous newlines
131+
if len(existing) == 0 {
132+
proposed = bytes.TrimLeft(proposed, "\n")
133+
}
130134
proposed = bytes.TrimRight(proposed, "\n")
131135
proposed = append(proposed, byte('\n'))
132136

133137
edits := myers.ComputeEdits("Makefile", string(existing), string(proposed))
134138
change := gotextdiff.ToUnified(filepath.Base(dest), filepath.Base(dest), string(existing), edits)
135139
if !dryRun {
136-
if err := os.WriteFile(dest, proposed, 0755); err != nil {
140+
if err := os.WriteFile(dest, proposed, 0o600); err != nil {
137141
return "", err
138142
}
139143
}
@@ -159,7 +163,7 @@ func updateGoLint(root string, dryRun bool) (string, error) {
159163
change := gotextdiff.ToUnified(filepath.Base(dest), filepath.Base(dest), string(existing), edits)
160164

161165
if !dryRun {
162-
if err := os.WriteFile(dest, goLintConfig, 0755); err != nil {
166+
if err := os.WriteFile(dest, goLintConfig, 0o600); err != nil {
163167
return "", err
164168
}
165169
}
@@ -168,7 +172,7 @@ func updateGoLint(root string, dryRun bool) (string, error) {
168172
}
169173

170174
// goLintCmd returns the appropriate golangci-lint command to run for a project.
171-
func goLintCmd(root string, level string) string {
175+
func goLintCmd(root string, level string, fix bool) string {
172176
klog.Infof("Searching for go modules within %s ...", root)
173177
found := []string{}
174178

@@ -181,39 +185,47 @@ func goLintCmd(root string, level string) string {
181185
},
182186
Unsorted: true,
183187
})
184-
185188
if err != nil {
186189
klog.Errorf("unable to find go.mod files: %v", err)
187190
}
188191

189192
suffix := ""
190-
if level == "warn" {
193+
if fix {
194+
suffix = " --fix"
195+
} else if level == "warn" {
191196
suffix = " || true"
192197
}
193198

194-
if len(found) == 1 && found[0] == root {
199+
klog.Infof("found %d modules within %s: %s", len(found), root, found)
200+
if len(found) == 0 || (len(found) == 1 && found[0] == strings.Trim(root, "/")) {
195201
return fmt.Sprintf("out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run%s", suffix)
196202
}
197203

198-
return fmt.Sprintf(`find . -name go.mod | xargs -n1 dirname | xargs -n1 -I{} sh -c "cd {} && golangci-lint run -c $(GOLINT_CONFIG)"%s`, suffix)
204+
return fmt.Sprintf(`find . -name go.mod -execdir "$(LINT_ROOT)/out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)" run -c "$(GOLINT_CONFIG)"%s \;`, suffix)
199205
}
200206

201207
// shellLintCmd returns the appropriate shell lint command for a project.
202-
func shellLintCmd(_ string, level string) string {
208+
func shellLintCmd(_ string, level string, fix bool) string {
203209
suffix := ""
204-
if level == "warn" {
210+
211+
if fix {
212+
// patch(1) doesn't support patching from stdin on all platforms, so we use git apply instead
213+
suffix = " -f diff | git apply -p2 -"
214+
} else if level == "warn" {
205215
suffix = " || true"
206216
}
217+
207218
return fmt.Sprintf(`out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh")%s`, suffix)
208219
}
209220

210221
// dockerLintCmd returns the appropriate docker lint command for a project.
211222
func dockerLintCmd(_ string, level string) string {
212-
threshold := "info"
223+
f := ""
213224
if level == "warn" {
214-
threshold = "none"
225+
f = " --no-fail"
215226
}
216-
return fmt.Sprintf(`out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) -t %s $(shell find . -name "*Dockerfile")`, threshold)
227+
228+
return fmt.Sprintf(`out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH)%s $(shell find . -name "*Dockerfile")`, f)
217229
}
218230

219231
// yamlLintCmd returns the appropriate yamllint command for a project.
@@ -262,19 +274,21 @@ func main() {
262274

263275
if needs[Go] {
264276
cfg.Go = *goFlag
265-
cfg.Commands = append(cfg.Commands, goLintCmd(root, cfg.Go))
277+
cfg.LintCommands = append(cfg.LintCommands, goLintCmd(root, cfg.Go, false))
278+
cfg.FixCommands = append(cfg.FixCommands, goLintCmd(root, cfg.Go, true))
266279
}
267280
if needs[Dockerfile] {
268281
cfg.Dockerfile = *dockerfileFlag
269-
cfg.Commands = append(cfg.Commands, dockerLintCmd(root, cfg.Dockerfile))
282+
cfg.LintCommands = append(cfg.LintCommands, dockerLintCmd(root, cfg.Dockerfile))
270283
}
271284
if needs[Shell] {
272285
cfg.Shell = *shellFlag
273-
cfg.Commands = append(cfg.Commands, shellLintCmd(root, cfg.Shell))
286+
cfg.LintCommands = append(cfg.LintCommands, shellLintCmd(root, cfg.Shell, false))
287+
cfg.FixCommands = append(cfg.FixCommands, shellLintCmd(root, cfg.Shell, true))
274288
}
275289
if needs[YAML] {
276290
cfg.YAML = *yamlFlag
277-
cfg.Commands = append(cfg.Commands, yamlLintCmd(root, cfg.Shell))
291+
cfg.LintCommands = append(cfg.LintCommands, yamlLintCmd(root, cfg.Shell))
278292
}
279293

280294
diff, err := updateMakefile(root, cfg, *dryRunFlag)

0 commit comments

Comments
 (0)