Skip to content

Commit a903209

Browse files
committed
chore(ci): add golangci-lint
1 parent e721d95 commit a903209

File tree

13 files changed

+186
-40
lines changed

13 files changed

+186
-40
lines changed

.github/workflows/lint.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
on: [push, pull_request]
2+
3+
jobs:
4+
go-lint:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v4
8+
- uses: actions/setup-go@v5
9+
with:
10+
go-version: '1.24'
11+
- uses: golangci/golangci-lint-action@v8
12+
with:
13+
version: v2.1.6
14+
args: --timeout 3m --verbose

.golangci.yaml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
version: "2"
2+
run:
3+
# If set, we pass it to "go list -mod={option}". From "go help modules":
4+
# If invoked with -mod=readonly, the go command is disallowed from the implicit
5+
# automatic updating of go.mod described above. Instead, it fails when any changes
6+
# to go.mod are needed. This setting is most useful to check that go.mod does
7+
# not need updates, such as in a continuous integration and testing system.
8+
# If invoked with -mod=vendor, the go command assumes that the vendor
9+
# directory holds the correct copies of dependencies and ignores
10+
# the dependency descriptions in go.mod.
11+
#
12+
# Allowed values: readonly|vendor|mod
13+
# Default: ""
14+
modules-download-mode: readonly
15+
16+
# Include test files or not.
17+
# Default: true
18+
tests: false
19+
20+
linters:
21+
default: all
22+
23+
disable:
24+
# Not useful
25+
- depguard
26+
# Not terribly useful and ends up in too much boilerplate
27+
- exhaustruct
28+
# https://github.com/italia/developers-italia-api/issues/190)
29+
# Don't feel about chasing this one down
30+
- musttag
31+
32+
# More false positive than actual issues
33+
- mnd
34+
35+
# TODO: fix and enable these
36+
- cyclop
37+
- depguard
38+
- err113
39+
- errname
40+
- errorlint
41+
- exhaustruct
42+
- forbidigo
43+
- forcetypeassert
44+
- funlen
45+
- gochecknoglobals
46+
- gochecknoinits
47+
- gocognit
48+
- goconst
49+
- godot
50+
- godox
51+
- gosec
52+
- ireturn
53+
- lll
54+
- nestif
55+
- nlreturn
56+
- perfsprint
57+
- prealloc
58+
- recvcheck
59+
- revive
60+
- tagalign
61+
- tagliatelle
62+
- unused
63+
- varnamelen
64+
- whitespace
65+
- wrapcheck
66+
- wsl
67+
- gocritic
68+
- gocyclo
69+
- noctx
70+
- predeclared
71+
- unparam
72+
73+
settings:
74+
wrapcheck:
75+
ignore-sigs:
76+
# No point in wrapping these
77+
- func encoding/json.Marshal(v any)
78+
- func encoding/json.UnmarshalJSON(v any)
79+
80+
# Defaults
81+
- .Errorf(
82+
- errors.New(
83+
- errors.Unwrap(
84+
- .Wrap(
85+
- .Wrapf(
86+
- .WithMessage(
87+
- .WithMessagef(
88+
- .WithStack(
89+
funlen:
90+
# Increase the number of lines, considering funlen counts comments as well
91+
# (https://github.com/ultraware/funlen/issues/12)
92+
#
93+
# default: 60
94+
lines: 80
95+
usetesting:
96+
context-background: true
97+
context-todo: true
98+
os-chdir: true
99+
os-mkdir-temp: true
100+
os-setenv: true
101+
os-create-temp: true
102+
os-temp-dir: false
103+
104+
exclusions:
105+
generated: lax
106+
presets:
107+
- comments
108+
- common-false-positives
109+
- legacy
110+
- std-error-handling
111+
paths:
112+
- third_party$
113+
- builtin$
114+
- examples$
115+
116+
formatters:
117+
enable:
118+
- gci
119+
- gofmt
120+
- gofumpt
121+
- goimports
122+
exclusions:
123+
generated: lax
124+
paths:
125+
- third_party$
126+
- builtin$
127+
- examples$

errors.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ func (e ParseError) Error() string {
1616
}
1717

1818
type ValidationError struct {
19-
Key string `json:"key"`
19+
Key string `json:"key"`
2020
Description string `json:"description"`
21-
Line int `json:"line"`
22-
Column int `json:"column"`
21+
Line int `json:"line"`
22+
Column int `json:"column"`
2323
}
24+
2425
func (e ValidationError) Error() string {
2526
key := ""
2627
if e.Key != "" {
@@ -29,14 +30,15 @@ func (e ValidationError) Error() string {
2930

3031
return fmt.Sprintf("publiccode.yml:%d:%d: error: %s%s", e.Line, e.Column, key, e.Description)
3132
}
33+
3234
func (e ValidationError) MarshalJSON() ([]byte, error) {
3335
type Ve ValidationError
3436

3537
return json.Marshal(&struct {
3638
*Ve
3739
Type string `json:"type"`
38-
} {
39-
Ve: (*Ve)(&e),
40+
}{
41+
Ve: (*Ve)(&e),
4042
Type: "error",
4143
})
4244
}
@@ -46,6 +48,7 @@ func newValidationError(key string, description string, args ...interface{}) Val
4648
}
4749

4850
type ValidationWarning ValidationError
51+
4952
func (e ValidationWarning) Error() string {
5053
key := ""
5154
if e.Key != "" {
@@ -54,19 +57,21 @@ func (e ValidationWarning) Error() string {
5457

5558
return fmt.Sprintf("publiccode.yml:%d:%d: warning: %s%s", e.Line, e.Column, key, e.Description)
5659
}
60+
5761
func (e ValidationWarning) MarshalJSON() ([]byte, error) {
5862
type Ve ValidationError
5963

6064
return json.Marshal(&struct {
6165
*Ve
6266
Type string `json:"type"`
63-
} {
64-
Ve: (*Ve)(&e),
67+
}{
68+
Ve: (*Ve)(&e),
6569
Type: "warning",
6670
})
6771
}
6872

6973
type ValidationResults []error
74+
7075
func (vr ValidationResults) Error() string {
7176
var s []string
7277
for _, e := range vr {

fields.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import (
55
"net/url"
66

77
"github.com/alranel/go-vcsurl/v2"
8-
spdxValidator "github.com/kyoh86/go-spdx/spdx"
9-
108
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
9+
spdxValidator "github.com/kyoh86/go-spdx/spdx"
1110
)
1211

1312
type validateFn func(publiccode PublicCode, parser Parser, network bool) error

internal/netutil.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func downloadFile(filepath string, url *url.URL, headers map[string]string) erro
1919
if err != nil {
2020
return err
2121
}
22-
defer out.Close()
22+
defer func() {
23+
_ = out.Close()
24+
}()
2325

2426
// Get the data from the url.
2527
resp, err := httpclient.GetURL(url.String(), headers)

marshallers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package publiccode;
1+
package publiccode
22

33
import (
44
"net/url"
@@ -23,12 +23,13 @@ func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
2323
}
2424

2525
func (u *URL) MarshalYAML() (interface{}, error) {
26-
return u.String(), nil
26+
return u.String(), nil
2727
}
2828

2929
func (u URL) String() string {
30-
return (*url.URL)(&u).String()
30+
return (*url.URL)(&u).String()
3131
}
32+
3233
type UrlOrUrlArray []*URL
3334

3435
func (a *UrlOrUrlArray) UnmarshalYAML(unmarshal func(interface{}) error) error {

parser.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import (
1616

1717
"github.com/alranel/go-vcsurl/v2"
1818
"github.com/go-playground/validator/v10"
19-
"gopkg.in/yaml.v3"
20-
2119
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
2220
publiccodeValidator "github.com/italia/publiccode-parser-go/v4/validators"
21+
"gopkg.in/yaml.v3"
2322
)
2423

2524
type ParserConfig struct {
@@ -280,20 +279,22 @@ func (p *Parser) Parse(uri string) (PublicCode, error) {
280279

281280
url, err := toURL(uri)
282281
if err != nil {
283-
return nil, fmt.Errorf("Invalid URL '%s': %w", uri, err)
282+
return nil, fmt.Errorf("invalid URL '%s': %w", uri, err)
284283
}
285284

286285
if url.Scheme == "file" {
287286
stream, err = os.Open(url.Path)
288287
if err != nil {
289-
return nil, fmt.Errorf("Can't open file '%s': %w", url.Path, err)
288+
return nil, fmt.Errorf("can't open file '%s': %w", url.Path, err)
290289
}
291290
} else {
292291
resp, err := http.Get(uri)
293292
if err != nil {
294-
return nil, fmt.Errorf("Can't GET '%s': %w", uri, err)
293+
return nil, fmt.Errorf("can't GET '%s': %w", uri, err)
295294
}
296-
defer resp.Body.Close()
295+
defer func() {
296+
_ = resp.Body.Close()
297+
}()
297298

298299
stream = resp.Body
299300
}
@@ -314,7 +315,7 @@ func getNodes(key string, node *yaml.Node) (*yaml.Node, *yaml.Node) {
314315
}
315316

316317
func getPositionInFile(key string, node yaml.Node) (int, int) {
317-
var n *yaml.Node = &node
318+
n := &node
318319

319320
keys := strings.Split(key, ".")
320321
for _, path := range keys[:len(keys)-1] {
@@ -340,7 +341,7 @@ func getPositionInFile(key string, node yaml.Node) (int, int) {
340341
// getKeyAtLine returns the key name at line "line" for the YAML document
341342
// represented at parentNode.
342343
func getKeyAtLine(parentNode yaml.Node, line int, path string) string {
343-
var key = path
344+
key := path
344345

345346
for i, currNode := range parentNode.Content {
346347
// If this node is a mapping and the index is odd it means

publiccode-parser/publiccode_parser.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"runtime/debug"
1010

1111
"github.com/alranel/go-vcsurl/v2"
12-
1312
publiccode "github.com/italia/publiccode-parser-go/v4"
1413
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
1514
)
@@ -33,7 +32,7 @@ func init() {
3332

3433
func main() {
3534
flag.Usage = func() {
36-
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [ OPTIONS ] publiccode.yml\n", os.Args[0])
35+
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [ OPTIONS ] publiccode.yml\n", os.Args[0])
3736
flag.PrintDefaults()
3837
}
3938
localBasePathPtr := flag.String("path", "", "Use this local directory as base path when checking for files existence instead of using the `url` key in publiccode.yml")
@@ -54,7 +53,7 @@ func main() {
5453
return
5554
}
5655

57-
var publiccodeFile = flag.Args()[0]
56+
publiccodeFile := flag.Args()[0]
5857

5958
if ok, url := urlutil.IsValidURL(publiccodeFile); ok {
6059
// supplied argument looks like an URL
@@ -68,7 +67,7 @@ func main() {
6867
config.DisableNetwork = *disableNetworkPtr
6968

7069
p, err := publiccode.NewParser(config)
71-
if (err != nil) {
70+
if err != nil {
7271
fmt.Fprintf(os.Stderr, "Error creating Parser: %s\n", err.Error())
7372
os.Exit(1)
7473
}
@@ -82,7 +81,7 @@ func main() {
8281
}
8382

8483
out, jsonerr := json.MarshalIndent(err, "", " ")
85-
if (jsonerr != nil) {
84+
if jsonerr != nil {
8685
fmt.Fprintf(os.Stderr, "Error encoding JSON\n")
8786
os.Exit(1)
8887
}
@@ -107,7 +106,6 @@ func hasValidationErrors(results error) bool {
107106
return false
108107
}
109108

110-
111109
switch e := results.(type) {
112110
case publiccode.ValidationResults:
113111
for _, res := range e {

v0.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package publiccode
22

33
import (
4-
"gopkg.in/yaml.v3"
5-
64
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
5+
"gopkg.in/yaml.v3"
76
)
87

98
// PublicCodeV0 defines how a publiccode.yml v0.x is structured

v1.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package publiccode
22

33
import (
4-
"gopkg.in/yaml.v3"
5-
64
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
5+
"gopkg.in/yaml.v3"
76
)
87

98
// There's no v1 yet, this is just an unexported placeholder type
@@ -16,7 +15,6 @@ type publicCodeV1 struct {
1615
//nolint:unused
1716
func (p publicCodeV1) Version() uint {
1817
return 1
19-
2018
}
2119

2220
//nolint:unused

0 commit comments

Comments
 (0)