Skip to content

Commit 1ed8e70

Browse files
author
Mike Farah
committed
Can create arrays
1 parent 5514d23 commit 1ed8e70

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

yaml.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,18 @@ func newYaml(args []string) interface{} {
160160
writeCommands[0] = yaml.MapItem{Key: args[0], Value: parseValue(args[1])}
161161
}
162162

163-
parsedData := make(yaml.MapSlice, 0)
163+
var parsedData yaml.MapSlice
164+
var prependCommand = ""
165+
var isArray = strings.HasPrefix(writeCommands[0].Key.(string), "[")
166+
if isArray {
167+
item := yaml.MapItem{Key: "thing", Value: make(yaml.MapSlice, 0)}
168+
parsedData = yaml.MapSlice{item}
169+
prependCommand = "thing"
170+
} else {
171+
parsedData = make(yaml.MapSlice, 0)
172+
}
164173

165-
return updateParsedData(parsedData, writeCommands, "")
174+
return updateParsedData(parsedData, writeCommands, prependCommand)
166175
}
167176

168177
func writeProperty(cmd *cobra.Command, args []string) {

yaml_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func TestNewYaml(t *testing.T) {
5555
formattedResult)
5656
}
5757

58+
func TestNewYamlArray(t *testing.T) {
59+
result := newYaml([]string{"[0].cat", "meow"})
60+
formattedResult := fmt.Sprintf("%v", result)
61+
assertResult(t,
62+
"[[{cat meow}]]",
63+
formattedResult)
64+
}
65+
5866
func TestUpdateYaml(t *testing.T) {
5967
result := updateYaml([]string{"sample.yaml", "b.c", "3"})
6068
formattedResult := fmt.Sprintf("%v", result)

0 commit comments

Comments
 (0)