Skip to content

Commit

Permalink
Can create arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Farah committed Aug 8, 2017
1 parent 5514d23 commit 1ed8e70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,18 @@ func newYaml(args []string) interface{} {
writeCommands[0] = yaml.MapItem{Key: args[0], Value: parseValue(args[1])}
}

parsedData := make(yaml.MapSlice, 0)
var parsedData yaml.MapSlice
var prependCommand = ""
var isArray = strings.HasPrefix(writeCommands[0].Key.(string), "[")
if isArray {
item := yaml.MapItem{Key: "thing", Value: make(yaml.MapSlice, 0)}
parsedData = yaml.MapSlice{item}
prependCommand = "thing"
} else {
parsedData = make(yaml.MapSlice, 0)
}

return updateParsedData(parsedData, writeCommands, "")
return updateParsedData(parsedData, writeCommands, prependCommand)
}

func writeProperty(cmd *cobra.Command, args []string) {
Expand Down
8 changes: 8 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func TestNewYaml(t *testing.T) {
formattedResult)
}

func TestNewYamlArray(t *testing.T) {
result := newYaml([]string{"[0].cat", "meow"})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[[{cat meow}]]",
formattedResult)
}

func TestUpdateYaml(t *testing.T) {
result := updateYaml([]string{"sample.yaml", "b.c", "3"})
formattedResult := fmt.Sprintf("%v", result)
Expand Down

0 comments on commit 1ed8e70

Please sign in to comment.