mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ec3b59da2 | ||
|
|
56c923228d | ||
|
|
c122b9a843 |
@@ -150,7 +150,7 @@ b.e[0].name: Howdy Partner
|
|||||||
then
|
then
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
yaml -w -s update_instructions.yaml sample.yaml
|
yaml w -s update_instructions.yaml sample.yaml
|
||||||
```
|
```
|
||||||
will output:
|
will output:
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@@ -3,12 +3,12 @@
|
|||||||
gofmt -w .
|
gofmt -w .
|
||||||
golint
|
golint
|
||||||
go test
|
go test
|
||||||
|
go build
|
||||||
|
|
||||||
# acceptance test
|
# acceptance test
|
||||||
go build
|
X=$(./yaml w sample.yaml b.c 3 | ./yaml r - b.c)
|
||||||
X=$(./yaml r sample.yaml b.c)
|
|
||||||
|
|
||||||
if [ $X != 2 ]
|
if [ $X != 3 ]
|
||||||
then
|
then
|
||||||
echo "Failed acceptance test: expected 2 but was $X"
|
echo "Failed acceptance test: expected 2 but was $X"
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
42
yaml.go
42
yaml.go
@@ -33,14 +33,25 @@ yaml r things.yaml a.array[*].blah
|
|||||||
var cmdWrite = &cobra.Command{
|
var cmdWrite = &cobra.Command{
|
||||||
Use: "write [yaml_file] [path] [value]",
|
Use: "write [yaml_file] [path] [value]",
|
||||||
Aliases: []string{"w"},
|
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: `
|
Example: `
|
||||||
yaml write things.yaml a.b.c cat
|
yaml write things.yaml a.b.c cat
|
||||||
yaml write --inplace 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 -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.
|
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,
|
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")
|
||||||
@@ -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) {
|
func readProperty(cmd *cobra.Command, args []string) {
|
||||||
|
printYaml(read(args))
|
||||||
|
}
|
||||||
|
|
||||||
|
func read(args []string) interface{} {
|
||||||
var parsedData map[interface{}]interface{}
|
var parsedData map[interface{}]interface{}
|
||||||
|
|
||||||
readYaml(args[0], &parsedData)
|
readYaml(args[0], &parsedData)
|
||||||
|
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
printYaml(parsedData)
|
return parsedData
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var paths = parsePath(args[1])
|
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) {
|
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{}
|
var writeCommands map[string]interface{}
|
||||||
if writeScript != "" {
|
if writeScript != "" {
|
||||||
readYaml(writeScript, &writeCommands)
|
readYaml(writeScript, &writeCommands)
|
||||||
} else if len(args) < 3 {
|
} else if len(args) < 3 {
|
||||||
die("Must provide <filename> <path_to_update> <value>")
|
die("Must provide <filename> <path_to_update> <value>")
|
||||||
} else {
|
} else {
|
||||||
|
writeCommands = make(map[string]interface{})
|
||||||
writeCommands[args[1]] = parseValue(args[2])
|
writeCommands[args[1]] = parseValue(args[2])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,13 +108,9 @@ func writeProperty(cmd *cobra.Command, args []string) {
|
|||||||
var paths = parsePath(path)
|
var paths = parsePath(path)
|
||||||
write(parsedData, paths[0], paths[1:len(paths)], value)
|
write(parsedData, paths[0], paths[1:len(paths)], value)
|
||||||
}
|
}
|
||||||
|
return parsedData
|
||||||
if writeInplace {
|
|
||||||
ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644)
|
|
||||||
} else {
|
|
||||||
printYaml(parsedData)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseValue(argument string) interface{} {
|
func parseValue(argument string) interface{} {
|
||||||
var value, err interface{}
|
var value, err interface{}
|
||||||
var inQuotes = argument[0] == '"'
|
var inQuotes = argument[0] == '"'
|
||||||
|
|||||||
14
yaml_test.go
14
yaml_test.go
@@ -20,3 +20,17 @@ func TestParseValue(t *testing.T) {
|
|||||||
assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
|
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"})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user