@@ -12,6 +12,7 @@ import (
12
12
13
13
var trimOutput = true
14
14
var writeInplace = false
15
+ var writeScript = ""
15
16
16
17
func main () {
17
18
var cmdRead = & cobra.Command {
@@ -43,6 +44,7 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
43
44
Run : writeProperty ,
44
45
}
45
46
cmdWrite .PersistentFlags ().BoolVarP (& writeInplace , "inplace" , "i" , false , "update the yaml file inplace" )
47
+ cmdWrite .PersistentFlags ().StringVarP (& writeScript , "script" , "s" , "" , "yaml script for updating yaml" )
46
48
47
49
var rootCmd = & cobra.Command {Use : "yaml" }
48
50
rootCmd .PersistentFlags ().BoolVarP (& trimOutput , "trim" , "t" , true , "trim yaml output" )
@@ -53,7 +55,7 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
53
55
func readProperty (cmd * cobra.Command , args []string ) {
54
56
var parsedData map [interface {}]interface {}
55
57
56
- readYaml (args , & parsedData )
58
+ readYaml (args [ 0 ] , & parsedData )
57
59
58
60
if len (args ) == 1 {
59
61
printYaml (parsedData )
@@ -66,24 +68,30 @@ func readProperty(cmd *cobra.Command, args []string) {
66
68
}
67
69
68
70
func writeProperty (cmd * cobra.Command , args []string ) {
69
- if len (args ) < 3 {
71
+ var writeCommands map [string ]interface {}
72
+ if writeScript != "" {
73
+ readYaml (writeScript , & writeCommands )
74
+ } else if len (args ) < 3 {
70
75
die ("Must provide <filename> <path_to_update> <value>" )
76
+ } else {
77
+ writeCommands [args [1 ]] = parseValue (args [2 ])
71
78
}
72
79
73
80
var parsedData map [interface {}]interface {}
74
- readYaml (args , & parsedData )
75
-
76
- var paths = parsePath (args [1 ])
81
+ readYaml (args [0 ], & parsedData )
77
82
78
- write (parsedData , paths [0 ], paths [1 :len (paths )], getValue (args [2 ]))
83
+ for path , value := range writeCommands {
84
+ var paths = parsePath (path )
85
+ write (parsedData , paths [0 ], paths [1 :len (paths )], value )
86
+ }
79
87
80
88
if writeInplace {
81
89
ioutil .WriteFile (args [0 ], []byte (yamlToString (parsedData )), 0644 )
82
90
} else {
83
91
printYaml (parsedData )
84
92
}
85
93
}
86
- func getValue (argument string ) interface {} {
94
+ func parseValue (argument string ) interface {} {
87
95
var value , err interface {}
88
96
var inQuotes = argument [0 ] == '"'
89
97
if ! inQuotes {
@@ -118,19 +126,19 @@ func yamlToString(context interface{}) string {
118
126
return outStr
119
127
}
120
128
121
- func readYaml (args [] string , parsedData * map [ interface {}] interface {}) {
122
- if len ( args ) == 0 {
129
+ func readYaml (filename string , parsedData interface {}) {
130
+ if filename == "" {
123
131
die ("Must provide filename" )
124
132
}
125
133
126
134
var rawData []byte
127
- if args [ 0 ] == "-" {
135
+ if filename == "-" {
128
136
rawData = readStdin ()
129
137
} else {
130
- rawData = readFile (args [ 0 ] )
138
+ rawData = readFile (filename )
131
139
}
132
140
133
- err := yaml .Unmarshal ([]byte (rawData ), & parsedData )
141
+ err := yaml .Unmarshal ([]byte (rawData ), parsedData )
134
142
if err != nil {
135
143
die ("error: %v" , err )
136
144
}
0 commit comments