github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/command/graph.go (about) 1 package command 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 "strings" 8 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 // GraphCommand is a Command implementation that takes a Terraform 13 // configuration and outputs the dependency tree in graphical form. 14 type GraphCommand struct { 15 Meta 16 } 17 18 func (c *GraphCommand) Run(args []string) int { 19 var moduleDepth int 20 21 args = c.Meta.process(args, false) 22 23 cmdFlags := flag.NewFlagSet("graph", flag.ContinueOnError) 24 cmdFlags.IntVar(&moduleDepth, "module-depth", 0, "module-depth") 25 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 26 if err := cmdFlags.Parse(args); err != nil { 27 return 1 28 } 29 30 var path string 31 args = cmdFlags.Args() 32 if len(args) > 1 { 33 c.Ui.Error("The graph command expects one argument.\n") 34 cmdFlags.Usage() 35 return 1 36 } else if len(args) == 1 { 37 path = args[0] 38 } else { 39 var err error 40 path, err = os.Getwd() 41 if err != nil { 42 c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err)) 43 } 44 } 45 46 ctx, _, err := c.Context(contextOpts{ 47 Path: path, 48 StatePath: "", 49 }) 50 if err != nil { 51 c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err)) 52 return 1 53 } 54 55 g, err := ctx.Graph() 56 if err != nil { 57 c.Ui.Error(fmt.Sprintf("Error creating graph: %s", err)) 58 return 1 59 } 60 61 opts := &terraform.GraphDotOpts{ 62 ModuleDepth: moduleDepth, 63 } 64 65 c.Ui.Output(terraform.GraphDot(g, opts)) 66 67 return 0 68 } 69 70 func (c *GraphCommand) Help() string { 71 helpText := ` 72 Usage: terraform graph [options] PATH 73 74 Outputs the visual graph of Terraform resources. If the path given is 75 the path to a configuration, the dependency graph of the resources are 76 shown. If the path is a plan file, then the dependency graph of the 77 plan itself is shown. 78 79 The graph is outputted in DOT format. The typical program that can 80 read this format is GraphViz, but many web services are also available 81 to read this format. 82 83 Options: 84 85 -module-depth=n The maximum depth to expand modules. By default this is 86 zero, which will not expand modules at all. 87 88 ` 89 return strings.TrimSpace(helpText) 90 } 91 92 func (c *GraphCommand) Synopsis() string { 93 return "Create a visual graph of Terraform resources" 94 }