Skip to content

Commit 3d1b269

Browse files
Merge pull request #31 from arduino/add-check-go-task
Add CI workflow to lint and check formatting of Go code
2 parents d8474a7 + af82105 commit 3d1b269

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-0
lines changed

.github/workflows/check-go-task.yml

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md
2+
name: Check Go
3+
4+
env:
5+
# See: https://github.com/actions/setup-go/tree/main#supported-version-syntax
6+
GO_VERSION: "1.19"
7+
8+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/check-go-task.ya?ml"
14+
- "Taskfile.ya?ml"
15+
- "**/go.mod"
16+
- "**/go.sum"
17+
- "**.go"
18+
pull_request:
19+
paths:
20+
- ".github/workflows/check-go-task.ya?ml"
21+
- "Taskfile.ya?ml"
22+
- "**/go.mod"
23+
- "**/go.sum"
24+
- "**.go"
25+
schedule:
26+
# Run periodically to catch breakage caused by external changes.
27+
- cron: "0 7 * * WED"
28+
workflow_dispatch:
29+
repository_dispatch:
30+
31+
jobs:
32+
run-determination:
33+
runs-on: ubuntu-latest
34+
outputs:
35+
result: ${{ steps.determination.outputs.result }}
36+
steps:
37+
- name: Determine if the rest of the workflow should run
38+
id: determination
39+
run: |
40+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
41+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
42+
if [[
43+
"${{ github.event_name }}" != "create" ||
44+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
45+
]]; then
46+
# Run the other jobs.
47+
RESULT="true"
48+
else
49+
# There is no need to run the other jobs.
50+
RESULT="false"
51+
fi
52+
53+
echo "result=$RESULT" >> $GITHUB_OUTPUT
54+
55+
check-errors:
56+
name: check-errors (${{ matrix.module.path }})
57+
needs: run-determination
58+
if: needs.run-determination.outputs.result == 'true'
59+
runs-on: ubuntu-latest
60+
61+
strategy:
62+
fail-fast: false
63+
64+
matrix:
65+
module:
66+
- path: ./
67+
68+
steps:
69+
- name: Checkout repository
70+
uses: actions/checkout@v3
71+
72+
- name: Install Go
73+
uses: actions/setup-go@v3
74+
with:
75+
go-version: ${{ env.GO_VERSION }}
76+
77+
- name: Install Task
78+
uses: arduino/setup-task@v1
79+
with:
80+
repo-token: ${{ secrets.GITHUB_TOKEN }}
81+
version: 3.x
82+
83+
- name: Download external files
84+
run: |
85+
task download-dfu-util
86+
task download-libusb
87+
88+
- name: Check for errors
89+
env:
90+
GO_MODULE_PATH: ${{ matrix.module.path }}
91+
run: task go:vet
92+
93+
check-outdated:
94+
name: check-outdated (${{ matrix.module.path }})
95+
needs: run-determination
96+
if: needs.run-determination.outputs.result == 'true'
97+
runs-on: ubuntu-latest
98+
99+
strategy:
100+
fail-fast: false
101+
102+
matrix:
103+
module:
104+
- path: ./
105+
106+
steps:
107+
- name: Checkout repository
108+
uses: actions/checkout@v3
109+
110+
- name: Install Go
111+
uses: actions/setup-go@v3
112+
with:
113+
go-version: ${{ env.GO_VERSION }}
114+
115+
- name: Install Task
116+
uses: arduino/setup-task@v1
117+
with:
118+
repo-token: ${{ secrets.GITHUB_TOKEN }}
119+
version: 3.x
120+
121+
- name: Modernize usages of outdated APIs
122+
env:
123+
GO_MODULE_PATH: ${{ matrix.module.path }}
124+
run: task go:fix
125+
126+
- name: Check if any fixes were needed
127+
run: git diff --color --exit-code
128+
129+
check-style:
130+
name: check-style (${{ matrix.module.path }})
131+
needs: run-determination
132+
if: needs.run-determination.outputs.result == 'true'
133+
runs-on: ubuntu-latest
134+
135+
strategy:
136+
fail-fast: false
137+
138+
matrix:
139+
module:
140+
- path: ./
141+
142+
steps:
143+
- name: Checkout repository
144+
uses: actions/checkout@v3
145+
146+
- name: Install Go
147+
uses: actions/setup-go@v3
148+
with:
149+
go-version: ${{ env.GO_VERSION }}
150+
151+
- name: Install Task
152+
uses: arduino/setup-task@v1
153+
with:
154+
repo-token: ${{ secrets.GITHUB_TOKEN }}
155+
version: 3.x
156+
157+
- name: Install golint
158+
run: go install golang.org/x/lint/golint@latest
159+
160+
- name: Check style
161+
env:
162+
GO_MODULE_PATH: ${{ matrix.module.path }}
163+
run: task --silent go:lint
164+
165+
check-formatting:
166+
name: check-formatting (${{ matrix.module.path }})
167+
needs: run-determination
168+
if: needs.run-determination.outputs.result == 'true'
169+
runs-on: ubuntu-latest
170+
171+
strategy:
172+
fail-fast: false
173+
174+
matrix:
175+
module:
176+
- path: ./
177+
178+
steps:
179+
- name: Checkout repository
180+
uses: actions/checkout@v3
181+
182+
- name: Install Go
183+
uses: actions/setup-go@v3
184+
with:
185+
go-version: ${{ env.GO_VERSION }}
186+
187+
- name: Install Task
188+
uses: arduino/setup-task@v1
189+
with:
190+
repo-token: ${{ secrets.GITHUB_TOKEN }}
191+
version: 3.x
192+
193+
- name: Format code
194+
env:
195+
GO_MODULE_PATH: ${{ matrix.module.path }}
196+
run: task go:format
197+
198+
- name: Check formatting
199+
run: git diff --color --exit-code
200+
201+
check-config:
202+
name: check-config (${{ matrix.module.path }})
203+
needs: run-determination
204+
if: needs.run-determination.outputs.result == 'true'
205+
runs-on: ubuntu-latest
206+
207+
strategy:
208+
fail-fast: false
209+
210+
matrix:
211+
module:
212+
- path: ./
213+
214+
steps:
215+
- name: Checkout repository
216+
uses: actions/checkout@v3
217+
218+
- name: Install Go
219+
uses: actions/setup-go@v3
220+
with:
221+
go-version: ${{ env.GO_VERSION }}
222+
223+
- name: Run go mod tidy
224+
working-directory: ${{ matrix.module.path }}
225+
run: go mod tidy
226+
227+
- name: Check whether any tidying was needed
228+
run: git diff --color --exit-code

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dfu-discovery
55
*.o
66
dfu-util_*.c
77
dist/
8+
libusb.h

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[![Sync Labels status](https://github.com/arduino/dfu-discovery/actions/workflows/sync-labels.yml/badge.svg)](https://github.com/arduino/dfu-discovery/actions/workflows/sync-labels.yml)
33
[![Check Go Dependencies status](https://github.com/arduino/dfu-discovery/actions/workflows/check-go-dependencies-task.yml/badge.svg)](https://github.com/arduino/dfu-discovery/actions/workflows/check-go-dependencies-task.yml)
44
[![Check License status](https://github.com/arduino/dfu-discovery/actions/workflows/check-license.yml/badge.svg)](https://github.com/arduino/dfu-discovery/actions/workflows/check-license.yml)
5+
[![Check Go status](https://github.com/arduino/dfu-discovery/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/dfu-discovery/actions/workflows/check-go-task.yml)
56

67
The `dfu-discovery` tool is a command line program that interacts via stdio. It accepts commands as plain ASCII strings terminated with LF `\n` and sends response as JSON.
78

Taskfile.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ vars:
2727
-X {{.CONFIGURATION_PACKAGE}}.Timestamp={{.TIMESTAMP}}
2828
'
2929
DFU_UTIL_VERSION: dfu-util-0.11
30+
LIBUSB_VERSION: libusb-1.0.26
31+
# Path of the project's primary Go module:
32+
DEFAULT_GO_MODULE_PATH: ./
33+
DEFAULT_GO_PACKAGES:
34+
sh: |
35+
echo $(
36+
cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} &&
37+
go list ./... | tr '\n' ' ' ||
38+
echo '"ERROR: Unable to discover Go packages"'
39+
)
3040
3141
tasks:
3242
download-dfu-util:
@@ -46,6 +56,19 @@ tasks:
4656
# for some reason quirks.c has the exec flag set
4757
chmod -x dfu-util_quirks.c
4858
fi
59+
60+
download-libusb:
61+
desc: download libusb and copy the files in the correct dir
62+
cmds:
63+
- |
64+
if [ -d "{{.LIBUSB_VERSION}}" ];
65+
then
66+
echo "{{.LIBUSB_VERSION}} already downloaded (directory exists)."
67+
else
68+
echo "Downloading {{.LIBUSB_VERSION}}..."
69+
curl -L https://github.com/libusb/libusb/releases/download/v1.0.26/libusb-1.0.26.tar.bz2 -s | tar xj
70+
cp {{.LIBUSB_VERSION}}/libusb/libusb.h .
71+
fi
4972
5073
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-dependencies-task/Taskfile.yml
5174
general:prepare-deps:
@@ -78,3 +101,39 @@ tasks:
78101
- task: general:cache-dep-licenses
79102
cmds:
80103
- licensed status
104+
105+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
106+
go:fix:
107+
desc: Modernize usages of outdated APIs
108+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
109+
cmds:
110+
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
111+
112+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
113+
go:format:
114+
desc: Format Go code
115+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
116+
cmds:
117+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
118+
119+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
120+
go:lint:
121+
desc: Lint Go code
122+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
123+
cmds:
124+
- |
125+
if ! which golint &>/dev/null; then
126+
echo "golint not installed or not in PATH. Please install: https://github.com/golang/lint#installation"
127+
exit 1
128+
fi
129+
- |
130+
golint \
131+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
132+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
133+
134+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
135+
go:vet:
136+
desc: Check for errors in Go code
137+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
138+
cmds:
139+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

0 commit comments

Comments
 (0)