mirror of
https://github.com/vbatts/go-mtree.git
synced 2024-11-22 16:25:38 +00:00
cmd: add --result-format
This allows callers to deal with multiple output formats and not require string parsing in order to understand what the error was. The default format is "bsd". Signed-off-by: Aleksa Sarai <asarai@suse.de>
This commit is contained in:
parent
20279bacf2
commit
8cf7253132
1 changed files with 26 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -17,8 +18,20 @@ 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()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -43,6 +56,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 +124,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