Skip to content

Commit 57fe15b

Browse files
committed
Merge branch 'main' into py3
2 parents b1aec32 + a3f3261 commit 57fe15b

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13-
out/
1413

1514
# Dependency directories (remove the comment below to include it)
1615
# vendor/
16+
17+
# added by lint-install
18+
out/

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ LINT_LOWER_OS = $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]')
2121
GOLINT_CONFIG = $(LINT_ROOT)/.golangci.yml
2222
YAMLLINT_ROOT = out/linters/yamllint-$(YAMLLINT_VERSION)
2323

24+
.PHONY: lint
2425
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
2526
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run
2627
out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) $(shell find . -name "*Dockerfile")
2728
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh")
2829
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
2930

31+
.PHONY: fix
3032
fix: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
3133
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run --fix
3234
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh") -f diff | git apply -p2 -

Makefile.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ endif
2121
{{ if .Go }}GOLINT_CONFIG = $(LINT_ROOT)/.golangci.yml{{ end }}
2222
{{ if .YAML }}YAMLLINT_ROOT = out/linters/yamllint-$(YAMLLINT_VERSION){{ end }}
2323

24+
.PHONY: lint
2425
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 }}
2526
{{- range .LintCommands }}
2627
{{ .}}{{ end}}
2728

29+
.PHONY: fix
2830
fix: {{ if .Shell }}out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck {{ end }}{{ if .Go}}out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH){{ end }}
2931
{{- range .FixCommands }}
3032
{{ .}}{{ end}}

lint-install.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,47 @@ func updateGoLint(root string, dryRun bool) (string, error) {
171171
return fmt.Sprint(change), nil
172172
}
173173

174+
// updateGitignore updates the .gitignore file within a project to exclude installed linters.
175+
func updateGitignore(root string, dryRun bool) (string, error) {
176+
dest := filepath.Join(root, ".gitignore")
177+
var existing []byte
178+
var err error
179+
180+
if _, err = os.Stat(dest); err == nil {
181+
klog.Infof("Found existing %s", dest)
182+
existing, err = os.ReadFile(dest)
183+
if err != nil {
184+
return "", err
185+
}
186+
}
187+
proposed := []byte{}
188+
found := false
189+
for _, line := range bytes.Split(existing, []byte("\n")) {
190+
if bytes.Equal(line, []byte("out/")) || bytes.Equal(line, []byte("out")) {
191+
found = true
192+
}
193+
proposed = append(proposed, line...)
194+
proposed = append(proposed, []byte("\n")...)
195+
}
196+
197+
if !found {
198+
proposed = append(proposed, []byte("# added by lint-install\nout/\n")...)
199+
}
200+
201+
// Trim extra newlines at the end
202+
proposed = bytes.TrimRight(proposed, "\n")
203+
proposed = append(proposed, byte('\n'))
204+
205+
edits := myers.ComputeEdits(".gitignore", string(existing), string(proposed))
206+
change := gotextdiff.ToUnified(filepath.Base(dest), filepath.Base(dest), string(existing), edits)
207+
if !dryRun {
208+
if err := os.WriteFile(dest, proposed, 0o600); err != nil {
209+
return "", err
210+
}
211+
}
212+
return fmt.Sprint(change), nil
213+
}
214+
174215
// goLintCmd returns the appropriate golangci-lint command to run for a project.
175216
func goLintCmd(root string, level string, fix bool) string {
176217
klog.Infof("Searching for go modules within %s ...", root)
@@ -300,5 +341,15 @@ func main() {
300341
} else {
301342
klog.Infof("Makefile has no changes")
302343
}
344+
345+
diff, err = updateGitignore(root, *dryRunFlag)
346+
if err != nil {
347+
klog.Exitf("update .gitignore failed: %v", err)
348+
}
349+
if diff != "" {
350+
klog.Infof(".gitignore changes:\n%s", diff)
351+
} else {
352+
klog.Infof(".gitignore has no changes")
353+
}
303354
}
304355
}

0 commit comments

Comments
 (0)