Skip to content

Commit 477982a

Browse files
Merge pull request #8 from MatteoPologruto/add-check-go
Add CI workflow to lint and check formatting of Go code
2 parents 6d1c4e8 + 37202bc commit 477982a

File tree

6 files changed

+328
-34
lines changed

6 files changed

+328
-34
lines changed

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

Lines changed: 223 additions & 0 deletions
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.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: windows-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: windows-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: windows-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: windows-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: windows-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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[![Check Taskfiles status](https://github.com/arduino/go-win32-utils/actions/workflows/check-taskfiles.yml/badge.svg)](https://github.com/arduino/go-win32-utils/actions/workflows/check-taskfiles.yml)
55
[![Test Go status](https://github.com/arduino/go-win32-utils/actions/workflows/test-go-task.yml/badge.svg)](https://github.com/arduino/go-win32-utils/actions/workflows/test-go-task.yml)
66
[![Codecov](https://codecov.io/gh/arduino/go-win32-utils/branch/main/graph/badge.svg)](https://codecov.io/gh/arduino/go-win32-utils)
7+
[![Check Go status](https://github.com/arduino/go-win32-utils/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/go-win32-utils/actions/workflows/check-go-task.yml)
78

89
This library contains some useful calls to win32 API that are not available on the standard golang library.
910

Taskfile.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,46 @@ tasks:
117117
-coverprofile=coverage_unit.txt \
118118
{{.TEST_LDFLAGS}} \
119119
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
120+
121+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
122+
go:fix:
123+
desc: Modernize usages of outdated APIs
124+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
125+
cmds:
126+
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
127+
128+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
129+
go:format:
130+
desc: Format Go code
131+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
132+
cmds:
133+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
134+
135+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
136+
go:lint:
137+
desc: Lint Go code
138+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
139+
cmds:
140+
- |
141+
if ! which golint &>/dev/null; then
142+
echo "golint not installed or not in PATH. Please install: https://github.com/golang/lint#installation"
143+
exit 1
144+
fi
145+
- |
146+
golint \
147+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
148+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
149+
150+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
151+
go:vet:
152+
desc: Check for errors in Go code
153+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
154+
cmds:
155+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
156+
157+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/go-task/Taskfile.yml
158+
go:build:
159+
desc: Build the Go code
160+
dir: "{{.DEFAULT_GO_MODULE_PATH}}"
161+
cmds:
162+
- go build -v {{.LDFLAGS}}

devicenotification/devicenotification.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ import (
4242
"golang.org/x/sys/windows"
4343
)
4444

45-
var osThreadId atomic.Uint32
45+
var osThreadID atomic.Uint32
4646

4747
// Start the device add/remove notification process, at every event a call to eventCB will be performed.
4848
// This function will block until interrupted by the given context. Errors will be passed to errorCB.
4949
// Returns error if sync process can't be started.
5050
func Start(ctx context.Context, eventCB func(), errorCB func(msg string)) error {
5151
runtime.LockOSThread()
5252
defer runtime.UnlockOSThread()
53-
osThreadId.Store(windows.GetCurrentThreadId())
53+
osThreadID.Store(windows.GetCurrentThreadId())
5454

5555
eventsChan := make(chan bool, 1)
5656
var eventsChanLock sync.Mutex
@@ -108,24 +108,24 @@ func Start(ctx context.Context, eventCB func(), errorCB func(msg string)) error
108108

109109
go func() {
110110
<-ctx.Done()
111-
_ = win32.PostMessage(windowHandle, win32.WM_Quit, 0, 0)
111+
_ = win32.PostMessage(windowHandle, win32.WMQuit, 0, 0)
112112
}()
113113

114114
for {
115115
// Verify running thread prerequisites
116-
if currThreadId := windows.GetCurrentThreadId(); currThreadId != osThreadId.Load() {
117-
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadId, osThreadId))
116+
if currThreadID := windows.GetCurrentThreadId(); currThreadID != osThreadID.Load() {
117+
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadID, osThreadID.Load()))
118118
}
119119

120120
var m win32.TagMSG
121-
if res := win32.GetMessage(&m, windowHandle, win32.WM_Quit, win32.WM_Quit); res == 0 { // 0 means we got a WM_QUIT
121+
if res := win32.GetMessage(&m, windowHandle, win32.WMQuit, win32.WMQuit); res == 0 { // 0 means we got a WMQUIT
122122
break
123123
} else if res == -1 { // -1 means that an error occurred
124124
err := windows.GetLastError()
125125
errorCB("error consuming messages: " + err.Error())
126126
break
127127
} else {
128-
// we got a message != WM_Quit, it should not happen but, just in case...
128+
// we got a message != WMQuit, it should not happen but, just in case...
129129
win32.TranslateMessage(&m)
130130
win32.DispatchMessage(&m)
131131
}
@@ -136,8 +136,8 @@ func Start(ctx context.Context, eventCB func(), errorCB func(msg string)) error
136136

137137
func createWindow(windowCallback win32.WindowProcCallback) (syscall.Handle, *byte, error) {
138138
// Verify running thread prerequisites
139-
if currThreadId := windows.GetCurrentThreadId(); currThreadId != osThreadId.Load() {
140-
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadId, osThreadId.Load()))
139+
if currThreadID := windows.GetCurrentThreadId(); currThreadID != osThreadID.Load() {
140+
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadID, osThreadID.Load()))
141141
}
142142

143143
moduleHandle, err := win32.GetModuleHandle(nil)
@@ -167,8 +167,8 @@ func createWindow(windowCallback win32.WindowProcCallback) (syscall.Handle, *byt
167167

168168
func destroyWindow(windowHandle syscall.Handle, className *byte) error {
169169
// Verify running thread prerequisites
170-
if currThreadId := windows.GetCurrentThreadId(); currThreadId != osThreadId.Load() {
171-
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadId, osThreadId.Load()))
170+
if currThreadID := windows.GetCurrentThreadId(); currThreadID != osThreadID.Load() {
171+
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadID, osThreadID.Load()))
172172
}
173173

174174
if err := win32.DestroyWindowEx(windowHandle); err != nil {
@@ -182,8 +182,8 @@ func destroyWindow(windowHandle syscall.Handle, className *byte) error {
182182

183183
func registerNotifications(windowHandle syscall.Handle) (syscall.Handle, error) {
184184
// Verify running thread prerequisites
185-
if currThreadId := windows.GetCurrentThreadId(); currThreadId != osThreadId.Load() {
186-
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadId, osThreadId.Load()))
185+
if currThreadID := windows.GetCurrentThreadId(); currThreadID != osThreadID.Load() {
186+
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadID, osThreadID.Load()))
187187
}
188188

189189
notificationFilter := win32.DevBroadcastDeviceInterface{
@@ -203,8 +203,8 @@ func registerNotifications(windowHandle syscall.Handle) (syscall.Handle, error)
203203

204204
func unregisterNotifications(notificationsDevHandle syscall.Handle) error {
205205
// Verify running thread prerequisites
206-
if currThreadId := windows.GetCurrentThreadId(); currThreadId != osThreadId.Load() {
207-
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadId, osThreadId.Load()))
206+
if currThreadID := windows.GetCurrentThreadId(); currThreadID != osThreadID.Load() {
207+
panic(fmt.Sprintf("this function must run on the main OS Thread: currThread=%d, osThread=%d", currThreadID, osThreadID.Load()))
208208
}
209209

210210
if err := win32.UnregisterDeviceNotification(notificationsDevHandle); err != nil {

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
* the GNU General Public License.
2828
*/
2929

30-
// win32 is a collection of useful bindings to Win32 API that are not available in the standard
30+
// Package win32 is a collection of useful bindings to Win32 API that are not available in the standard
3131
// golang windows/syscall package.
3232
package win32

0 commit comments

Comments
 (0)