1
1
mirror of https://github.com/adammck/terraform-inventory synced 2024-09-25 03:48:12 +02:00
terraform-inventory/cli.go

95 lines
2.2 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"fmt"
"io"
)
func gatherResources(s *state) map[string][]string {
groups := make(map[string][]string, 0)
for _, res := range s.resources() {
2015-12-15 04:24:41 +01:00
for _, grp := range res.Groups() {
tmpGroup := []string{}
2015-12-15 04:24:41 +01:00
_, ok := groups[grp]
if ok {
tmpGroup = groups[grp].([]string)
2015-12-15 04:24:41 +01:00
}
tmpGroup = append(tmpGroup, res.Address())
groups[grp] = tmpGroup
2015-12-15 04:24:41 +01:00
}
}
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 {
_, 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() {
2015-06-05 04:43:56 +02:00
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
}