Skip to content

Commit

Permalink
Can update a yaml file from an instruction yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
mfarah committed Oct 7, 2015
1 parent 219105f commit 15f51d4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,31 @@ then
yaml w -i sample.yaml b.c 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 changes: 2 additions & 0 deletions instruction_sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
b.c: cat
b.e[0].name: Mike Farah
32 changes: 20 additions & 12 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

var trimOutput = true
var writeInplace = false
var writeScript = ""

func main() {
var cmdRead = &cobra.Command{
Expand Down Expand Up @@ -43,6 +44,7 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
Run: writeProperty,
}
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"}
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
Expand All @@ -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) {
var parsedData map[interface{}]interface{}

readYaml(args, &parsedData)
readYaml(args[0], &parsedData)

if len(args) == 1 {
printYaml(parsedData)
Expand All @@ -66,24 +68,30 @@ func readProperty(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>")
} else {
writeCommands[args[1]] = parseValue(args[2])
}

var parsedData map[interface{}]interface{}
readYaml(args, &parsedData)

var paths = parsePath(args[1])
readYaml(args[0], &parsedData)

write(parsedData, paths[0], paths[1:len(paths)], getValue(args[2]))
for path, value := range writeCommands {
var paths = parsePath(path)
write(parsedData, paths[0], paths[1:len(paths)], value)
}

if writeInplace {
ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644)
} else {
printYaml(parsedData)
}
}
func getValue(argument string) interface{} {
func parseValue(argument string) interface{} {
var value, err interface{}
var inQuotes = argument[0] == '"'
if !inQuotes {
Expand Down Expand Up @@ -118,19 +126,19 @@ func yamlToString(context interface{}) string {
return outStr
}

func readYaml(args []string, parsedData *map[interface{}]interface{}) {
if len(args) == 0 {
func readYaml(filename string, parsedData interface{}) {
if filename == "" {
die("Must provide filename")
}

var rawData []byte
if args[0] == "-" {
if filename == "-" {
rawData = readStdin()
} else {
rawData = readFile(args[0])
rawData = readFile(filename)
}

err := yaml.Unmarshal([]byte(rawData), &parsedData)
err := yaml.Unmarshal([]byte(rawData), parsedData)
if err != nil {
die("error: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
)

var getValueTests = []struct {
var parseValueTests = []struct {
argument string
expectedResult interface{}
testDescription string
Expand All @@ -15,8 +15,8 @@ var getValueTests = []struct {
{"\"3.4\"", "3.4", "number as string"},
}

func TestGetValue(t *testing.T) {
for _, tt := range getValueTests {
assertResultWithContext(t, tt.expectedResult, getValue(tt.argument), tt.testDescription)
func TestParseValue(t *testing.T) {
for _, tt := range parseValueTests {
assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
}
}

0 comments on commit 15f51d4

Please sign in to comment.