Skip to content

Commit 0e941ce

Browse files
Jonnymccadammck
authored andcommitted
Create all group as per Ansible inventory script conventions (#46)
* Conform to Ansible inventory script conventions * Make cmdInventory work with all group * Add only uniq hosts to group "all"'s host list * go fmt * Fix typo * Fix tests broken by #46 * Add duplicate host to test appendUniq * Fix appendUniq and dedup other groups besides "all"
1 parent e62801b commit 0e941ce

File tree

2 files changed

+85
-20
lines changed

2 files changed

+85
-20
lines changed

cli.go

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,32 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"sort"
78
)
89

10+
type allGroup struct {
11+
Hosts []string `json:"hosts"`
12+
Vars map[string]interface{} `json:"vars"`
13+
}
14+
15+
func appendUniq(strs []string, item string) []string {
16+
if len(strs) == 0 {
17+
strs = append(strs, item)
18+
return strs
19+
}
20+
sort.Strings(strs)
21+
i := sort.SearchStrings(strs, item)
22+
if i == len(strs) || (i < len(strs) && strs[i] != item) {
23+
strs = append(strs, item)
24+
}
25+
return strs
26+
}
27+
928
func gatherResources(s *state) map[string]interface{} {
1029
groups := make(map[string]interface{}, 0)
30+
all_group := allGroup{Vars: make(map[string]interface{})}
31+
groups["all"] = &all_group
32+
1133
for _, res := range s.resources() {
1234
for _, grp := range res.Groups() {
1335

@@ -16,14 +38,14 @@ func gatherResources(s *state) map[string]interface{} {
1638
groups[grp] = []string{}
1739
}
1840

19-
groups[grp] = append(groups[grp].([]string), res.Address())
41+
groups[grp] = appendUniq(groups[grp].([]string), res.Address())
42+
all_group.Hosts = appendUniq(all_group.Hosts, res.Address())
2043
}
2144
}
2245

2346
if len(s.outputs()) > 0 {
24-
groups["all"] = make(map[string]interface{}, 0)
2547
for _, out := range s.outputs() {
26-
groups["all"].(map[string]interface{})[out.keyName] = out.value
48+
all_group.Vars[out.keyName] = out.value
2749
}
2850
}
2951
return groups
@@ -37,31 +59,44 @@ func cmdInventory(stdout io.Writer, stderr io.Writer, s *state) int {
3759
groups := gatherResources(s)
3860
for group, res := range groups {
3961

40-
_, err := io.WriteString(stdout, "["+group+"]\n")
41-
if err != nil {
42-
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
43-
return 1
44-
}
45-
46-
for _, ress := range res.([]string) {
62+
switch grp := res.(type) {
63+
case []string:
64+
writeLn("["+group+"]", stdout, stderr)
65+
for _, item := range grp {
66+
writeLn(item, stdout, stderr)
67+
}
4768

48-
_, err := io.WriteString(stdout, ress+"\n")
49-
if err != nil {
50-
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
51-
return 1
69+
case *allGroup:
70+
writeLn("["+group+"]", stdout, stderr)
71+
for _, item := range grp.Hosts {
72+
writeLn(item, stdout, stderr)
73+
}
74+
writeLn("", stdout, stderr)
75+
writeLn("["+group+":vars]", stdout, stderr)
76+
for key, item := range grp.Vars {
77+
writeLn(key+"="+item.(string), stdout, stderr)
5278
}
5379
}
5480

55-
_, err = io.WriteString(stdout, "\n")
56-
if err != nil {
57-
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
58-
return 1
59-
}
81+
writeLn("", stdout, stderr)
6082
}
6183

6284
return 0
6385
}
6486

87+
func writeLn(str string, stdout io.Writer, stderr io.Writer) {
88+
_, err := io.WriteString(stdout, str+"\n")
89+
checkErr(err, stderr)
90+
}
91+
92+
func checkErr(err error, stderr io.Writer) int {
93+
if err != nil {
94+
fmt.Fprintf(stderr, "Error writing inventory: %s\n", err)
95+
return 1
96+
}
97+
return 0
98+
}
99+
65100
func cmdHost(stdout io.Writer, stderr io.Writer, s *state, hostname string) int {
66101
for _, res := range s.resources() {
67102
if hostname == res.Address() {

parser_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ const exampleStateFile = `
4949
}
5050
}
5151
},
52+
"aws_instance.dup.0": {
53+
"type": "aws_instance",
54+
"primary": {
55+
"id": "i-aaaaaaaa",
56+
"attributes": {
57+
"id": "i-aaaaaaaa",
58+
"private_ip": "10.0.0.1",
59+
"tags.#": "1",
60+
"tags.Role": "Web"
61+
}
62+
}
63+
},
5264
"aws_instance.one.1": {
5365
"type": "aws_instance",
5466
"primary": {
@@ -139,15 +151,33 @@ const exampleStateFile = `
139151

140152
const expectedListOutput = `
141153
{
142-
"all": {"datacenter": "mydc", "olddatacenter": "<0.7_format", "ids": [1, 2, 3, 4], "map": {"key": "value"}},
154+
"all": {
155+
"hosts": [
156+
"10.0.0.1",
157+
"10.0.1.1",
158+
"10.120.0.226",
159+
"10.2.1.5",
160+
"10.20.30.40",
161+
"192.168.0.3",
162+
"50.0.0.1"
163+
],
164+
"vars": {
165+
"datacenter": "mydc",
166+
"olddatacenter": "<0.7_format",
167+
"ids": [1, 2, 3, 4],
168+
"map": {"key": "value"}
169+
}
170+
},
143171
"one": ["10.0.0.1", "10.0.1.1"],
172+
"dup": ["10.0.0.1"],
144173
"two": ["50.0.0.1"],
145174
"three": ["192.168.0.3"],
146175
"four": ["10.2.1.5"],
147176
"five": ["10.20.30.40"],
148177
"six": ["10.120.0.226"],
149178
150179
"one.0": ["10.0.0.1"],
180+
"dup.0": ["10.0.0.1"],
151181
"one.1": ["10.0.1.1"],
152182
"two.0": ["50.0.0.1"],
153183
"three.0": ["192.168.0.3"],

0 commit comments

Comments
 (0)