Skip to content

Commit c6187f4

Browse files
committed
Add support for YAML linting
Signed-off-by: Thomas Stromberg <[email protected]>
1 parent 175b553 commit c6187f4

File tree

7 files changed

+101
-49
lines changed

7 files changed

+101
-49
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ name: Go
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010

1111
build:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v2
1515

16-
- name: Set up Go
17-
uses: actions/setup-go@v2
18-
with:
19-
go-version: 1.17
16+
- name: Set up Go
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: 1.17
2020

21-
- name: Build
22-
run: go build -v ./...
21+
- name: Build
22+
run: go build -v ./...
2323

24-
- name: Test
25-
run: go test -v ./...
24+
- name: Test
25+
run: go test -v ./...
2626

27-
- name: Run
28-
run: cp Makefile Makefile.old && go run . .
27+
- name: Run
28+
run: cp Makefile Makefile.old && go run . .
2929

30-
- name: Lint
31-
run: make lint
30+
- name: Lint
31+
run: make lint
3232

33-
- name: Makefile diff
34-
run: diff -ubB Makefile.old Makefile
33+
- name: Makefile diff
34+
run: diff -ubB Makefile.old Makefile

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

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

1415
# Dependency directories (remove the comment below to include it)
1516
# vendor/

.golangci.yml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ linters:
9898
- durationcheck
9999
- errcheck
100100
# errname is only available in golangci-lint v1.42.0+ - wait until v1.43 is available to settle
101-
#- errname
101+
# - errname
102102
- errorlint
103103
- exhaustive
104104
- exportloopref
@@ -125,7 +125,7 @@ linters:
125125
- nolintlint
126126
- predeclared
127127
# disabling for the initial iteration of the linting tool
128-
#- promlinter
128+
# - promlinter
129129
- revive
130130
- rowserrcheck
131131
- sqlclosecheck
@@ -142,25 +142,25 @@ linters:
142142
- wastedassign
143143
- whitespace
144144

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
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
164164

165165
issues:
166166
# Excluding configuration per-path, per-linter, per-text and per-source

.yamllint

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
extends: default
3+
4+
rules:
5+
braces:
6+
max-spaces-inside: 1
7+
brackets:
8+
max-spaces-inside: 1
9+
comments: disable
10+
comments-indentation: disable
11+
document-start: disable
12+
line-length:
13+
level: warning
14+
max: 160
15+
allow-non-breakable-inline-mappings: true
16+
truthy: disable

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
GOLINT_VERSION ?= v1.42.0
66
HADOLINT_VERSION ?= v2.7.0
77
SHELLCHECK_VERSION ?= v0.7.2
8+
YAMLLINT_VERSION ?= 1.26.3
89
LINT_OS := $(shell uname)
910
LINT_ARCH := $(shell uname -m)
1011

@@ -15,13 +16,15 @@ ifeq ($(LINT_OS),Darwin)
1516
endif
1617
endif
1718

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

21-
lint: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
23+
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
2224
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run
2325
out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) -t info $(shell find . -name "*Dockerfile")
2426
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh")
27+
PYTHONPATH=$(YAMLLINT_ROOT)/lib $(YAMLLINT_ROOT)/bin/yamllint .
2528

2629
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck:
2730
mkdir -p out/linters
@@ -38,4 +41,8 @@ out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH):
3841
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLINT_VERSION)
3942
mv out/linters/golangci-lint out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
4043

44+
$(YAMLLINT_ROOT)/bin/yamllint:
45+
mkdir -p $(YAMLLINT_ROOT)/lib
46+
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
4148
# END: lint-install .

Makefile.tmpl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{{ if .Go }}GOLINT_VERSION ?= v1.42.0{{ end }}
66
{{ if .Dockerfile}}HADOLINT_VERSION ?= v2.7.0{{ end }}
77
{{ if .Shell}}SHELLCHECK_VERSION ?= v0.7.2{{ end }}
8+
{{ if .YAML}}YAMLLINT_VERSION ?= 1.26.3{{ end }}
89
LINT_OS := $(shell uname)
910
LINT_ARCH := $(shell uname -m)
1011

@@ -15,10 +16,11 @@ ifeq ($(LINT_OS),Darwin)
1516
endif
1617
endif
1718

18-
{{ if .Shell }}LINT_LOWER_OS = $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]'){{ end }}
19-
{{ if .Go }}GOLINT_CONFIG:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))/.golangci.yml{{ end }}
19+
{{ 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 .YAML }}YAMLLINT_ROOT = out/linters/yamllint-$(YAMLLINT_VERSION){{ end }}
2022

21-
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 }}
23+
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 }}
2224
{{- range .Commands }}
2325
{{ .}}{{ end}}
2426

@@ -43,4 +45,12 @@ out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH):
4345
mv out/linters/golangci-lint out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
4446

4547
{{ end -}}
48+
49+
{{ if .YAML -}}
50+
$(YAMLLINT_ROOT)/bin/yamllint:
51+
mkdir -p $(YAMLLINT_ROOT)/lib
52+
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
54+
{{ end -}}
55+
4656
# END: lint-install {{.Args}}

lint-install.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
goFlag = flag.String("go", "error", "Level to lint Go with: [ignore, warn, error]")
2222
shellFlag = flag.String("shell", "error", "Level to lint Shell with: [ignore, warn, error]")
2323
dockerfileFlag = flag.String("dockerfile", "error", "Level to lint Dockerfile with: [ignore, warn, error]")
24+
yamlFlag = flag.String("yaml", "error", "Level to lint YAML with: [ignore, warn, error]")
2425
makeFileName = flag.String("makefile", "Makefile", "name of Makefile to update")
2526

2627
//go:embed .golangci.yml
@@ -36,6 +37,7 @@ const (
3637
Go Language = iota
3738
Shell
3839
Dockerfile
40+
YAML
3941
)
4042

4143
type Config struct {
@@ -44,6 +46,7 @@ type Config struct {
4446
Go string
4547
Dockerfile string
4648
Shell string
49+
YAML string
4750
Commands []string
4851
}
4952

@@ -54,15 +57,17 @@ func applicableLinters(root string) (map[Language]bool, error) {
5457

5558
err := godirwalk.Walk(root, &godirwalk.Options{
5659
Callback: func(path string, de *godirwalk.Dirent) error {
57-
if strings.HasSuffix(path, ".go") {
60+
switch {
61+
case strings.HasSuffix(path, ".go"):
5862
found[Go] = true
59-
}
60-
if strings.HasSuffix(path, "Dockerfile") {
63+
case strings.HasSuffix(path, "Dockerfile"):
6164
found[Dockerfile] = true
62-
}
63-
if strings.HasSuffix(path, ".sh") {
65+
case strings.HasSuffix(path, ".sh"):
6466
found[Shell] = true
67+
case strings.HasSuffix(path, ".yml"), strings.HasSuffix(path, ".yaml"):
68+
found[YAML] = true
6569
}
70+
6671
return nil
6772
},
6873
Unsorted: true,
@@ -211,6 +216,15 @@ func dockerLintCmd(_ string, level string) string {
211216
return fmt.Sprintf(`out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) -t %s $(shell find . -name "*Dockerfile")`, threshold)
212217
}
213218

219+
// yamlLintCmd returns the appropriate yamllint command for a project.
220+
func yamlLintCmd(_ string, level string) string {
221+
suffix := ""
222+
if level == "warn" {
223+
suffix = " || true"
224+
}
225+
return fmt.Sprintf(`PYTHONPATH=$(YAMLLINT_ROOT)/lib $(YAMLLINT_ROOT)/bin/yamllint .%s`, suffix)
226+
}
227+
214228
// main creates peanut butter & jelly sandwiches with utmost precision.
215229
func main() {
216230
klog.InitFlags(nil)
@@ -258,6 +272,10 @@ func main() {
258272
cfg.Shell = *shellFlag
259273
cfg.Commands = append(cfg.Commands, shellLintCmd(root, cfg.Shell))
260274
}
275+
if needs[YAML] {
276+
cfg.YAML = *yamlFlag
277+
cfg.Commands = append(cfg.Commands, yamlLintCmd(root, cfg.Shell))
278+
}
261279

262280
diff, err := updateMakefile(root, cfg, *dryRunFlag)
263281
if err != nil {

0 commit comments

Comments
 (0)