1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Return exit code based on validation result

This commit is contained in:
koplas 2024-12-04 13:53:56 +01:00
parent 57953e495f
commit 938ceb872a
2 changed files with 20 additions and 0 deletions

View file

@ -22,6 +22,13 @@ import (
"github.com/gocsaf/csaf/v3/util"
)
const (
exitCodeAllValid = 0
exitCodeSchemaInvalid = 1 << 0
exitCodeNoRemoteValidator = 1 << 1
exitCodeFailedRemoteValidation = 1 << 2
)
type options struct {
Version bool `long:"version" description:"Display version of the binary"`
RemoteValidator string `long:"validator" description:"URL to validate documents remotely" value-name:"URL"`
@ -53,6 +60,7 @@ func main() {
// run validates the given files.
func run(opts *options, files []string) error {
exitCode := exitCodeAllValid
var validator csaf.RemoteValidator
eval := util.NewPathEval()
@ -70,6 +78,7 @@ func run(opts *options, files []string) error {
}
defer validator.Close()
} else {
exitCode |= exitCodeNoRemoteValidator
log.Printf("warn: no remote validator specified")
}
@ -106,6 +115,7 @@ func run(opts *options, files []string) error {
}
if len(validationErrs) > 0 {
exitCode |= exitCodeSchemaInvalid
fmt.Printf("schema validation errors of %q\n", file)
for _, vErr := range validationErrs {
fmt.Printf(" * %s\n", vErr)
@ -132,12 +142,15 @@ func run(opts *options, files []string) error {
if rvr.Valid {
passes = "passes"
} else {
exitCode |= exitCodeFailedRemoteValidation
passes = "does not pass"
}
fmt.Printf("%q %s remote validation.\n", file, passes)
}
}
// Exit code is based on validation results
os.Exit(exitCodeAllValid)
return nil
}

View file

@ -2,6 +2,13 @@
is a tool to validate local advisories files against the JSON Schema and an optional remote validator.
### Exit codes
If no fatal error occurs the program will exit with the following codes:
- `0`: all valid
- `2⁰`: schema invalid
- `2¹`: no remote validator configured
- `2²`: failure in remote validation
### Usage
```