mirror of
https://github.com/vbatts/go-mtree.git
synced 2024-11-22 16:25:38 +00:00
Merge pull request #31 from cyphar/alternate-formats
cmd: add alternate formats
This commit is contained in:
commit
e6bdb36de3
2 changed files with 51 additions and 7 deletions
11
check.go
11
check.go
|
@ -9,15 +9,16 @@ import (
|
||||||
|
|
||||||
// Result of a Check
|
// Result of a Check
|
||||||
type Result struct {
|
type Result struct {
|
||||||
Failures []Failure // list of any failures in the Check
|
// list of any failures in the Check
|
||||||
|
Failures []Failure `json:"failures"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Failure of a particular keyword for a path
|
// Failure of a particular keyword for a path
|
||||||
type Failure struct {
|
type Failure struct {
|
||||||
Path string
|
Path string `json:"path"`
|
||||||
Keyword string
|
Keyword string `json:"keyword"`
|
||||||
Expected string
|
Expected string `json:"expected"`
|
||||||
Got string
|
Got string `json:"got"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a "pretty" formatting for a Failure
|
// String returns a "pretty" formatting for a Failure
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -17,8 +19,38 @@ var (
|
||||||
flAddKeywords = flag.String("K", "", "Add the specified (delimited by comma or space) keywords to the current set of keywords")
|
flAddKeywords = flag.String("K", "", "Add the specified (delimited by comma or space) keywords to the current set of keywords")
|
||||||
flUseKeywords = flag.String("k", "", "Use the specified (delimited by comma or space) keywords as the current set of keywords")
|
flUseKeywords = flag.String("k", "", "Use the specified (delimited by comma or space) keywords as the current set of keywords")
|
||||||
flListKeywords = flag.Bool("list-keywords", false, "List the keywords available")
|
flListKeywords = flag.Bool("list-keywords", false, "List the keywords available")
|
||||||
|
flResultFormat = flag.String("result-format", "bsd", "output the validation results using the given format (bsd, json, path)")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var formats = map[string]func(*mtree.Result) string{
|
||||||
|
// Outputs the errors in the BSD format.
|
||||||
|
"bsd": func(r *mtree.Result) string {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
for _, fail := range r.Failures {
|
||||||
|
fmt.Fprintln(&buffer, fail)
|
||||||
|
}
|
||||||
|
return buffer.String()
|
||||||
|
},
|
||||||
|
|
||||||
|
// Outputs the full result struct in JSON.
|
||||||
|
"json": func(r *mtree.Result) string {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
if err := json.NewEncoder(&buffer).Encode(r); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return buffer.String()
|
||||||
|
},
|
||||||
|
|
||||||
|
// Outputs only the paths which failed to validate.
|
||||||
|
"path": func(r *mtree.Result) string {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
for _, fail := range r.Failures {
|
||||||
|
fmt.Fprintln(&buffer, fail.Path)
|
||||||
|
}
|
||||||
|
return buffer.String()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -43,6 +75,14 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --output
|
||||||
|
formatFunc, ok := formats[*flResultFormat]
|
||||||
|
if !ok {
|
||||||
|
log.Printf("invalid output format: %s", *flResultFormat)
|
||||||
|
isErr = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var currentKeywords []string
|
var currentKeywords []string
|
||||||
// -k <keywords>
|
// -k <keywords>
|
||||||
if *flUseKeywords != "" {
|
if *flUseKeywords != "" {
|
||||||
|
@ -103,8 +143,11 @@ func main() {
|
||||||
}
|
}
|
||||||
if res != nil && len(res.Failures) > 0 {
|
if res != nil && len(res.Failures) > 0 {
|
||||||
defer os.Exit(1)
|
defer os.Exit(1)
|
||||||
for _, failure := range res.Failures {
|
out := formatFunc(res)
|
||||||
fmt.Println(failure)
|
if _, err := os.Stdout.Write([]byte(out)); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
isErr = true
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue