@@ -30,13 +30,14 @@ func main() {
30
30
31
31
var cmdRead = createReadCmd ()
32
32
var cmdWrite = createWriteCmd ()
33
+ var cmdNew = createNewCmd ()
33
34
34
35
var rootCmd = & cobra.Command {Use : "yaml" }
35
36
rootCmd .PersistentFlags ().BoolVarP (& trimOutput , "trim" , "t" , true , "trim yaml output" )
36
37
rootCmd .PersistentFlags ().BoolVarP (& outputToJSON , "tojson" , "j" , false , "output as json" )
37
38
rootCmd .PersistentFlags ().BoolVarP (& inputJSON , "fromjson" , "J" , false , "input as json" )
38
39
rootCmd .PersistentFlags ().BoolVarP (& verbose , "verbose" , "v" , false , "verbose mode" )
39
- rootCmd .AddCommand (cmdRead , cmdWrite )
40
+ rootCmd .AddCommand (cmdRead , cmdWrite , cmdNew )
40
41
rootCmd .Execute ()
41
42
}
42
43
@@ -87,6 +88,28 @@ a.b.e:
87
88
return cmdWrite
88
89
}
89
90
91
+ func createNewCmd () * cobra.Command {
92
+ var cmdNew = & cobra.Command {
93
+ Use : "new [path] [value]" ,
94
+ Aliases : []string {"n" },
95
+ Short : "yaml n [--script/-s script_file] a.b.c newValueForC" ,
96
+ Example : `
97
+ yaml new a.b.c cat
98
+ yaml n a.b.c cat
99
+ yaml n --script create_script.yaml
100
+ ` ,
101
+ Long : `Creates a new yaml w.r.t the given path and value.
102
+ Outputs to STDOUT
103
+
104
+ Create Scripts:
105
+ Note that you can give a create script to perform more sophisticated yaml. This follows the same format as the update script.
106
+ ` ,
107
+ Run : newProperty ,
108
+ }
109
+ cmdNew .PersistentFlags ().StringVarP (& writeScript , "script" , "s" , "" , "yaml script for updating yaml" )
110
+ return cmdNew
111
+ }
112
+
90
113
func readProperty (cmd * cobra.Command , args []string ) {
91
114
if verbose {
92
115
backend .SetLevel (logging .DEBUG , "" )
@@ -108,6 +131,35 @@ func read(args []string) interface{} {
108
131
return readMap (parsedData , paths [0 ], paths [1 :len (paths )])
109
132
}
110
133
134
+ func newProperty (cmd * cobra.Command , args []string ) {
135
+ if verbose {
136
+ backend .SetLevel (logging .DEBUG , "" )
137
+ }
138
+ updatedData := newYaml (args )
139
+ print (updatedData )
140
+ }
141
+
142
+ func newYaml (args []string ) interface {} {
143
+ var writeCommands map [string ]interface {}
144
+ if writeScript != "" {
145
+ readData (writeScript , & writeCommands , false )
146
+ } else if len (args ) < 2 {
147
+ die ("Must provide <path_to_update> <value>" )
148
+ } else {
149
+ writeCommands = make (map [string ]interface {})
150
+ writeCommands [args [0 ]] = parseValue (args [1 ])
151
+ }
152
+
153
+ parsedData := make (yaml.MapSlice , 0 )
154
+
155
+ for path , value := range writeCommands {
156
+ var paths = parsePath (path )
157
+ parsedData = writeMap (parsedData , paths , value )
158
+ }
159
+
160
+ return parsedData
161
+ }
162
+
111
163
func writeProperty (cmd * cobra.Command , args []string ) {
112
164
if verbose {
113
165
backend .SetLevel (logging .DEBUG , "" )
0 commit comments