@@ -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+
928func 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+
65100func cmdHost (stdout io.Writer , stderr io.Writer , s * state , hostname string ) int {
66101 for _ , res := range s .resources () {
67102 if hostname == res .Address () {
0 commit comments