mirror of
https://github.com/adammck/terraform-inventory
synced 2024-11-22 20:01:58 +01:00
Merge pull request #31 from andrefbsantos/add_inventory_option
Added the inventory option to produce an output similar to Ansible host file
This commit is contained in:
commit
3d2948144e
36
cli.go
36
cli.go
@ -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 {
|
||||
|
6
main.go
6
main.go
@ -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))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user