-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
87 lines (69 loc) · 1.42 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
package main
import (
_ "embed"
"flag"
"fmt"
"os"
"github.com/busser/tftree/internal/format"
"github.com/busser/tftree/internal/terraform"
"github.com/busser/tftree/internal/tftree"
)
func main() {
if err := run(); err != nil {
os.Stderr.WriteString(format.Error(err))
os.Exit(1)
}
}
//go:embed VERSION
var version string
func run() error {
parseFlags()
if noColor {
format.NoColor = true
}
if printVersion {
fmt.Println(version)
return nil
}
var workdir string
switch {
case len(os.Args) > 2:
return fmt.Errorf("too many arguments, must be either 0 or 1")
case len(os.Args) == 2:
workdir = os.Args[1]
default:
workdir = "."
}
tf := terraform.NewRunner(workdir, terraformBin)
logln("Running \"terraform init\"...")
err := tf.Init()
if err != nil {
return err
}
logln("Running \"terraform plan\"...")
plan, err := tf.Plan()
if err != nil {
return err
}
root, err := tftree.New(plan, workdir)
if err != nil {
return err
}
fmt.Print(format.Module(root))
return nil
}
func logln(msg string) {
fmt.Fprint(os.Stderr, format.Info(msg))
}
// Flags
var (
noColor bool
printVersion bool
terraformBin string
)
func parseFlags() {
flag.BoolVar(&noColor, "no-color", false, "disable color in output")
flag.BoolVar(&printVersion, "version", false, "print version and exit")
flag.StringVar(&terraformBin, "terraform-bin", "terraform", "terraform binary to use")
flag.Parse()
}