Skip to content

Commit 949253e

Browse files
committed
feat: add ParserConfig.DisableExternalChecks
1 parent abf0617 commit 949253e

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

fields.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
5050
if publiccodev0.Logo != "" {
5151
if _, err := isRelativePathOrURL(publiccodev0.Logo, "logo"); err != nil {
5252
vr = append(vr, err)
53-
} else if validLogo, err := parser.validLogo(toCodeHostingURL(publiccodev0.Logo, parser.currentBaseURL), network); !validLogo {
54-
vr = append(vr, newValidationError("logo", err.Error()))
53+
} else if !parser.disableExternalChecks {
54+
validLogo, err := parser.validLogo(toCodeHostingURL(publiccodev0.Logo, parser.currentBaseURL), network)
55+
if !validLogo {
56+
vr = append(vr, newValidationError("logo", err.Error()))
57+
}
5558
}
5659
}
5760

@@ -60,8 +63,11 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
6063

6164
if _, err := isRelativePathOrURL(publiccodev0.MonochromeLogo, "monochromeLogo"); err != nil {
6265
vr = append(vr, err)
63-
} else if validLogo, err := parser.validLogo(toCodeHostingURL(publiccodev0.MonochromeLogo, parser.currentBaseURL), network); !validLogo {
64-
vr = append(vr, newValidationError("monochromeLogo", err.Error()))
66+
} else if !parser.disableExternalChecks {
67+
validLogo, err := parser.validLogo(toCodeHostingURL(publiccodev0.MonochromeLogo, parser.currentBaseURL), network)
68+
if !validLogo {
69+
vr = append(vr, newValidationError("monochromeLogo", err.Error()))
70+
}
6571
}
6672
}
6773

@@ -70,10 +76,13 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
7076

7177
if _, err := isRelativePathOrURL(*publiccodev0.Legal.AuthorsFile, "legal.authorsFile"); err != nil {
7278
vr = append(vr, err)
73-
} else if exists, err := parser.fileExists(toCodeHostingURL(*publiccodev0.Legal.AuthorsFile, parser.currentBaseURL), network); !exists {
74-
u := toCodeHostingURL(*publiccodev0.Legal.AuthorsFile, parser.currentBaseURL)
79+
} else if !parser.disableExternalChecks {
80+
exists, err := parser.fileExists(toCodeHostingURL(*publiccodev0.Legal.AuthorsFile, parser.currentBaseURL), network)
81+
if !exists {
82+
u := toCodeHostingURL(*publiccodev0.Legal.AuthorsFile, parser.currentBaseURL)
7583

76-
vr = append(vr, newValidationError("legal.authorsFile", "'%s' does not exist: %s", urlutil.DisplayURL(&u), err.Error()))
84+
vr = append(vr, newValidationError("legal.authorsFile", "'%s' does not exist: %s", urlutil.DisplayURL(&u), err.Error()))
85+
}
7786
}
7887
}
7988

@@ -97,7 +106,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
97106
})
98107
}
99108

100-
if network && desc.Documentation != nil {
109+
if !parser.disableExternalChecks && network && desc.Documentation != nil {
101110
if reachable, err := parser.isReachable(*(*url.URL)(desc.Documentation), network); !reachable {
102111
vr = append(vr, newValidationError(
103112
fmt.Sprintf("description.%s.documentation", lang),
@@ -106,7 +115,7 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
106115
}
107116
}
108117

109-
if network && desc.APIDocumentation != nil {
118+
if !parser.disableExternalChecks && network && desc.APIDocumentation != nil {
110119
if reachable, err := parser.isReachable(*(*url.URL)(desc.APIDocumentation), network); !reachable {
111120
vr = append(vr, newValidationError(
112121
fmt.Sprintf("description.%s.apiDocumentation", lang),
@@ -119,11 +128,14 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
119128
keyName := fmt.Sprintf("description.%s.screenshots[%d]", lang, i)
120129
if _, err := isRelativePathOrURL(v, keyName); err != nil {
121130
vr = append(vr, err)
122-
} else if isImage, err := parser.isImageFile(toCodeHostingURL(v, parser.currentBaseURL), network); !isImage {
123-
vr = append(vr, newValidationError(
124-
keyName,
125-
"'%s' is not an image: %s", v, err.Error(),
126-
))
131+
} else if !parser.disableExternalChecks {
132+
isImage, err := parser.isImageFile(toCodeHostingURL(v, parser.currentBaseURL), network)
133+
if !isImage {
134+
vr = append(vr, newValidationError(
135+
keyName,
136+
"'%s' is not an image: %s", v, err.Error(),
137+
))
138+
}
127139
}
128140
}
129141

parser.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@ import (
2626
)
2727

2828
type ParserConfig struct {
29-
// DisableNetwork disables all network tests (URL existence and Oembed). This
29+
// DisableNetwork disables all network tests (eg. URL existence). This
3030
// results in much faster parsing.
3131
DisableNetwork bool
3232

33+
// DisableExternalChecks disables ALL the additional checks on external files
34+
// and resources, local or remote (eg. existence, images actually being images, etc.).
35+
//
36+
// It implies DisableNetwork = true.
37+
DisableExternalChecks bool
38+
3339
// Domain will have domain specific settings, including basic auth if provided
3440
// this will avoid strong quota limit imposed by code hosting platform
3541
Domain Domain
@@ -45,11 +51,12 @@ type ParserConfig struct {
4551

4652
// Parser is a helper class for parsing publiccode.yml files.
4753
type Parser struct {
48-
disableNetwork bool
49-
domain Domain
50-
branch string
51-
baseURL *url.URL
52-
fileURL *url.URL
54+
disableNetwork bool
55+
disableExternalChecks bool
56+
domain Domain
57+
branch string
58+
baseURL *url.URL
59+
fileURL *url.URL
5360

5461
// This is the baseURL we'll try to compute and use between
5562
// Parse{,Stream)() calls.
@@ -70,10 +77,15 @@ type Domain struct {
7077
// NewParser initializes and returns a new Parser object following the settings in
7178
// ParserConfig.
7279
func NewParser(config ParserConfig) (*Parser, error) {
80+
if config.DisableExternalChecks {
81+
config.DisableNetwork = true
82+
}
83+
7384
parser := Parser{
74-
disableNetwork: config.DisableNetwork,
75-
domain: config.Domain,
76-
branch: config.Branch,
85+
disableNetwork: config.DisableNetwork,
86+
disableExternalChecks: config.DisableExternalChecks,
87+
domain: config.Domain,
88+
branch: config.Branch,
7789
}
7890

7991
if config.BaseURL != "" {

publiccode-parser/publiccode_parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func main() {
3737
}
3838
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")
3939
disableNetworkPtr := flag.Bool("no-network", false, "Disables checks that require network connections (URL existence and oEmbed). This makes validation much faster.")
40+
disableExternalChecksPtr := flag.Bool("no-external-checks", false, "Disables ALL checks that reference external resources such as remote URLs or local file existence. Implies --no-network")
4041
_ = flag.String("export", "", "(DEPRECATED) Provided for backward compatibility only")
4142
jsonOutputPtr := flag.Bool("json", false, "Output the validation errors as a JSON list.")
4243
helpPtr := flag.Bool("help", false, "Display command line usage.")
@@ -67,6 +68,7 @@ func main() {
6768

6869
config := publiccode.ParserConfig{BaseURL: *localBasePathPtr}
6970
config.DisableNetwork = *disableNetworkPtr
71+
config.DisableExternalChecks = *disableExternalChecksPtr
7072

7173
p, err := publiccode.NewParser(config)
7274
if err != nil {

0 commit comments

Comments
 (0)