Repository navigation
Expand file tree
/
Copy pathgraph.go
More file actions
75 lines (62 loc) · 1.68 KB
/
Copy pathgraph.go
File metadata and controls
75 lines (62 loc) · 1.68 KB
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
package command
import (
"flag"
"fmt"
"os"
"strings"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/digraph"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
// GraphCommand is a Command implementation that takes a Terraform
// configuration and outputs the dependency tree in graphical form.
type GraphCommand struct {
TFConfig *terraform.Config
Ui cli.Ui
}
func (c *GraphCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("graph", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
args = cmdFlags.Args()
if len(args) != 1 {
c.Ui.Error("The graph command expects one argument.\n")
cmdFlags.Usage()
return 1
}
conf, err := config.Load(args[0])
if err != nil {
c.Ui.Error(fmt.Sprintf("Error loading config: %s", err))
return 1
}
g, err := terraform.Graph(&terraform.GraphOpts{
Config: conf,
Providers: c.TFConfig.Providers,
})
if err != nil {
c.Ui.Error(fmt.Sprintf("Error creating graph: %s", err))
return 1
}
nodes := make([]digraph.Node, len(g.Nouns))
for i, n := range g.Nouns {
nodes[i] = n
}
digraph.GenerateDot(nodes, os.Stdout)
return 0
}
func (c *GraphCommand) Help() string {
helpText := `
Usage: terraform graph [options] PATH
Outputs the visual graph of Terraform resources. If the path given is
the path to a configuration, the dependency graph of the resources are
shown. If the path is a plan file, then the dependency graph of the
plan itself is shown.
`
return strings.TrimSpace(helpText)
}
func (c *GraphCommand) Synopsis() string {
return "Output visual graph of Terraform resources"
}