forked from adammck/terraform-inventory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
119 lines (96 loc) · 2.84 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/adammck/venv"
"github.com/blang/vfs"
)
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()
file := flag.Arg(0)
if *version == true {
fmt.Printf("%s version %s\n", os.Args[0], versionInfo())
return
}
fs := vfs.OS()
if file == "" {
env := venv.OS()
file = GetInputPath(fs, env)
}
if !*list && *host == "" && !*inventory {
fmt.Fprint(os.Stderr, "Either --host or --list must be specified")
os.Exit(1)
}
path, err := filepath.Abs(file)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid file: %s\n", err)
os.Exit(1)
}
f, err := fs.Stat(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid file: %s\n", err)
os.Exit(1)
}
var s stateAnyTerraformVersion
if !f.IsDir() {
stateFile, err := os.Open(path)
defer stateFile.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening tfstate file: %s\n", err)
os.Exit(1)
}
err = s.read(stateFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading tfstate file: %s\n", err)
os.Exit(1)
}
}
if f.IsDir() {
cmd := exec.Command("terraform", "show", "-json")
cmd.Dir = path
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error running `terraform show -json` in directory %s, %s, falling back to trying Terraform pre-0.12 command\n", path, err)
cmd = exec.Command("terraform", "state", "pull")
cmd.Dir = path
out.Reset()
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error running `terraform state pull` in directory %s, %s\n", path, err)
os.Exit(1)
}
}
err = s.read(&out)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading Terraform state: %s\n", err)
os.Exit(1)
}
}
if s.TerraformVersion == TerraformVersionUnknown {
fmt.Fprintf(os.Stderr, "Unknown state format\n\nUsage: %s [options] path\npath: this is either a path to a state file or a folder from which `terraform commands` are valid\n", os.Args[0])
os.Exit(1)
}
if (s.TerraformVersion == TerraformVersionPre0dot12 && s.StatePre0dot12.Modules == nil) ||
(s.TerraformVersion == TerraformVersion0dot12 && s.State0dot12.Values.RootModule == nil) {
fmt.Fprintf(os.Stderr, "No modules found in state\n\nUsage: %s [options] path\npath: this is either a path to a state file or a folder from which `terraform commands` are valid\n", os.Args[0])
os.Exit(1)
}
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))
}
}