-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
111 lines (96 loc) · 3.03 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
package main
import (
"flag"
"fmt"
"os"
"github.com/steeve85/tfviz/utils"
"github.com/steeve85/tfviz/aws"
)
var exportFormats = []string{"dot", "jpeg", "pdf", "png"}
func main() {
inputFlag := flag.String("input", ".", "Path to Terraform file or directory ")
outputFlag := flag.String("output", "tfviz.bin", "Path to the exported file")
formatFlag := flag.String("format", "png", "Format for the output file: dot, jpeg, pdf, png")
disableEdge := flag.Bool("disableedges", false, "Set to disable edges (Security Groups rules) on the graph")
verbose := flag.Bool("verbose", false, "Set to enable verbose output")
flag.BoolVar(&utils.Ignorewarnings, "ignorewarnings", false, "Set to ignore warning messages")
flag.BoolVar(&aws.IgnoreIngress, "ignoreingress", false, "Set to ignore ingress rules")
flag.BoolVar(&aws.IgnoreEgress, "ignoreegress", false, "Set to ignore egress rules")
flag.Parse()
// Verbose mode
if *verbose {
aws.Verbose = true
utils.Verbose = true
}
// checking that export format is supported
_, found := utils.Find(exportFormats, *formatFlag)
if !found {
fmt.Printf("[ERROR] File format %s is not supported. Quitting...\n", *formatFlag)
os.Exit(1)
}
// check that the export path does not already exist
if _, err := os.Stat(*outputFlag); err == nil {
fmt.Printf("[ERROR] File %s already exists. Quitting...\n", *outputFlag)
os.Exit(1)
}
stepsNb := 7
if *disableEdge {
stepsNb--
}
fmt.Printf("[1/%d] ", stepsNb)
tfModule, err := utils.ParseTFfile(*inputFlag)
if err != nil {
// invalid input directory/file
utils.PrintError(err)
os.Exit(1)
}
fmt.Printf("[2/%d] Initiating variables and Terraform references\n", stepsNb)
ctx, err := aws.InitiateVariablesAndResources(tfModule)
if err != nil {
utils.PrintError(err)
os.Exit(1)
}
graph, err := utils.InitiateGraph()
if err != nil {
utils.PrintError(err)
os.Exit(1)
}
tfAws := &aws.Data{
Vpc: make(map[string]aws.Vpc),
Subnet: make(map[string]aws.Subnet),
Instance: make(map[string]aws.Instance),
SecurityGroup: make(map[string]aws.SecurityGroup),
DBInstance: make(map[string]aws.DBInstance),
DBSubnetGroup: make(map[string]aws.DBSubnetGroup),
S3: make(map[string]aws.S3),
SecurityGroupNodeLinks: make(map[string][]string),
}
fmt.Printf("[3/%d] Creating default nodes (if needed)\n", stepsNb)
err = tfAws.CreateDefaultNodes(tfModule, graph)
if err != nil {
utils.PrintError(err)
}
fmt.Printf("[4/%d] Parsing TF resources\n", stepsNb)
err = tfAws.ParseTfResources(tfModule, ctx, graph)
if err != nil {
utils.PrintError(err)
}
fmt.Printf("[5/%d] Creating Graph nodes\n", stepsNb)
err = tfAws.CreateGraphNodes(graph)
if err != nil {
utils.PrintError(err)
}
if !*disableEdge {
fmt.Printf("[6/%d] Creating Graph edges\n", stepsNb)
err = tfAws.CreateGraphEdges(graph)
if err != nil {
utils.PrintError(err)
}
}
fmt.Printf("[%d/%d] ", stepsNb, stepsNb)
err = utils.ExportGraphToFile(*outputFlag, *formatFlag, graph)
if err != nil {
utils.PrintError(err)
}
tfAws.PrintUnsupportedResources()
}