-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform-index.go
61 lines (48 loc) · 1.1 KB
/
terraform-index.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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"fmt"
"github.com/mauve/terraform-index/index"
)
const (
BINARY = "terraform-index"
)
func Contents(path string) ([]byte, error) {
if path == "-" {
return ioutil.ReadAll(os.Stdin)
}
return ioutil.ReadFile(path)
}
func main() {
includeRaw := flag.Bool("raw-ast", false, "include the raw ast")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options] <paths>\n\n", BINARY)
fmt.Fprintf(os.Stderr, "Extracts references and declarations from Terraform files\n")
flag.PrintDefaults()
}
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
index := index.NewIndex()
for _, path := range flag.Args() {
source, err := Contents(path)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Cannot open path '%s': %s\n", path, err)
os.Exit(2)
}
err = index.CollectString(source, path, *includeRaw)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not parse '%s': %s\n", path, err)
}
}
json, err := json.MarshalIndent(index, "", " ")
if err != nil {
os.Exit(3)
}
os.Stdout.Write(json)
}