forked from karuppiah7890/helm-schema-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
53 lines (46 loc) · 1.26 KB
/
root.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"github.com/karuppiah7890/go-jsonschema-generator"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "helm schema-gen <values-yaml-file>",
SilenceUsage: true,
SilenceErrors: true,
Short: "Helm plugin to generate json schema for values yaml",
Long: `Helm plugin to generate json schema for values yaml
Examples:
$ helm schema-gen values.yaml # generate schema json
`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("pass one values yaml file")
}
if len(args) != 1 {
return fmt.Errorf("schema can be generated only for one values yaml at once")
}
valuesFilePath := args[0]
values := make(map[string]interface{})
valuesFileData, err := ioutil.ReadFile(valuesFilePath)
if err != nil {
return fmt.Errorf("error when reading file '%s': %v", valuesFilePath, err)
}
err = yaml.Unmarshal(valuesFileData, &values)
s := &jsonschema.Document{}
s.ReadDeep(&values)
fmt.Println(s)
return nil
},
}
// Execute executes the root command
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}