Skip to content

Commit bfd34ba

Browse files
authored
Merge pull request #20 from per1234/check-go
Add template workflow to lint and check formatting of Go code
2 parents 355e447 + 3e4e6a6 commit bfd34ba

File tree

4 files changed

+314
-0
lines changed

4 files changed

+314
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# See: https://taskfile.dev/#/usage
2+
version: "3"
3+
4+
vars:
5+
DEFAULT_GO_PACKAGES:
6+
sh: echo $(go list ./... | tr '\n' ' ')
7+
8+
tasks:
9+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
10+
go:check:
11+
desc: Check for problems with Go code
12+
deps:
13+
- task: go:vet
14+
- task: go:lint
15+
16+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
17+
go:vet:
18+
desc: Check for errors in Go code
19+
cmds:
20+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
21+
22+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
23+
go:fix:
24+
desc: Modernize usages of outdated APIs
25+
cmds:
26+
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
27+
28+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
29+
go:lint:
30+
desc: Lint Go code
31+
cmds:
32+
- |
33+
PROJECT_PATH="$PWD"
34+
# `go get` and `go list` commands must be run from a temporary folder to avoid polluting go.mod
35+
cd "$(mktemp -d "${TMPDIR-${TMP-/tmp}}/task-temporary-XXXXX")"
36+
go get golang.org/x/lint/golint
37+
GOLINT_PATH="$(go list -f '{{"{{"}}.Target{{"}}"}}' golang.org/x/lint/golint || echo "false")"
38+
# `golint` must be run from the module folder
39+
cd "$PROJECT_PATH"
40+
"$GOLINT_PATH" \
41+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
42+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
43+
44+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
45+
go:format:
46+
desc: Format Go code
47+
cmds:
48+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

workflow-templates/check-go-task.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# "Check Go" workflow (Task)
2+
3+
Workflow file: [check-go-task.yml](check-go-task.yml)
4+
5+
Lint and check formatting of a [Go](https://golang.org/) module.
6+
7+
## Assets
8+
9+
- [`Taskfile.yml`](assets/check-go-task/Taskfile.yml] - Linting and formatting [tasks](https://taskfile.dev/).
10+
- Install to: repository root (or add the tasks into the existing `Taskfile.yml`)
11+
12+
## Readme badge
13+
14+
Markdown badge:
15+
16+
```markdown
17+
[![Check Go status](https://github.com/REPO_OWNER/REPO_NAME/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/REPO_OWNER/REPO_NAME/actions/workflows/check-go-task.yml)
18+
```
19+
20+
Replace the `REPO_OWNER` and `REPO_NAME` placeholders in the URLs with the final repository owner and name ([example](https://raw.githubusercontent.com/arduino-libraries/ArduinoIoTCloud/master/README.md)).
21+
22+
---
23+
24+
Asciidoc badge:
25+
26+
```adoc
27+
image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/check-go-task.yml/badge.svg["Check Go status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/check-go-task.yml"]
28+
```
29+
30+
Define the `{repository-owner}` and `{repository-name}` attributes and use them throughout the readme ([example](https://raw.githubusercontent.com/arduino-libraries/WiFiNINA/master/README.adoc)).
31+
32+
## Commit message
33+
34+
```
35+
Add CI workflow to lint and check formatting of Go code
36+
37+
On every push and pull request that affects relevant files, and periodically, check the Go module for:
38+
39+
- Common detectable errors in the code.
40+
- Use of outdated APIs
41+
- Code style violations
42+
- Code formatting inconsistency
43+
- Misconfiguration
44+
```
45+
46+
## PR message
47+
48+
```markdown
49+
On every push and pull request that affects relevant files, and periodically, check the repository's [Go](https://golang.org/) module for:
50+
51+
- Common detectable errors in the code.
52+
- Use of outdated APIs
53+
- Code style violations
54+
- Code formatting inconsistency
55+
- Misconfiguration
56+
```

workflow-templates/check-go-task.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md
2+
name: Check Go
3+
4+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
5+
on:
6+
push:
7+
paths:
8+
- ".github/workflows/check-go-task.ya?ml"
9+
- "Taskfile.ya?ml"
10+
- "go.mod"
11+
- "go.sum"
12+
- "**.go"
13+
pull_request:
14+
paths:
15+
- ".github/workflows/check-go-task.ya?ml"
16+
- "Taskfile.ya?ml"
17+
- "go.mod"
18+
- "go.sum"
19+
- "**.go"
20+
workflow_dispatch:
21+
repository_dispatch:
22+
23+
jobs:
24+
check-errors:
25+
runs-on: ubuntu-latest
26+
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v2
30+
31+
- name: Install Task
32+
uses: arduino/setup-task@v1
33+
with:
34+
repo-token: ${{ secrets.GITHUB_TOKEN }}
35+
version: 3.x
36+
37+
- name: Check for errors
38+
run: task go:vet
39+
40+
check-outdated:
41+
runs-on: ubuntu-latest
42+
43+
steps:
44+
- name: Checkout repository
45+
uses: actions/checkout@v2
46+
47+
- name: Install Task
48+
uses: arduino/setup-task@v1
49+
with:
50+
repo-token: ${{ secrets.GITHUB_TOKEN }}
51+
version: 3.x
52+
53+
- name: Modernize usages of outdated APIs
54+
run: task go:fix
55+
56+
- name: Check if any fixes were needed
57+
run: git diff --color --exit-code
58+
59+
check-style:
60+
runs-on: ubuntu-latest
61+
62+
steps:
63+
- name: Checkout repository
64+
uses: actions/checkout@v2
65+
66+
- name: Install Task
67+
uses: arduino/setup-task@v1
68+
with:
69+
repo-token: ${{ secrets.GITHUB_TOKEN }}
70+
version: 3.x
71+
72+
- name: Check style
73+
run: task --silent go:lint
74+
75+
check-formatting:
76+
runs-on: ubuntu-latest
77+
78+
steps:
79+
- name: Checkout repository
80+
uses: actions/checkout@v2
81+
82+
- name: Install Task
83+
uses: arduino/setup-task@v1
84+
with:
85+
repo-token: ${{ secrets.GITHUB_TOKEN }}
86+
version: 3.x
87+
88+
- name: Format code
89+
run: task go:format
90+
91+
- name: Check formatting
92+
run: git diff --color --exit-code
93+
94+
check-config:
95+
runs-on: ubuntu-latest
96+
97+
steps:
98+
- name: Checkout repository
99+
uses: actions/checkout@v2
100+
101+
- name: Run go mod tidy
102+
run: go mod tidy
103+
104+
- name: Check whether any tidying was needed
105+
run: git diff --color --exit-code
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md
2+
name: Check Go
3+
4+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
5+
on:
6+
push:
7+
paths:
8+
- ".github/workflows/check-go-task.ya?ml"
9+
- "Taskfile.ya?ml"
10+
- "go.mod"
11+
- "go.sum"
12+
- "**.go"
13+
pull_request:
14+
paths:
15+
- ".github/workflows/check-go-task.ya?ml"
16+
- "Taskfile.ya?ml"
17+
- "go.mod"
18+
- "go.sum"
19+
- "**.go"
20+
workflow_dispatch:
21+
repository_dispatch:
22+
23+
jobs:
24+
check-errors:
25+
runs-on: ubuntu-latest
26+
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v2
30+
31+
- name: Install Task
32+
uses: arduino/setup-task@v1
33+
with:
34+
repo-token: ${{ secrets.GITHUB_TOKEN }}
35+
version: 3.x
36+
37+
- name: Check for errors
38+
run: task go:vet
39+
40+
check-outdated:
41+
runs-on: ubuntu-latest
42+
43+
steps:
44+
- name: Checkout repository
45+
uses: actions/checkout@v2
46+
47+
- name: Install Task
48+
uses: arduino/setup-task@v1
49+
with:
50+
repo-token: ${{ secrets.GITHUB_TOKEN }}
51+
version: 3.x
52+
53+
- name: Modernize usages of outdated APIs
54+
run: task go:fix
55+
56+
- name: Check if any fixes were needed
57+
run: git diff --color --exit-code
58+
59+
check-style:
60+
runs-on: ubuntu-latest
61+
62+
steps:
63+
- name: Checkout repository
64+
uses: actions/checkout@v2
65+
66+
- name: Install Task
67+
uses: arduino/setup-task@v1
68+
with:
69+
repo-token: ${{ secrets.GITHUB_TOKEN }}
70+
version: 3.x
71+
72+
- name: Check style
73+
run: task --silent go:lint
74+
75+
check-formatting:
76+
runs-on: ubuntu-latest
77+
78+
steps:
79+
- name: Checkout repository
80+
uses: actions/checkout@v2
81+
82+
- name: Install Task
83+
uses: arduino/setup-task@v1
84+
with:
85+
repo-token: ${{ secrets.GITHUB_TOKEN }}
86+
version: 3.x
87+
88+
- name: Format code
89+
run: task go:format
90+
91+
- name: Check formatting
92+
run: git diff --color --exit-code
93+
94+
check-config:
95+
runs-on: ubuntu-latest
96+
97+
steps:
98+
- name: Checkout repository
99+
uses: actions/checkout@v2
100+
101+
- name: Run go mod tidy
102+
run: go mod tidy
103+
104+
- name: Check whether any tidying was needed
105+
run: git diff --color --exit-code

0 commit comments

Comments
 (0)