mirror of
https://github.com/adammck/terraform-inventory
synced 2024-11-23 00:12:13 +01:00
2d1c7fdfd9
Closes #1
55 lines
925 B
Go
55 lines
925 B
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
var list = flag.Bool("list", false, "list mode")
|
|
var host = flag.String("host", "", "host mode")
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
file := flag.Arg(0)
|
|
|
|
if file == "" {
|
|
fmt.Printf("Usage: %s [options] path\n", os.Args[0])
|
|
os.Exit(1)
|
|
}
|
|
|
|
if !*list && *host == "" {
|
|
fmt.Println("Either --host or --list must be specified")
|
|
os.Exit(1)
|
|
}
|
|
|
|
path, err := filepath.Abs(file)
|
|
if err != nil {
|
|
fmt.Printf("Invalid file: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
stateFile, err := os.Open(path)
|
|
defer stateFile.Close()
|
|
if err != nil {
|
|
fmt.Printf("Error opening tfstate file: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
var s state
|
|
err = s.read(stateFile)
|
|
if err != nil {
|
|
fmt.Printf("Error reading tfstate file: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *list {
|
|
os.Exit(cmdList(os.Stdout, os.Stderr, &s))
|
|
|
|
} else if *host != "" {
|
|
os.Exit(cmdHost(os.Stdout, os.Stderr, &s, *host))
|
|
|
|
}
|
|
}
|