Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Read yaml array #37

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,99 @@ func TestReadCmd(t *testing.T) {
assertResult(t, "2\n", result.Output)
}

func TestReadCmd_ArrayYaml(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/array.yaml [0].gather_facts")
if result.Error != nil {
t.Error(result.Error)
}
assertResult(t, "false\n", result.Output)
}

func TestReadCmd_ArrayYaml_NoPath(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/array.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- become: true
gather_facts: false
hosts: lalaland
name: Apply smth
roles:
- lala
- land
serial: 1
`
assertResult(t, expectedOutput, result.Output)
}

func TestReadCmd_ArrayYaml_OneElement(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/array.yaml [0]")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `become: true
gather_facts: false
hosts: lalaland
name: Apply smth
roles:
- lala
- land
serial: 1
`
assertResult(t, expectedOutput, result.Output)
}

func TestReadCmd_ArrayYaml_Splat(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/array.yaml [*]")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- become: true
gather_facts: false
hosts: lalaland
name: Apply smth
roles:
- lala
- land
serial: 1
`
assertResult(t, expectedOutput, result.Output)
}

func TestReadCmd_ArrayYaml_SplatKey(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/array.yaml [*].gather_facts")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := "- false\n"
assertResult(t, expectedOutput, result.Output)
}

func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/array.yaml [x].gather_facts")
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
assertResult(t, expectedOutput, result.Error.Error())
}

func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/array.yaml [*].roles[x]")
if result.Error == nil {
t.Error("Expected command to fail due to invalid path")
}
expectedOutput := `Error accessing array: strconv.ParseInt: parsing "x": invalid syntax`
assertResult(t, expectedOutput, result.Error.Error())
}

func TestReadCmd_Error(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read")
Expand Down
9 changes: 9 additions & 0 deletions examples/array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- become: true
gather_facts: false
hosts: lalaland
name: "Apply smth"
roles:
- lala
- land
serial: 1
43 changes: 43 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ func read(args []string) (interface{}, error) {
path = "thing." + path
}

if parsedData != nil && parsedData[0].Key == nil {
var interfaceData []map[interface{}]interface{}
if err := readData(args[0], &interfaceData); err == nil {
var listMap []yaml.MapSlice
for _, item := range interfaceData {
listMap = append(listMap, mapToMapSlice(item))
}
return readYamlArray(listMap, path)
}
}

if path == "" {
return parsedData, nil
}
Expand All @@ -194,6 +205,38 @@ func read(args []string) (interface{}, error) {
return readMap(parsedData, paths[0], paths[1:])
}

func readYamlArray(listMap []yaml.MapSlice, path string) (interface{}, error) {
if path == "" {
return listMap, nil
}

var paths = parsePath(path)

if paths[0] == "*" {
if len(paths[1:]) == 0 {
return listMap, nil
}
var results []interface{}
for _, m := range listMap {
value, err := readMap(m, paths[1], paths[2:])
if err != nil {
return nil, err
}
results = append(results, value)
}
return results, nil
}

index, err := strconv.ParseInt(paths[0], 10, 64)
if err != nil {
return nil, fmt.Errorf("Error accessing array: %v", err)
}
if len(paths[1:]) == 0 {
return listMap[index], nil
}
return readMap(listMap[index], paths[1], paths[2:])
}

func newProperty(cmd *cobra.Command, args []string) error {
updatedData, err := newYaml(args)
if err != nil {
Expand Down