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

Compare commits

..

5 Commits
1.1 ... 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
mfarah
15f51d4bf0 Can update a yaml file from an instruction yaml file 2015-10-08 10:31:31 +11:00
mfarah
219105f999 Test result order no longer matters 2015-10-07 13:36:39 +11:00
6 changed files with 105 additions and 30 deletions

View File

@@ -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
```

View File

@@ -34,7 +34,10 @@ mapSplat:
item1: things
item2: whatever
`)
assertResult(t, "[things whatever]", fmt.Sprintf("%v", readMap(data, "mapSplat", []string{"*"})))
var result = readMap(data, "mapSplat", []string{"*"}).([]interface{})
var actual = []string{result[0].(string), result[1].(string)}
sort.Strings(actual)
assertResult(t, "[things whatever]", fmt.Sprintf("%v", actual))
}
func TestReadMap_deep_splat(t *testing.T) {

2
instruction_sample.yaml Normal file
View File

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

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

72
yaml.go
View File

@@ -12,6 +12,7 @@ import (
var trimOutput = true
var writeInplace = false
var writeScript = ""
func main() {
var cmdRead = &cobra.Command{
@@ -32,17 +33,29 @@ 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")
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")
@@ -51,39 +64,54 @@ 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, &parsedData)
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) {
if len(args) < 3 {
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])
}
var parsedData map[interface{}]interface{}
readYaml(args, &parsedData)
readYaml(args[0], &parsedData)
var paths = parsePath(args[1])
write(parsedData, paths[0], paths[1:len(paths)], getValue(args[2]))
if writeInplace {
ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644)
} else {
printYaml(parsedData)
for path, value := range writeCommands {
var paths = parsePath(path)
write(parsedData, paths[0], paths[1:len(paths)], value)
}
return parsedData
}
func getValue(argument string) interface{} {
func parseValue(argument string) interface{} {
var value, err interface{}
var inQuotes = argument[0] == '"'
if !inQuotes {
@@ -118,19 +146,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)
}

View File

@@ -4,7 +4,7 @@ import (
"testing"
)
var getValueTests = []struct {
var parseValueTests = []struct {
argument string
expectedResult interface{}
testDescription string
@@ -15,8 +15,22 @@ 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)
}
}
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"})
}