Can update a yaml file from an instruction yaml file

This commit is contained in:
mfarah 2015-10-08 10:31:31 +11:00
parent 219105f999
commit 15f51d4bf0
4 changed files with 54 additions and 16 deletions

View File

@ -132,3 +132,31 @@ then
yaml w -i sample.yaml b.c cat yaml w -i sample.yaml b.c cat
``` ```
will update the sample.yaml file so that the value of 'c' is cat. will update the sample.yaml file so that the value of 'c' is cat.
### Updating multiple values with a script
Given a sample.yaml file of:
```yaml
b:
c: 2
e:
- name: Billy Bob
```
and a script update_instructions.yaml of:
```yaml
b.c: 3
b.e[0].name: Howdy Partner
```
then
```bash
yaml -w -s update_instructions.yaml sample.yaml
```
will output:
```yaml
b:
c: 3
e:
- name: Howdy Partner
```

2
instruction_sample.yaml Normal file
View File

@ -0,0 +1,2 @@
b.c: cat
b.e[0].name: Mike Farah

32
yaml.go
View File

@ -12,6 +12,7 @@ import (
var trimOutput = true var trimOutput = true
var writeInplace = false var writeInplace = false
var writeScript = ""
func main() { func main() {
var cmdRead = &cobra.Command{ var cmdRead = &cobra.Command{
@ -43,6 +44,7 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
Run: writeProperty, Run: writeProperty,
} }
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace") cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
var rootCmd = &cobra.Command{Use: "yaml"} var rootCmd = &cobra.Command{Use: "yaml"}
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output") 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
func readProperty(cmd *cobra.Command, args []string) { func readProperty(cmd *cobra.Command, args []string) {
var parsedData map[interface{}]interface{} var parsedData map[interface{}]interface{}
readYaml(args, &parsedData) readYaml(args[0], &parsedData)
if len(args) == 1 { if len(args) == 1 {
printYaml(parsedData) printYaml(parsedData)
@ -66,16 +68,22 @@ func readProperty(cmd *cobra.Command, args []string) {
} }
func writeProperty(cmd *cobra.Command, args []string) { func writeProperty(cmd *cobra.Command, args []string) {
if len(args) < 3 { var writeCommands map[string]interface{}
if writeScript != "" {
readYaml(writeScript, &writeCommands)
} else if len(args) < 3 {
die("Must provide <filename> <path_to_update> <value>") die("Must provide <filename> <path_to_update> <value>")
} else {
writeCommands[args[1]] = parseValue(args[2])
} }
var parsedData map[interface{}]interface{} var parsedData map[interface{}]interface{}
readYaml(args, &parsedData) readYaml(args[0], &parsedData)
var paths = parsePath(args[1]) for path, value := range writeCommands {
var paths = parsePath(path)
write(parsedData, paths[0], paths[1:len(paths)], getValue(args[2])) write(parsedData, paths[0], paths[1:len(paths)], value)
}
if writeInplace { if writeInplace {
ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644) ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644)
@ -83,7 +91,7 @@ func writeProperty(cmd *cobra.Command, args []string) {
printYaml(parsedData) printYaml(parsedData)
} }
} }
func getValue(argument string) interface{} { func parseValue(argument string) interface{} {
var value, err interface{} var value, err interface{}
var inQuotes = argument[0] == '"' var inQuotes = argument[0] == '"'
if !inQuotes { if !inQuotes {
@ -118,19 +126,19 @@ func yamlToString(context interface{}) string {
return outStr return outStr
} }
func readYaml(args []string, parsedData *map[interface{}]interface{}) { func readYaml(filename string, parsedData interface{}) {
if len(args) == 0 { if filename == "" {
die("Must provide filename") die("Must provide filename")
} }
var rawData []byte var rawData []byte
if args[0] == "-" { if filename == "-" {
rawData = readStdin() rawData = readStdin()
} else { } else {
rawData = readFile(args[0]) rawData = readFile(filename)
} }
err := yaml.Unmarshal([]byte(rawData), &parsedData) err := yaml.Unmarshal([]byte(rawData), parsedData)
if err != nil { if err != nil {
die("error: %v", err) die("error: %v", err)
} }

View File

@ -4,7 +4,7 @@ import (
"testing" "testing"
) )
var getValueTests = []struct { var parseValueTests = []struct {
argument string argument string
expectedResult interface{} expectedResult interface{}
testDescription string testDescription string
@ -15,8 +15,8 @@ var getValueTests = []struct {
{"\"3.4\"", "3.4", "number as string"}, {"\"3.4\"", "3.4", "number as string"},
} }
func TestGetValue(t *testing.T) { func TestParseValue(t *testing.T) {
for _, tt := range getValueTests { for _, tt := range parseValueTests {
assertResultWithContext(t, tt.expectedResult, getValue(tt.argument), tt.testDescription) assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
} }
} }