forked from adammck/terraform-inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
92 lines (75 loc) · 2 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/json"
"fmt"
"io"
)
func gatherResources(s *state) map[string]interface{} {
groups := make(map[string]interface{}, 0)
for _, res := range s.resources() {
for _, grp := range res.Groups() {
_, ok := groups[grp]
if !ok {
groups[grp] = []string{}
}
groups[grp] = append(groups[grp].([]string), res.Address())
}
}
if len(s.outputs()) > 0 {
groups["all"] = make(map[string]string, 0)
for _, out := range s.outputs() {
groups["all"].(map[string]string)[out.keyName] = out.value
}
}
return groups
}
func cmdList(stdout io.Writer, stderr io.Writer, s *state) int {
return output(stdout, stderr, gatherResources(s))
}
func cmdInventory(stdout io.Writer, stderr io.Writer, s *state) int {
groups := gatherResources(s)
for group, res := range groups {
_, err := io.WriteString(stdout, "["+group+"]\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1
}
for _, ress := range res.([]string) {
_, err := io.WriteString(stdout, ress+"\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1
}
}
_, err = io.WriteString(stdout, "\n")
if err != nil {
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
return 1
}
}
return 0
}
func cmdHost(stdout io.Writer, stderr io.Writer, s *state, hostname string) int {
for _, res := range s.resources() {
if hostname == res.Address() {
return output(stdout, stderr, res.Attributes())
}
}
fmt.Fprintf(stderr, "No such host: %s\n", hostname)
return 1
}
// output marshals an arbitrary JSON object and writes it to stdout, or writes
// an error to stderr, then returns the appropriate exit code.
func output(stdout io.Writer, stderr io.Writer, whatever interface{}) int {
b, err := json.Marshal(whatever)
if err != nil {
fmt.Fprintf(stderr, "Error encoding JSON: %s\n", err)
return 1
}
_, err = stdout.Write(b)
if err != nil {
fmt.Fprintf(stderr, "Error writing JSON: %s\n", err)
return 1
}
return 0
}