diff --git a/yaml.go b/yaml.go index a5580706..49784c00 100644 --- a/yaml.go +++ b/yaml.go @@ -162,7 +162,7 @@ func newYaml(args []string) interface{} { parsedData := make(yaml.MapSlice, 0) - return updateParsedData(parsedData, writeCommands) + return updateParsedData(parsedData, writeCommands, "") } func writeProperty(cmd *cobra.Command, args []string) { @@ -177,18 +177,26 @@ func writeProperty(cmd *cobra.Command, args []string) { } } -func updateParsedData(parsedData yaml.MapSlice, writeCommands yaml.MapSlice) yaml.MapSlice { +func updateParsedData(parsedData yaml.MapSlice, writeCommands yaml.MapSlice, prependCommand string) interface{} { + var prefix = "" + if prependCommand != "" { + prefix = prependCommand + "." + } for _, entry := range writeCommands { - path := entry.Key + path := prefix + entry.Key.(string) value := entry.Value - var paths = parsePath(path.(string)) + var paths = parsePath(path) parsedData = writeMap(parsedData, paths, value) } + if prependCommand != "" { + return readMap(parsedData, prependCommand, make([]string, 0)) + } return parsedData } func updateYaml(args []string) interface{} { var writeCommands yaml.MapSlice + var prependCommand = "" if writeScript != "" { readDataOrDie(writeScript, &writeCommands, false) } else if len(args) < 3 { @@ -199,9 +207,16 @@ func updateYaml(args []string) interface{} { } var parsedData yaml.MapSlice - readDataOrDie(args[0], &parsedData, inputJSON) + err := readData(args[0], &parsedData, inputJSON) + if err != nil { + var generalData interface{} + readDataOrDie(args[0], &generalData, inputJSON) + item := yaml.MapItem{Key: "thing", Value: generalData} + parsedData = yaml.MapSlice{item} + prependCommand = "thing" + } - return updateParsedData(parsedData, writeCommands) + return updateParsedData(parsedData, writeCommands, prependCommand) } func parseValue(argument string) interface{} { diff --git a/yaml_test.go b/yaml_test.go index 367d9e3a..a4a8e32d 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -63,6 +63,14 @@ func TestUpdateYaml(t *testing.T) { formattedResult) } +func TestUpdateYamlArray(t *testing.T) { + result := updateYaml([]string{"sample_array.yaml", "[0]", "3"}) + formattedResult := fmt.Sprintf("%v", result) + assertResult(t, + "[3 2 3]", + formattedResult) +} + func TestUpdateYaml_WithScript(t *testing.T) { writeScript = "instruction_sample.yaml" updateYaml([]string{"sample.yaml"})