-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_cmd.go
More file actions
67 lines (61 loc) · 1.5 KB
/
validate_cmd.go
File metadata and controls
67 lines (61 loc) · 1.5 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"github.com/naighes/imposter/cfg"
"github.com/naighes/imposter/functions"
)
func validateCmd() command {
fs := flag.NewFlagSet("imposter validate", flag.ExitOnError)
opts := validateOpts{}
fs.StringVar(&opts.configFile, "config-file", "stdin", "The configuration file")
fs.BoolVar(&opts.jsonEncoded, "json", false, "Enable JSON output instead of plain text")
return command{fs, func(args []string) error {
fs.Parse(args)
return validateExec(&opts)
}}
}
type validateOpts struct {
configFile string
jsonEncoded bool
}
func validateExec(opts *validateOpts) error {
var r []string
config, err := cfg.ReadConfig(opts.configFile)
if err != nil {
return fmt.Errorf("could not load configuration: %v", err)
}
var vars map[string]interface{}
if config.Vars == nil {
vars = make(map[string]interface{})
} else {
vars = config.Vars
}
defs := config.Defs
for _, def := range defs {
errors := def.Validate(functions.ParseExpression, vars)
if len(errors) > 0 {
r = append(r, errors...)
}
}
if l := len(r); l > 0 {
if opts.jsonEncoded {
rep := errorReport{Errors: r, Count: l}
bytes, _ := json.MarshalIndent(&rep, "", " ")
fmt.Printf("%s", string(bytes))
} else {
const sep = "\n--------------------\n"
fmt.Printf("found %d errors:%s", l, sep)
fmt.Printf(strings.Join(r[:], sep))
}
os.Exit(1)
}
return nil
}
type errorReport struct {
Count int `json:"count"`
Errors []string `json:"errors"`
}