Skip to content

Commit

Permalink
Return exit code based on validation result
Browse files Browse the repository at this point in the history
  • Loading branch information
koplas committed Dec 4, 2024
1 parent 57953e4 commit 938ceb8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/csaf_validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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()
Expand All @@ -70,6 +78,7 @@ func run(opts *options, files []string) error {
}
defer validator.Close()
} else {
exitCode |= exitCodeNoRemoteValidator
log.Printf("warn: no remote validator specified")
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions docs/csaf_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- ``: no remote validator configured
- ``: failure in remote validation

### Usage

```
Expand Down

0 comments on commit 938ceb8

Please sign in to comment.