mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Can update a yaml file from an instruction yaml file
This commit is contained in:
parent
219105f999
commit
15f51d4bf0
28
README.md
28
README.md
@ -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
instruction_sample.yaml
Normal file
2
instruction_sample.yaml
Normal file
@ -0,0 +1,2 @@
|
||||
b.c: cat
|
||||
b.e[0].name: Mike Farah
|
32
yaml.go
32
yaml.go
@ -12,6 +12,7 @@ import (
|
||||
|
||||
var trimOutput = true
|
||||
var writeInplace = false
|
||||
var writeScript = ""
|
||||
|
||||
func main() {
|
||||
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,
|
||||
}
|
||||
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")
|
||||
@ -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)
|
||||
@ -66,16 +68,22 @@ 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)
|
||||
readYaml(args[0], &parsedData)
|
||||
|
||||
var paths = parsePath(args[1])
|
||||
|
||||
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)
|
||||
@ -83,7 +91,7 @@ func writeProperty(cmd *cobra.Command, args []string) {
|
||||
printYaml(parsedData)
|
||||
}
|
||||
}
|
||||
func getValue(argument string) interface{} {
|
||||
func parseValue(argument string) interface{} {
|
||||
var value, err interface{}
|
||||
var inQuotes = argument[0] == '"'
|
||||
if !inQuotes {
|
||||
@ -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)
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var getValueTests = []struct {
|
||||
var parseValueTests = []struct {
|
||||
argument string
|
||||
expectedResult interface{}
|
||||
testDescription string
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user