@@ -17,7 +17,20 @@ var inputJSON = false
17
17
var outputToJSON = false
18
18
19
19
func main () {
20
- var cmdRead = & cobra.Command {
20
+ var cmdRead = createReadCmd ()
21
+ var cmdWrite = createWriteCmd ()
22
+
23
+ var rootCmd = & cobra.Command {Use : "yaml" }
24
+ rootCmd .PersistentFlags ().BoolVarP (& trimOutput , "trim" , "t" , true , "trim yaml output" )
25
+ rootCmd .PersistentFlags ().BoolVarP (& outputToJSON , "tojson" , "j" , false , "output as json" )
26
+ rootCmd .PersistentFlags ().BoolVarP (& inputJSON , "fromjson" , "J" , false , "input as json" )
27
+
28
+ rootCmd .AddCommand (cmdRead , cmdWrite )
29
+ rootCmd .Execute ()
30
+ }
31
+
32
+ func createReadCmd () * cobra.Command {
33
+ return & cobra.Command {
21
34
Use : "read [yaml_file] [path]" ,
22
35
Aliases : []string {"r" },
23
36
Short : "yaml r sample.yaml a.b.c" ,
@@ -27,11 +40,13 @@ yaml r - a.b.c (reads from stdin)
27
40
yaml r things.yaml a.*.c
28
41
yaml r things.yaml a.array[0].blah
29
42
yaml r things.yaml a.array[*].blah
30
- ` ,
43
+ ` ,
31
44
Long : "Outputs the value of the given path in the yaml file to STDOUT" ,
32
45
Run : readProperty ,
33
46
}
47
+ }
34
48
49
+ func createWriteCmd () * cobra.Command {
35
50
var cmdWrite = & cobra.Command {
36
51
Use : "write [yaml_file] [path] [value]" ,
37
52
Aliases : []string {"w" },
@@ -42,7 +57,7 @@ yaml write --inplace things.yaml a.b.c cat
42
57
yaml w -i things.yaml a.b.c cat
43
58
yaml w --script update_script.yaml things.yaml
44
59
yaml w -i -s update_script.yaml things.yaml
45
- ` ,
60
+ ` ,
46
61
Long : `Updates the yaml file w.r.t the given path and value.
47
62
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
48
63
@@ -58,14 +73,7 @@ a.b.e:
58
73
}
59
74
cmdWrite .PersistentFlags ().BoolVarP (& writeInplace , "inplace" , "i" , false , "update the yaml file inplace" )
60
75
cmdWrite .PersistentFlags ().StringVarP (& writeScript , "script" , "s" , "" , "yaml script for updating yaml" )
61
-
62
- var rootCmd = & cobra.Command {Use : "yaml" }
63
- rootCmd .PersistentFlags ().BoolVarP (& trimOutput , "trim" , "t" , true , "trim yaml output" )
64
- rootCmd .PersistentFlags ().BoolVarP (& outputToJSON , "tojson" , "j" , false , "output as json" )
65
- rootCmd .PersistentFlags ().BoolVarP (& inputJSON , "fromjson" , "J" , false , "input as json" )
66
-
67
- rootCmd .AddCommand (cmdRead , cmdWrite )
68
- rootCmd .Execute ()
76
+ return cmdWrite
69
77
}
70
78
71
79
func readProperty (cmd * cobra.Command , args []string ) {
0 commit comments