1
1
mirror of https://github.com/adammck/terraform-inventory synced 2024-09-22 00:40:42 +02:00

Added the inventory option to produce an output similar to ansible host file

This commit is contained in:
André Santos 2016-02-24 12:03:36 +00:00
parent 35e77604ba
commit 10faafd763
2 changed files with 39 additions and 3 deletions

36
cli.go
View File

@ -6,7 +6,7 @@ import (
"io"
)
func cmdList(stdout io.Writer, stderr io.Writer, s *state) int {
func gatherResources(s *state) map[string][]string {
groups := make(map[string][]string, 0)
for _, res := range s.resources() {
for _, grp := range res.Groups() {
@ -19,8 +19,40 @@ func cmdList(stdout io.Writer, stderr io.Writer, s *state) int {
groups[grp] = append(groups[grp], res.Address())
}
}
return groups
}
return output(stdout, stderr, 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 {

View File

@ -10,6 +10,7 @@ import (
var version = flag.Bool("version", false, "print version information and exit")
var list = flag.Bool("list", false, "list mode")
var host = flag.String("host", "", "host mode")
var inventory = flag.Bool("inventory", false, "inventory mode")
func main() {
flag.Parse()
@ -44,7 +45,7 @@ func main() {
os.Exit(1)
}
if !*list && *host == "" {
if !*list && *host == "" && !*inventory {
fmt.Fprint(os.Stderr, "Either --host or --list must be specified")
os.Exit(1)
}
@ -72,6 +73,9 @@ func main() {
if *list {
os.Exit(cmdList(os.Stdout, os.Stderr, &s))
} else if *inventory {
os.Exit(cmdInventory(os.Stdout, os.Stderr, &s))
} else if *host != "" {
os.Exit(cmdHost(os.Stdout, os.Stderr, &s, *host))