Skip to content

Commit

Permalink
Can update arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Farah committed Aug 8, 2017
1 parent 5bb0934 commit 5514d23
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
27 changes: 21 additions & 6 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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{} {
Expand Down
8 changes: 8 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit 5514d23

Please sign in to comment.