-
Notifications
You must be signed in to change notification settings - Fork 30
/
validate.go
68 lines (55 loc) · 1.57 KB
/
validate.go
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
68
package main
import (
"fmt"
"log"
"github.com/spf13/cobra"
"github.com/jkcfg/jk/pkg/std"
)
var validateCmd = &cobra.Command{
Use: "validate <file>...",
Example: validateExamples,
Short: "Validate configuration files",
Args: validateArgs,
Run: validate,
}
const validateExamples = `
validating YAML files using a module's default export
jk validate -m '@example.com/validate' *.{yaml,yml}
validating YAMLs and JSONs using a local script's default export
jk validate ./valid.js *.{yaml,yml,json}
validating a specific file with an inline validation function
jk validate -c 'v => v.name === "correctName"' config.json
`
var validateOptions struct {
vmOptions
scriptOptions
}
func init() {
initScriptFlags(validateCmd, &validateOptions.scriptOptions)
initExecFlags(validateCmd, &validateOptions.vmOptions)
jk.AddCommand(validateCmd)
}
func validateArgs(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("validate requires a script and at least one file")
}
return nil
}
func validate(cmd *cobra.Command, args []string) {
vm := newVM(&validateOptions.vmOptions, ".")
inputs := make(map[string]interface{})
for _, f := range args[1:] {
inputs[f] = f
}
vm.parameters.Set("jk.validate.input", inputs)
var module string
switch {
case validateOptions.inline:
module = fmt.Sprintf(string(std.Module("cmd/validate-exec.js")), args[0])
default:
module = fmt.Sprintf(string(std.Module("cmd/validate-module.js")), args[0])
}
if err := vm.Run("@jkcfg/std/cmd/<validate>", module); err != nil {
log.Fatal(err)
}
}