1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Compare commits

..

3 Commits
1.2 ... 1.2.1

Author SHA1 Message Date
Mike Farah
2ec3b59da2 Added more help info about using write with an update script 2015-10-09 21:30:15 +11:00
Mike Farah
56c923228d Fixed bug when using write command
Added more tests to avoid bugs ;)
2015-10-09 21:24:42 +11:00
Mike Farah
c122b9a843 Fixed example in README 2015-10-08 20:07:27 +11:00
4 changed files with 49 additions and 15 deletions

View File

@@ -150,7 +150,7 @@ b.e[0].name: Howdy Partner
then
```bash
yaml -w -s update_instructions.yaml sample.yaml
yaml w -s update_instructions.yaml sample.yaml
```
will output:
```yaml

View File

@@ -3,12 +3,12 @@
gofmt -w .
golint
go test
go build
# acceptance test
go build
X=$(./yaml r sample.yaml b.c)
X=$(./yaml w sample.yaml b.c 3 | ./yaml r - b.c)
if [ $X != 2 ]
if [ $X != 3 ]
then
echo "Failed acceptance test: expected 2 but was $X"
exit 1

42
yaml.go
View File

@@ -33,14 +33,25 @@ yaml r things.yaml a.array[*].blah
var cmdWrite = &cobra.Command{
Use: "write [yaml_file] [path] [value]",
Aliases: []string{"w"},
Short: "yaml w [--inplace/-i] sample.yaml a.b.c newValueForC",
Short: "yaml w [--inplace/-i] [--script/-s script_file] sample.yaml a.b.c newValueForC",
Example: `
yaml write things.yaml a.b.c cat
yaml write --inplace things.yaml a.b.c cat
yaml w -i things.yaml a.b.c cat
yaml w --script update_script.yaml things.yaml
yaml w -i -s update_script.yaml things.yaml
`,
Long: `Updates the yaml file w.r.t the given path and value.
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.`,
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
Update Scripts:
Note that you can give an update script to perform more sophisticated updated. Update script
format is a yaml map where the key is the path and the value is..well the value. e.g.:
---
a.b.c: true,
a.b.e:
- name: bob
`,
Run: writeProperty,
}
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
@@ -53,27 +64,40 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
}
func readProperty(cmd *cobra.Command, args []string) {
printYaml(read(args))
}
func read(args []string) interface{} {
var parsedData map[interface{}]interface{}
readYaml(args[0], &parsedData)
if len(args) == 1 {
printYaml(parsedData)
os.Exit(0)
return parsedData
}
var paths = parsePath(args[1])
printYaml(readMap(parsedData, paths[0], paths[1:len(paths)]))
return readMap(parsedData, paths[0], paths[1:len(paths)])
}
func writeProperty(cmd *cobra.Command, args []string) {
updatedData := updateYaml(args)
if writeInplace {
ioutil.WriteFile(args[0], []byte(yamlToString(updatedData)), 0644)
} else {
printYaml(updatedData)
}
}
func updateYaml(args []string) interface{} {
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 = make(map[string]interface{})
writeCommands[args[1]] = parseValue(args[2])
}
@@ -84,13 +108,9 @@ func writeProperty(cmd *cobra.Command, args []string) {
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)
}
return parsedData
}
func parseValue(argument string) interface{} {
var value, err interface{}
var inQuotes = argument[0] == '"'

View File

@@ -20,3 +20,17 @@ func TestParseValue(t *testing.T) {
assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
}
}
func TestRead(t *testing.T) {
result := read([]string{"sample.yaml", "b.c"})
assertResult(t, 2, result)
}
func TestUpdateYaml(t *testing.T) {
updateYaml([]string{"sample.yaml", "b.c", "3"})
}
func TestUpdateYaml_WithScript(t *testing.T) {
writeScript = "instruction_sample.yaml"
updateYaml([]string{"sample.yaml"})
}