Repository navigation
Expand file tree
/
Copy pathpubliccode_parser.go
More file actions
175 lines (137 loc) · 3.79 KB
/
Copy pathpubliccode_parser.go
File metadata and controls
175 lines (137 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"runtime/debug"
publiccode "github.com/publiccodeyml/libpubliccode/v5"
)
var version, date = buildVersion()
// buildVersion returns the module version and the commit date recorded in the
// build information of the running binary.
func buildVersion() (string, string) {
version, date := "devel", "(latest)"
info, ok := debug.ReadBuildInfo()
if !ok {
return version, date
}
if info.Main.Version != "" {
version = info.Main.Version
}
for _, setting := range info.Settings {
if setting.Key == "vcs.time" {
date = setting.Value
}
}
return version, date
}
func main() {
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
}
func run(args []string, stdout, stderr io.Writer) int {
flags := flag.NewFlagSet("publiccode-parser", flag.ContinueOnError)
flags.SetOutput(stderr)
flags.Usage = func() {
_, _ = fmt.Fprintf(flags.Output(), "Usage: %s [ OPTIONS ] publiccode.yml\n", flags.Name())
flags.PrintDefaults()
}
localBasePathPtr := flags.String(
"path", "",
"Use this local directory as base path when checking for files existence "+
"instead of using the `url` key in publiccode.yml",
)
disableNetworkPtr := flags.Bool(
"no-network", false,
"Disables checks that require network connections (URL existence and oEmbed). This makes validation much faster.",
)
disableExternalChecksPtr := flags.Bool(
"no-external-checks", false,
"Disables ALL checks that reference external resources such as remote URLs or local file existence. "+
"Implies --no-network",
)
timeoutPtr := flags.Duration(
"timeout", 0,
"Timeout for each HTTP request during external checks (e.g. 10s, 1m). "+
"Defaults to 30s if not set. No effect with --no-network or --no-external-checks.",
)
jsonOutputPtr := flags.Bool("json", false, "Output the validation errors as a JSON list.")
helpPtr := flags.Bool("help", false, "Display command line usage.")
versionPtr := flags.Bool("version", false, "Display current software version.")
if err := flags.Parse(args); err != nil {
return 2
}
if *versionPtr {
fmt.Fprintln(stdout, version, date)
return 0
}
if *helpPtr {
flags.SetOutput(stdout)
flags.Usage()
return 0
}
if flags.NArg() < 1 {
flags.Usage()
return 2
}
config := publiccode.ParserConfig{BaseURL: *localBasePathPtr}
config.DisableNetwork = *disableNetworkPtr
config.DisableExternalChecks = *disableExternalChecksPtr
config.Timeout = *timeoutPtr
p, err := publiccode.NewParser(config)
if err != nil {
fmt.Fprintf(stderr, "Error creating Parser: %s\n", err.Error())
return 1
}
_, parseErr := p.Parse(flags.Arg(0))
if *jsonOutputPtr {
return reportJSON(parseErr, stdout, stderr)
}
if parseErr != nil {
fmt.Fprintln(stdout, parseErr)
}
if hasValidationErrors(parseErr) {
return 1
}
return 0
}
func reportJSON(parseErr error, stdout, stderr io.Writer) int {
if parseErr == nil {
fmt.Fprintln(stdout, "[]")
return 0
}
// Failures that are not validation results, such as an unreadable file, carry
// unexported fields only and would marshal to an empty JSON object.
var results publiccode.ValidationResults
if !errors.As(parseErr, &results) {
results = publiccode.ValidationResults{publiccode.ValidationError{Description: parseErr.Error()}}
}
out, jsonerr := json.MarshalIndent(results, "", " ")
if jsonerr != nil {
fmt.Fprintf(stderr, "Error encoding JSON\n")
return 1
}
fmt.Fprintln(stdout, string(out))
if hasValidationErrors(parseErr) {
return 1
}
return 0
}
func hasValidationErrors(results error) bool {
if results == nil {
return false
}
var vr publiccode.ValidationResults
if errors.As(results, &vr) {
for _, res := range vr {
var ve publiccode.ValidationError
if errors.As(res, &ve) {
return true
}
}
return false
}
return true
}