Skip to content

Commit 15f51d4

Browse files
author
mfarah
committed
Can update a yaml file from an instruction yaml file
1 parent 219105f commit 15f51d4

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,31 @@ then
132132
yaml w -i sample.yaml b.c cat
133133
```
134134
will update the sample.yaml file so that the value of 'c' is cat.
135+
136+
137+
### Updating multiple values with a script
138+
Given a sample.yaml file of:
139+
```yaml
140+
b:
141+
c: 2
142+
e:
143+
- name: Billy Bob
144+
```
145+
and a script update_instructions.yaml of:
146+
```yaml
147+
b.c: 3
148+
b.e[0].name: Howdy Partner
149+
```
150+
then
151+
152+
```bash
153+
yaml -w -s update_instructions.yaml sample.yaml
154+
```
155+
will output:
156+
```yaml
157+
b:
158+
c: 3
159+
e:
160+
- name: Howdy Partner
161+
```
162+

instruction_sample.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
b.c: cat
2+
b.e[0].name: Mike Farah

yaml.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
var trimOutput = true
1414
var writeInplace = false
15+
var writeScript = ""
1516

1617
func main() {
1718
var cmdRead = &cobra.Command{
@@ -43,6 +44,7 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
4344
Run: writeProperty,
4445
}
4546
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
47+
cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
4648

4749
var rootCmd = &cobra.Command{Use: "yaml"}
4850
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
5355
func readProperty(cmd *cobra.Command, args []string) {
5456
var parsedData map[interface{}]interface{}
5557

56-
readYaml(args, &parsedData)
58+
readYaml(args[0], &parsedData)
5759

5860
if len(args) == 1 {
5961
printYaml(parsedData)
@@ -66,24 +68,30 @@ func readProperty(cmd *cobra.Command, args []string) {
6668
}
6769

6870
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 {
7075
die("Must provide <filename> <path_to_update> <value>")
76+
} else {
77+
writeCommands[args[1]] = parseValue(args[2])
7178
}
7279

7380
var parsedData map[interface{}]interface{}
74-
readYaml(args, &parsedData)
75-
76-
var paths = parsePath(args[1])
81+
readYaml(args[0], &parsedData)
7782

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+
}
7987

8088
if writeInplace {
8189
ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644)
8290
} else {
8391
printYaml(parsedData)
8492
}
8593
}
86-
func getValue(argument string) interface{} {
94+
func parseValue(argument string) interface{} {
8795
var value, err interface{}
8896
var inQuotes = argument[0] == '"'
8997
if !inQuotes {
@@ -118,19 +126,19 @@ func yamlToString(context interface{}) string {
118126
return outStr
119127
}
120128

121-
func readYaml(args []string, parsedData *map[interface{}]interface{}) {
122-
if len(args) == 0 {
129+
func readYaml(filename string, parsedData interface{}) {
130+
if filename == "" {
123131
die("Must provide filename")
124132
}
125133

126134
var rawData []byte
127-
if args[0] == "-" {
135+
if filename == "-" {
128136
rawData = readStdin()
129137
} else {
130-
rawData = readFile(args[0])
138+
rawData = readFile(filename)
131139
}
132140

133-
err := yaml.Unmarshal([]byte(rawData), &parsedData)
141+
err := yaml.Unmarshal([]byte(rawData), parsedData)
134142
if err != nil {
135143
die("error: %v", err)
136144
}

yaml_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
)
66

7-
var getValueTests = []struct {
7+
var parseValueTests = []struct {
88
argument string
99
expectedResult interface{}
1010
testDescription string
@@ -15,8 +15,8 @@ var getValueTests = []struct {
1515
{"\"3.4\"", "3.4", "number as string"},
1616
}
1717

18-
func TestGetValue(t *testing.T) {
19-
for _, tt := range getValueTests {
20-
assertResultWithContext(t, tt.expectedResult, getValue(tt.argument), tt.testDescription)
18+
func TestParseValue(t *testing.T) {
19+
for _, tt := range parseValueTests {
20+
assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
2121
}
2222
}

0 commit comments

Comments
 (0)