Skip to content

Commit 15f878e

Browse files
authored
refactor: enable more linters (italia#216)
1 parent baa5319 commit 15f878e

File tree

13 files changed

+85
-112
lines changed

13 files changed

+85
-112
lines changed

.golangci.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ linters:
3434

3535
# TODO: fix and enable these
3636
- cyclop
37-
- depguard
3837
- err113
3938
- errname
4039
- errorlint
41-
- exhaustruct
4240
- forbidigo
4341
- forcetypeassert
4442
- funlen
@@ -52,23 +50,15 @@ linters:
5250
- ireturn
5351
- lll
5452
- nestif
55-
- nlreturn
5653
- perfsprint
57-
- prealloc
5854
- recvcheck
5955
- revive
60-
- tagalign
6156
- tagliatelle
62-
- unused
6357
- varnamelen
64-
- whitespace
6558
- wrapcheck
66-
- wsl
6759
- gocritic
6860
- gocyclo
6961
- noctx
70-
- predeclared
71-
- unparam
7262

7363
settings:
7464
wrapcheck:

errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ func (e ValidationWarning) MarshalJSON() ([]byte, error) {
7373
type ValidationResults []error
7474

7575
func (vr ValidationResults) Error() string {
76-
var s []string
76+
s := make([]string, 0, len(vr))
7777
for _, e := range vr {
7878
s = append(s, e.Error())
7979
}
80+
8081
return strings.Join(s, "\n")
8182
}

fields.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
2222
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.URL), network); !reachable {
2323
vr = append(vr, newValidationError("url", "'%s' not reachable: %s", publiccodev0.URL, err.Error()))
2424
}
25+
2526
if !vcsurl.IsRepo((*url.URL)(publiccodev0.URL)) {
2627
vr = append(vr, newValidationError("url", "is not a valid code repository"))
2728
}
@@ -46,14 +47,15 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
4647
}
4748

4849
if publiccodev0.Logo != "" {
49-
if validLogo, err := parser.validLogo(toCodeHostingURL(publiccodev0.Logo, parser.baseURL), parser, network); !validLogo {
50+
if validLogo, err := parser.validLogo(toCodeHostingURL(publiccodev0.Logo, parser.baseURL), network); !validLogo {
5051
vr = append(vr, newValidationError("logo", err.Error()))
5152
}
5253
}
54+
5355
if publiccodev0.MonochromeLogo != "" {
5456
vr = append(vr, ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future", 0, 0})
5557

56-
if validLogo, err := parser.validLogo(toCodeHostingURL(publiccodev0.MonochromeLogo, parser.baseURL), parser, network); !validLogo {
58+
if validLogo, err := parser.validLogo(toCodeHostingURL(publiccodev0.MonochromeLogo, parser.baseURL), network); !validLogo {
5759
vr = append(vr, newValidationError("monochromeLogo", err.Error()))
5860
}
5961
}
@@ -71,6 +73,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
7173
if len(publiccodev0.InputTypes) > 0 {
7274
vr = append(vr, ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future", 0, 0})
7375
}
76+
7477
for i, mimeType := range publiccodev0.InputTypes {
7578
if !isMIME(mimeType) {
7679
vr = append(vr, newValidationError(
@@ -82,6 +85,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
8285
if len(publiccodev0.OutputTypes) > 0 {
8386
vr = append(vr, ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future", 0, 0})
8487
}
88+
8589
for i, mimeType := range publiccodev0.OutputTypes {
8690
if !isMIME(mimeType) {
8791
vr = append(vr, newValidationError(
@@ -110,6 +114,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
110114
))
111115
}
112116
}
117+
113118
if network && desc.APIDocumentation != nil {
114119
if reachable, err := parser.isReachable(*(*url.URL)(desc.APIDocumentation), network); !reachable {
115120
vr = append(vr, newValidationError(
@@ -127,6 +132,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
127132
))
128133
}
129134
}
135+
130136
for i, v := range desc.Videos {
131137
_, err := parser.isOEmbedURL((*url.URL)(v))
132138
if err != nil {

internal/netutil.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func downloadFile(filepath string, url *url.URL, headers map[string]string) erro
1919
if err != nil {
2020
return err
2121
}
22+
2223
defer func() {
2324
_ = out.Close()
2425
}()
@@ -47,6 +48,7 @@ func DownloadTmpFile(url *url.URL, headers map[string]string) (string, error) {
4748

4849
// Download the file in the temp dir.
4950
tmpFile := filepath.Join(tmpdir, path.Base(url.Path))
51+
5052
err = downloadFile(tmpFile, url, headers)
5153
if err != nil {
5254
return "", err

marshallers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
1717
if err != nil {
1818
return err
1919
}
20+
2021
*u = (URL)(*urlp)
2122

2223
return nil
@@ -34,13 +35,16 @@ type UrlOrUrlArray []*URL
3435

3536
func (a *UrlOrUrlArray) UnmarshalYAML(unmarshal func(interface{}) error) error {
3637
var multi []*URL
38+
3739
err := unmarshal(&multi)
3840
if err != nil {
3941
var single *URL
42+
4043
err := unmarshal(&single)
4144
if err != nil {
4245
return err
4346
}
47+
4448
*a = []*URL{single}
4549
} else {
4650
*a = multi

parser.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
111111
if version == nil {
112112
return nil, ValidationResults{newValidationError("publiccodeYmlVersion", "publiccodeYmlVersion is a required field")}
113113
}
114+
114115
if version.ShortTag() != "!!str" {
115116
line, column := getPositionInFile("publiccodeYmlVersion", node)
116117

@@ -152,7 +153,9 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
152153
}
153154

154155
var publiccode PublicCode
156+
155157
var validateFields validateFn
158+
156159
var decodeResults ValidationResults
157160

158161
if version.Value[0] == '0' {
@@ -276,6 +279,7 @@ func (p *Parser) Parse(uri string) (PublicCode, error) {
276279
if err != nil {
277280
return nil, fmt.Errorf("can't GET '%s': %w", uri, err)
278281
}
282+
279283
defer func() {
280284
_ = resp.Body.Close()
281285
}()
@@ -394,6 +398,7 @@ func decode[T any](data []byte, publiccode *T, node yaml.Node) ValidationResults
394398

395399
d := yaml.NewDecoder(bytes.NewReader(data))
396400
d.KnownFields(true)
401+
397402
if err := d.Decode(&publiccode); err != nil {
398403
switch err := err.(type) {
399404
case *yaml.TypeError:

publiccode-parser/publiccode_parser.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"flag"
66
"fmt"
7-
"net/url"
87
"os"
98
"runtime/debug"
109

@@ -25,6 +24,7 @@ func init() {
2524
version = info.Main.Version
2625
}
2726
}
27+
2828
if date == "" {
2929
date = "(latest)"
3030
}
@@ -45,11 +45,13 @@ func main() {
4545

4646
if *versionPtr {
4747
println(version, date)
48+
4849
return
4950
}
5051

5152
if *helpPtr || len(flag.Args()) < 1 {
5253
flag.Usage()
54+
5355
return
5456
}
5557

@@ -93,6 +95,7 @@ func main() {
9395
if err != nil {
9496
fmt.Println(err)
9597
}
98+
9699
if hasValidationErrors(err) {
97100
os.Exit(1)
98101
}
@@ -120,26 +123,3 @@ func hasValidationErrors(results error) bool {
120123

121124
return false
122125
}
123-
124-
// isValidURL tests a string to determine if it is a well-structured url or not.
125-
func isValidURL(toTest string) (bool, *url.URL) {
126-
_, err := url.ParseRequestURI(toTest)
127-
if err != nil {
128-
return false, nil
129-
}
130-
131-
u, err := url.Parse(toTest)
132-
if err != nil || u.Scheme == "" || u.Host == "" {
133-
return false, nil
134-
}
135-
136-
// Check it's an acceptable scheme
137-
switch u.Scheme {
138-
case "http":
139-
case "https":
140-
default:
141-
return false, nil
142-
}
143-
144-
return true, u
145-
}

publiccode-parser/publiccode_parser_test.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)