Skip to content

Commit 150767a

Browse files
authored
Merge pull request #2 from per1234/check-go
Add CI workflow to lint and check formatting of Go code
2 parents 21b7809 + e4d3584 commit 150767a

19 files changed

+199
-55
lines changed

.github/workflows/check-go.yml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Check Go
2+
3+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
4+
on:
5+
push:
6+
paths:
7+
- ".github/workflows/check-go.yml"
8+
- "Taskfile.yml"
9+
- "**.go"
10+
pull_request:
11+
paths:
12+
- ".github/workflows/check-go.yml"
13+
- "Taskfile.yml"
14+
- "**.go"
15+
schedule:
16+
# Run every Tuesday at 8 AM UTC to catch breakage caused by changes to tools.
17+
- cron: "0 8 * * TUE"
18+
workflow_dispatch:
19+
repository_dispatch:
20+
21+
jobs:
22+
check-errors:
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- name: Checkout repository
27+
uses: actions/checkout@v2
28+
29+
- name: Install Task
30+
uses: arduino/actions/setup-taskfile@master
31+
with:
32+
repo-token: ${{ secrets.GITHUB_TOKEN }}
33+
version: 3.x
34+
35+
- name: Check for errors
36+
run: task go:vet
37+
38+
check-style:
39+
runs-on: ubuntu-latest
40+
41+
steps:
42+
- name: Checkout repository
43+
uses: actions/checkout@v2
44+
45+
- name: Install Task
46+
uses: arduino/actions/setup-taskfile@master
47+
with:
48+
repo-token: ${{ secrets.GITHUB_TOKEN }}
49+
version: 3.x
50+
51+
- name: Check style
52+
run: task --silent go:lint
53+
54+
check-formatting:
55+
runs-on: ubuntu-latest
56+
57+
steps:
58+
- name: Checkout repository
59+
uses: actions/checkout@v2
60+
61+
- name: Install Task
62+
uses: arduino/actions/setup-taskfile@master
63+
with:
64+
repo-token: ${{ secrets.GITHUB_TOKEN }}
65+
version: 3.x
66+
67+
- name: Format code
68+
run: task go:format
69+
70+
- name: Check formatting
71+
run: git diff --color --exit-code

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Check Go status](https://github.com/arduino/libraries-repository-engine/actions/workflows/check-go.yml/badge.svg)](https://github.com/arduino/libraries-repository-engine/actions/workflows/check-go.yml)
2+
13
BUILD
24
----------------------------
35

Taskfile.yml

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# See: https://taskfile.dev/#/usage
22
version: "3"
33

4-
env:
4+
vars:
55
DEFAULT_GO_PACKAGES:
66
sh: echo $(go list ./... | tr '\n' ' ')
77

@@ -15,3 +15,34 @@ tasks:
1515
desc: Run unit tests
1616
cmds:
1717
- go test -v -short -run '{{default ".*" .GO_TEST_REGEX}}' {{default "-timeout 10m -coverpkg=./... -covermode=atomic" .GO_TEST_FLAGS}} -coverprofile=coverage_unit.txt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
18+
19+
go:check:
20+
desc: Check for problems with Go code
21+
deps:
22+
- task: go:vet
23+
- task: go:lint
24+
25+
go:vet:
26+
desc: Check for errors in Go code
27+
cmds:
28+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
29+
30+
go:lint:
31+
desc: Lint Go code
32+
cmds:
33+
- |
34+
PROJECT_PATH="$PWD"
35+
# `go get` and `go list` commands must be run from a temporary folder to avoid polluting go.mod
36+
cd "$(mktemp -d "${TMPDIR-${TMP-/tmp}}/task-temporary-XXXXX")"
37+
go get golang.org/x/lint/golint
38+
GOLINT_PATH="$(go list -f '{{"{{"}}.Target{{"}}"}}' golang.org/x/lint/golint || echo "false")"
39+
# `golint` must be run from the module folder
40+
cd "$PROJECT_PATH"
41+
"$GOLINT_PATH" \
42+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
43+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
44+
45+
go:format:
46+
desc: Format Go code
47+
cmds:
48+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

libraries/bad_file_cleaner.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
)
99

10+
// FailIfHasUndesiredFiles returns an error if the folder contains any undesired files.
1011
func FailIfHasUndesiredFiles(folder string) error {
1112
err := failIfContainsForbiddenFileInRoot(folder)
1213
if err != nil {
@@ -15,10 +16,11 @@ func FailIfHasUndesiredFiles(folder string) error {
1516
return failIfContainsExes(folder)
1617
}
1718

18-
var FORBIDDEN_FILES = []string{".development"}
19+
// ForbiddenFiles is the names of the forbidden files.
20+
var ForbiddenFiles = []string{".development"}
1921

2022
func failIfContainsForbiddenFileInRoot(folder string) error {
21-
for _, file := range FORBIDDEN_FILES {
23+
for _, file := range ForbiddenFiles {
2224
if _, err := os.Stat(filepath.Join(folder, file)); err == nil {
2325
return errors.New(file + " file found, skipping")
2426
}
@@ -27,10 +29,11 @@ func failIfContainsForbiddenFileInRoot(folder string) error {
2729
return nil
2830
}
2931

30-
var PATTERNS = []string{"*.exe"}
32+
// Patterns is the file patterns of executables.
33+
var Patterns = []string{"*.exe"}
3134

3235
func failIfContainsExes(folder string) error {
33-
for _, pattern := range PATTERNS {
36+
for _, pattern := range Patterns {
3437
cmd := exec.Command("find", folder, "-type", "f", "-name", pattern)
3538
output, err := cmd.CombinedOutput()
3639
if err != nil {

libraries/clamav.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func modifyEnv(env []string, key, value string) []string {
3131
return envMapToSlice(envMap)
3232
}
3333

34+
// RunAntiVirus scans the folder for viruses.
3435
func RunAntiVirus(folder string) ([]byte, error) {
3536
cmd := exec.Command("clamdscan", "-i", folder)
3637
cmd.Env = modifyEnv(os.Environ(), "LANG", "en")
@@ -42,7 +43,7 @@ func RunAntiVirus(folder string) ([]byte, error) {
4243

4344
output := string(out)
4445
if strings.Index(output, "Infected files: 0") == -1 {
45-
return out, errors.New("Infected files found!")
46+
return out, errors.New("Infected files found")
4647
}
4748

4849
return out, nil

libraries/cron/fill_missing_checksums.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package cron
22

33
import (
4-
"arduino.cc/repository/libraries/hash"
54
"io"
65
"net/http"
76
"os"
7+
8+
"arduino.cc/repository/libraries/hash"
89
)
910

1011
/*
11-
Check for missing size and checksum field and fills them
12-
by downloading a copy of the file.
12+
FillMissingChecksumsForDownloadArchives checks for missing size and checksum field and fills them
13+
by downloading a copy of the file.
1314
*/
1415
func FillMissingChecksumsForDownloadArchives(URL string, filename string) (int64, string, error) {
1516
size, err := download(URL, filename)

libraries/db/db.go

+15
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ type Dependency struct {
5555
Version string
5656
}
5757

58+
// New returns a new DB object.
5859
func New(libraryFile string) *DB {
5960
return &DB{libraryFile: libraryFile}
6061
}
6162

63+
// AddLibrary adds a library to the database.
6264
func (db *DB) AddLibrary(library *Library) error {
6365
db.mutex.Lock()
6466
defer db.mutex.Unlock()
@@ -70,6 +72,7 @@ func (db *DB) AddLibrary(library *Library) error {
7072
return nil
7173
}
7274

75+
// HasLibrary returns whether the database already contains the given library.
7376
func (db *DB) HasLibrary(libraryName string) bool {
7477
db.mutex.Lock()
7578
defer db.mutex.Unlock()
@@ -81,6 +84,7 @@ func (db *DB) hasLibrary(libraryName string) bool {
8184
return found != nil
8285
}
8386

87+
// FindLibrary returns the Library object for the given name.
8488
func (db *DB) FindLibrary(libraryName string) (*Library, error) {
8589
db.mutex.Lock()
8690
defer db.mutex.Unlock()
@@ -96,6 +100,7 @@ func (db *DB) findLibrary(libraryName string) (*Library, error) {
96100
return nil, errors.New("library not found")
97101
}
98102

103+
// AddRelease adds a library release to the database.
99104
func (db *DB) AddRelease(release *Release, repoURL string) error {
100105
db.mutex.Lock()
101106
defer db.mutex.Unlock()
@@ -120,6 +125,7 @@ func (db *DB) AddRelease(release *Release, repoURL string) error {
120125
return nil
121126
}
122127

128+
// HasReleaseByNameVersion returns whether the database contains a release for the given library and version number.
123129
func (db *DB) HasReleaseByNameVersion(libraryName string, libraryVersion string) bool {
124130
db.mutex.Lock()
125131
defer db.mutex.Unlock()
@@ -131,6 +137,7 @@ func (db *DB) hasReleaseByNameVersion(libraryName string, libraryVersion string)
131137
return found != nil
132138
}
133139

140+
// HasRelease returns whether the database already contains the given Release object.
134141
func (db *DB) HasRelease(release *Release) bool {
135142
db.mutex.Lock()
136143
defer db.mutex.Unlock()
@@ -141,6 +148,7 @@ func (db *DB) hasRelease(release *Release) bool {
141148
return db.hasReleaseByNameVersion(release.LibraryName, release.Version.String())
142149
}
143150

151+
// FindRelease returns the Release object from the database that matches the given object.
144152
func (db *DB) FindRelease(release *Release) (*Release, error) {
145153
db.mutex.Lock()
146154
defer db.mutex.Unlock()
@@ -156,6 +164,7 @@ func (db *DB) findReleaseByNameVersion(libraryName string, libraryVersion string
156164
return nil, errors.New("library not found")
157165
}
158166

167+
// LoadFromFile returns a DB object loaded from the given filename.
159168
func LoadFromFile(filename string) (*DB, error) {
160169
file, err := os.Open(filename)
161170
if err != nil {
@@ -170,6 +179,7 @@ func LoadFromFile(filename string) (*DB, error) {
170179
return db, nil
171180
}
172181

182+
// Load returns a DB object loaded from the given reader.
173183
func Load(r io.Reader) (*DB, error) {
174184
decoder := json.NewDecoder(r)
175185
db := new(DB)
@@ -180,6 +190,7 @@ func Load(r io.Reader) (*DB, error) {
180190
return db, nil
181191
}
182192

193+
// SaveToFile saves the database to a file.
183194
func (db *DB) SaveToFile() error {
184195
db.mutex.Lock()
185196
defer db.mutex.Unlock()
@@ -191,6 +202,7 @@ func (db *DB) SaveToFile() error {
191202
return db.save(file)
192203
}
193204

205+
// Save writes the database via the given writer.
194206
func (db *DB) Save(r io.Writer) error {
195207
db.mutex.Lock()
196208
defer db.mutex.Unlock()
@@ -222,6 +234,7 @@ func (db *DB) findLatestReleaseOfLibrary(lib *Library) (*Release, error) {
222234
return found, nil
223235
}
224236

237+
// FindReleasesOfLibrary returns the database's releases for the given Library object.
225238
func (db *DB) FindReleasesOfLibrary(lib *Library) []*Release {
226239
db.mutex.Lock()
227240
defer db.mutex.Unlock()
@@ -239,10 +252,12 @@ func (db *DB) findReleasesOfLibrary(lib *Library) []*Release {
239252
return releases
240253
}
241254

255+
// Commit saves the database to disk.
242256
func (db *DB) Commit() error {
243257
return db.SaveToFile()
244258
}
245259

260+
// Init loads a database from file and returns it.
246261
func Init(libraryFile string) *DB {
247262
libs, err := LoadFromFile(libraryFile)
248263
if err != nil {

libraries/db/versioning.go

+6
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,34 @@ package db
22

33
import "encoding/json"
44

5+
// Version is the type for library versions.
56
type Version struct {
67
version string
78
}
89

10+
// Less returns whether the receiver version is lower than the argument.
911
func (version *Version) Less(other Version) (bool, error) {
1012
// TODO: apply semantic versioning
1113
return version.version < other.version, nil
1214
}
1315

16+
// String returns the version in string form.
1417
func (version *Version) String() string {
1518
return version.version
1619
}
1720

21+
// UnmarshalJSON parses the JSON-encoded argument and stores the result in the receiver.
1822
func (version *Version) UnmarshalJSON(data []byte) error {
1923
return json.Unmarshal(data, &version.version)
2024
}
2125

26+
// MarshalJSON returns the JSON encoding of the receiver.
2227
func (version *Version) MarshalJSON() ([]byte, error) {
2328
// Encode version as a string
2429
return json.Marshal(version.version)
2530
}
2631

32+
// VersionFromString parses a string to a Version object.
2733
func VersionFromString(str string) Version {
2834
return Version{version: str}
2935
}

libraries/file/SCCS.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package file
22

3+
// SCCSFiles is a map of folder names used internally by source code control systems.
34
var SCCSFiles = map[string]bool{
45
"CVS": true,
56
"RCS": true,
@@ -8,6 +9,7 @@ var SCCSFiles = map[string]bool{
89
".hg": true,
910
".bzr": true}
1011

12+
// IsSCCS returns whether the given string is a folder name used internally by source code control systems.
1113
func IsSCCS(name string) bool {
1214
return SCCSFiles[name]
1315
}

0 commit comments

Comments
 (0)