Skip to content

Commit 2f7a0d0

Browse files
authored
Merge pull request #23 from per1234/module
Add CI workflow to lint and check formatting of Go code
2 parents e5adc1e + d986e16 commit 2f7a0d0

File tree

5 files changed

+274
-10
lines changed

5 files changed

+274
-10
lines changed

Diff for: .github/workflows/check-go-task.yml

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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.17"
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 "::set-output name=result::$RESULT"
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: Check for errors
84+
env:
85+
GO_MODULE_PATH: ${{ matrix.module.path }}
86+
run: task go:vet
87+
88+
check-outdated:
89+
name: check-outdated (${{ matrix.module.path }})
90+
needs: run-determination
91+
if: needs.run-determination.outputs.result == 'true'
92+
runs-on: ubuntu-latest
93+
94+
strategy:
95+
fail-fast: false
96+
97+
matrix:
98+
module:
99+
- path: ./
100+
101+
steps:
102+
- name: Checkout repository
103+
uses: actions/checkout@v3
104+
105+
- name: Install Go
106+
uses: actions/setup-go@v3
107+
with:
108+
go-version: ${{ env.GO_VERSION }}
109+
110+
- name: Install Task
111+
uses: arduino/setup-task@v1
112+
with:
113+
repo-token: ${{ secrets.GITHUB_TOKEN }}
114+
version: 3.x
115+
116+
- name: Modernize usages of outdated APIs
117+
env:
118+
GO_MODULE_PATH: ${{ matrix.module.path }}
119+
run: task go:fix
120+
121+
- name: Check if any fixes were needed
122+
run: git diff --color --exit-code
123+
124+
check-style:
125+
name: check-style (${{ matrix.module.path }})
126+
needs: run-determination
127+
if: needs.run-determination.outputs.result == 'true'
128+
runs-on: ubuntu-latest
129+
130+
strategy:
131+
fail-fast: false
132+
133+
matrix:
134+
module:
135+
- path: ./
136+
137+
steps:
138+
- name: Checkout repository
139+
uses: actions/checkout@v3
140+
141+
- name: Install Go
142+
uses: actions/setup-go@v3
143+
with:
144+
go-version: ${{ env.GO_VERSION }}
145+
146+
- name: Install Task
147+
uses: arduino/setup-task@v1
148+
with:
149+
repo-token: ${{ secrets.GITHUB_TOKEN }}
150+
version: 3.x
151+
152+
- name: Install golint
153+
run: go install golang.org/x/lint/golint@latest
154+
155+
- name: Check style
156+
env:
157+
GO_MODULE_PATH: ${{ matrix.module.path }}
158+
run: task --silent go:lint
159+
160+
check-formatting:
161+
name: check-formatting (${{ matrix.module.path }})
162+
needs: run-determination
163+
if: needs.run-determination.outputs.result == 'true'
164+
runs-on: ubuntu-latest
165+
166+
strategy:
167+
fail-fast: false
168+
169+
matrix:
170+
module:
171+
- path: ./
172+
173+
steps:
174+
- name: Checkout repository
175+
uses: actions/checkout@v3
176+
177+
- name: Install Go
178+
uses: actions/setup-go@v3
179+
with:
180+
go-version: ${{ env.GO_VERSION }}
181+
182+
- name: Install Task
183+
uses: arduino/setup-task@v1
184+
with:
185+
repo-token: ${{ secrets.GITHUB_TOKEN }}
186+
version: 3.x
187+
188+
- name: Format code
189+
env:
190+
GO_MODULE_PATH: ${{ matrix.module.path }}
191+
run: task go:format
192+
193+
- name: Check formatting
194+
run: git diff --color --exit-code
195+
196+
check-config:
197+
name: check-config (${{ matrix.module.path }})
198+
needs: run-determination
199+
if: needs.run-determination.outputs.result == 'true'
200+
runs-on: ubuntu-latest
201+
202+
strategy:
203+
fail-fast: false
204+
205+
matrix:
206+
module:
207+
- path: ./
208+
209+
steps:
210+
- name: Checkout repository
211+
uses: actions/checkout@v3
212+
213+
- name: Install Go
214+
uses: actions/setup-go@v3
215+
with:
216+
go-version: ${{ env.GO_VERSION }}
217+
218+
- name: Run go mod tidy
219+
working-directory: ${{ matrix.module.path }}
220+
run: go mod tidy
221+
222+
- name: Check whether any tidying was needed
223+
run: git diff --color --exit-code

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# arduinoOTA
22

33
[![Check General Formatting status](https://github.com/arduino/arduinoOTA/actions/workflows/check-general-formatting-task.yml/badge.svg)](https://github.com/arduino/arduinoOTA/actions/workflows/check-general-formatting-task.yml)
4+
[![Check Go status](https://github.com/arduino/arduinoOTA/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/arduinoOTA/actions/workflows/check-go-task.yml)
45
[![Check npm status](https://github.com/arduino/arduinoOTA/actions/workflows/check-npm-task.yml/badge.svg)](https://github.com/arduino/arduinoOTA/actions/workflows/check-npm-task.yml)
56
[![Check Prettier Formatting status](https://github.com/arduino/arduinoOTA/actions/workflows/check-prettier-formatting-task.yml/badge.svg)](https://github.com/arduino/arduinoOTA/actions/workflows/check-prettier-formatting-task.yml)
67
[![Spell Check status](https://github.com/arduino/arduinoOTA/actions/workflows/spell-check-task.yml/badge.svg)](https://github.com/arduino/arduinoOTA/actions/workflows/spell-check-task.yml)

Diff for: Taskfile.yml

+41
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
version: "3"
33

44
vars:
5+
# Path of the project's primary Go module:
6+
DEFAULT_GO_MODULE_PATH: ./
7+
DEFAULT_GO_PACKAGES:
8+
sh: |
9+
echo $(cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"')
510
# Last version of ajv-cli with support for the JSON schema "Draft 4" specification
611
SCHEMA_DRAFT_4_AJV_CLI_VERSION: 3.3.0
712

@@ -74,6 +79,42 @@ tasks:
7479
cmds:
7580
- poetry run codespell --write-changes
7681

82+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
83+
go:fix:
84+
desc: Modernize usages of outdated APIs
85+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
86+
cmds:
87+
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
88+
89+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
90+
go:format:
91+
desc: Format Go code
92+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
93+
cmds:
94+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
95+
96+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
97+
go:lint:
98+
desc: Lint Go code
99+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
100+
cmds:
101+
- |
102+
if ! which golint &>/dev/null; then
103+
echo "golint not installed or not in PATH. Please install: https://github.com/golang/lint#installation"
104+
exit 1
105+
fi
106+
- |
107+
golint \
108+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
109+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
110+
111+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
112+
go:vet:
113+
desc: Check for errors in Go code
114+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
115+
cmds:
116+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
117+
77118
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
78119
markdown:check-links:
79120
desc: Check for broken links

Diff for: go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/arduino/arduinoOTA
2+
3+
go 1.17

Diff for: main.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"time"
1818
)
1919

20+
// AppVersion is the application version.
2021
const AppVersion = "1.3.0"
2122

2223
var compileInfo string
@@ -40,11 +41,6 @@ var (
4041
timeoutSeconds = flag.Int("t", 10, "Upload timeout")
4142
)
4243

43-
type Item struct {
44-
Id int
45-
Name string
46-
}
47-
4844
func main() {
4945
flag.Parse()
5046

@@ -131,9 +127,9 @@ func main() {
131127
var sketchData *bytes.Buffer
132128

133129
if *binMode {
134-
sketchData = StreamToBytes(f)
130+
sketchData = streamToBytes(f)
135131
} else {
136-
str := StreamToString(f)
132+
str := streamToString(f)
137133
re := regexp.MustCompile(`\r?\n`)
138134
str = re.ReplaceAllString(str, "")
139135
sketchData = bytes.NewBufferString(str)
@@ -232,14 +228,14 @@ func main() {
232228
}
233229
}
234230

235-
func StreamToBytes(stream io.Reader) *bytes.Buffer {
231+
func streamToBytes(stream io.Reader) *bytes.Buffer {
236232
buf := new(bytes.Buffer)
237233
buf.ReadFrom(stream)
238234
return buf
239235
}
240236

241-
func StreamToString(stream io.Reader) string {
242-
return StreamToBytes(stream).String()
237+
func streamToString(stream io.Reader) string {
238+
return streamToBytes(stream).String()
243239
}
244240

245241
func getMyIP(otherip net.IP) net.IP {

0 commit comments

Comments
 (0)