Skip to content

Commit 4994b77

Browse files
authored
Merge pull request tinkerbell#21 from tstromberg/prettier
Add support for YAML linting
2 parents 40d58d6 + ef31848 commit 4994b77

File tree

8 files changed

+124
-43
lines changed

8 files changed

+124
-43
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ linters:
108108
- durationcheck
109109
- errcheck
110110
# errname is only available in golangci-lint v1.42.0+ - wait until v1.43 is available to settle
111-
#- errname
111+
# - errname
112112
- errorlint
113113
- exhaustive
114114
- exportloopref
@@ -137,7 +137,7 @@ linters:
137137
- nolintlint
138138
- predeclared
139139
# disabling for the initial iteration of the linting tool
140-
#- promlinter
140+
# - promlinter
141141
- revive
142142
- rowserrcheck
143143
- sqlclosecheck

.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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
12
# BEGIN: lint-install .
23
# http://github.com/tinkerbell/lint-install
34

45
GOLINT_VERSION ?= v1.42.0
56
HADOLINT_VERSION ?= v2.7.0
67
SHELLCHECK_VERSION ?= v0.7.2
8+
YAMLLINT_VERSION ?= 1.26.3
79
LINT_OS := $(shell uname)
810
LINT_ARCH := $(shell uname -m)
911
LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
@@ -15,13 +17,15 @@ ifeq ($(LINT_OS),Darwin)
1517
endif
1618
endif
1719

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

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)
24+
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
2225
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run
2326
out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) $(shell find . -name "*Dockerfile")
2427
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh")
28+
PYTHONPATH=$(YAMLLINT_ROOT)/lib $(YAMLLINT_ROOT)/bin/yamllint .
2529

2630
fix: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
2731
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run --fix
@@ -42,4 +46,8 @@ out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH):
4246
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLINT_VERSION)
4347
mv out/linters/golangci-lint out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
4448

49+
$(YAMLLINT_ROOT)/bin/yamllint:
50+
mkdir -p $(YAMLLINT_ROOT)/lib
51+
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
52+
cd $(YAMLLINT_ROOT) && PYTHONPATH=lib python setup.py -q install --prefix . --install-lib lib
4553
# 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
LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
@@ -16,10 +17,11 @@ ifeq ($(LINT_OS),Darwin)
1617
endif
1718
endif
1819

19-
{{ if .Shell }}LINT_LOWER_OS = $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]'){{ end }}
20-
{{ if .Go }}GOLINT_CONFIG:=$(LINT_ROOT)/.golangci.yml{{ end }}
20+
{{ if .Shell }}LINT_LOWER_OS = $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]'){{ end }}
21+
{{ if .Go }}GOLINT_CONFIG = $(LINT_ROOT)/.golangci.yml{{ end }}
22+
{{ if .YAML }}YAMLLINT_ROOT = out/linters/yamllint-$(YAMLLINT_VERSION){{ end }}
2123

22-
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 }}
24+
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 }}
2325
{{- range .LintCommands }}
2426
{{ .}}{{ end}}
2527

@@ -48,4 +50,12 @@ out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH):
4850
mv out/linters/golangci-lint out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
4951

5052
{{ end -}}
53+
54+
{{ if .YAML -}}
55+
$(YAMLLINT_ROOT)/bin/yamllint:
56+
mkdir -p $(YAMLLINT_ROOT)/lib
57+
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
58+
cd $(YAMLLINT_ROOT) && PYTHONPATH=lib python setup.py -q install --prefix . --install-lib lib
59+
{{ end -}}
60+
5161
# END: lint-install {{.Args}}

README.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,60 @@
11
# lint-install
22

33
[![GoReport Widget]][GoReport Status]
4-
![](https://img.shields.io/badge/Stability-Experimental-red.svg)
4+
[![stability-stable](https://img.shields.io/badge/stability-stable-green.svg)](https://github.com/emersion/stability-badges#stable)
55

66
[GoReport Status]: https://goreportcard.com/report/github.com/tinkerbell/lint-install
77
[GoReport Widget]: https://goreportcard.com/badge/github.com/tinkerbell/lint-install
88

9-
Install well-configured linters to your project in a consistent and repeatable way. This tool specifically supports creating and updating `Makefile`
10-
targets, and lints the following:
9+
Idiomatic linters for opinionated projects.
10+
11+
This tool installs well-configured linters to any project, open-source or
12+
otherwise. The linters can be used in a repeatable and consistent way across CI,
13+
local tests, and IDE's.
14+
15+
lint-install adds linter configuration to the root of your project, and Makefile
16+
rules to install a consistently versioned set of linters to be used in any
17+
environment. These Makefile rules can also be upgrading by lint-install, updating
18+
all environments simultaneously.
19+
20+
Currently supported languages:
1121

1222
- Go
1323
- Shell
1424
- Dockerfile
25+
- YAML
1526

1627
## Philosophy
1728

18-
Catch as many errors as possible, but be idiomatic to the language.
29+
- Catch all the bugs!
30+
- Improve readability as much as possible.
31+
- Be idiomatic: only raise issues that the language authors would flag
1932

2033
## Usage
2134

22-
```
23-
go get github.com/tinkerbell/lint-install
24-
$HOME/go/bin/lint-install <repo>
25-
```
35+
Installation:
36+
37+
`go get github.com/tinkerbell/lint-install`
38+
39+
Add Makefile rules for a git repository:
40+
41+
`$HOME/go/bin/lint-install <repo>`
42+
43+
Users can then lint the project using:
44+
45+
`make lint`
2646

27-
## Options
47+
Other options:
2848

29-
* `--dry-run`: Log what changes would be made if any
30-
* `--shell=warn`: Make shell warnings non-fatal
31-
* `--dockerfile=warn`: Make Dockerfile warnings non-fatal
32-
* `--go=warn`: Make Dockerfile warnings non-fatal
49+
-dockerfile string
50+
Level to lint Dockerfile with: [ignore, warn, error] (default "error")
51+
-dry-run
52+
Display changes to make
53+
-go string
54+
Level to lint Go with: [ignore, warn, error] (default "error")
55+
-makefile string
56+
name of Makefile to update (default "Makefile")
57+
-shell string
58+
Level to lint Shell with: [ignore, warn, error] (default "error")
59+
-yaml string
60+
Level to lint YAML with: [ignore, warn, error] (default "error")

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
LintCommands []string
4851
FixCommands []string
4952
}
@@ -55,15 +58,17 @@ func applicableLinters(root string) (map[Language]bool, error) {
5558

5659
err := godirwalk.Walk(root, &godirwalk.Options{
5760
Callback: func(path string, de *godirwalk.Dirent) error {
58-
if strings.HasSuffix(path, ".go") {
61+
switch {
62+
case strings.HasSuffix(path, ".go"):
5963
found[Go] = true
60-
}
61-
if strings.HasSuffix(path, "Dockerfile") {
64+
case strings.HasSuffix(path, "Dockerfile"):
6265
found[Dockerfile] = true
63-
}
64-
if strings.HasSuffix(path, ".sh") {
66+
case strings.HasSuffix(path, ".sh"):
6567
found[Shell] = true
68+
case strings.HasSuffix(path, ".yml"), strings.HasSuffix(path, ".yaml"):
69+
found[YAML] = true
6670
}
71+
6772
return nil
6873
},
6974
Unsorted: true,
@@ -223,6 +228,15 @@ func dockerLintCmd(_ string, level string) string {
223228
return fmt.Sprintf(`out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH)%s $(shell find . -name "*Dockerfile")`, f)
224229
}
225230

231+
// yamlLintCmd returns the appropriate yamllint command for a project.
232+
func yamlLintCmd(_ string, level string) string {
233+
suffix := ""
234+
if level == "warn" {
235+
suffix = " || true"
236+
}
237+
return fmt.Sprintf(`PYTHONPATH=$(YAMLLINT_ROOT)/lib $(YAMLLINT_ROOT)/bin/yamllint .%s`, suffix)
238+
}
239+
226240
// main creates peanut butter & jelly sandwiches with utmost precision.
227241
func main() {
228242
klog.InitFlags(nil)
@@ -272,6 +286,10 @@ func main() {
272286
cfg.LintCommands = append(cfg.LintCommands, shellLintCmd(root, cfg.Shell, false))
273287
cfg.FixCommands = append(cfg.FixCommands, shellLintCmd(root, cfg.Shell, true))
274288
}
289+
if needs[YAML] {
290+
cfg.YAML = *yamlFlag
291+
cfg.LintCommands = append(cfg.LintCommands, yamlLintCmd(root, cfg.Shell))
292+
}
275293

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

0 commit comments

Comments
 (0)