github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/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 args = c.Meta.process(args) 20 21 cmdFlags := flag.NewFlagSet("graph", flag.ContinueOnError) 22 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 23 if err := cmdFlags.Parse(args); err != nil { 24 return 1 25 } 26 27 var path string 28 args = cmdFlags.Args() 29 if len(args) > 1 { 30 c.Ui.Error("The graph command expects one argument.\n") 31 cmdFlags.Usage() 32 return 1 33 } else if len(args) == 1 { 34 path = args[0] 35 } else { 36 var err error 37 path, err = os.Getwd() 38 if err != nil { 39 c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err)) 40 } 41 } 42 43 ctx, _, err := c.Context(path, "") 44 if err != nil { 45 c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err)) 46 return 1 47 } 48 49 g, err := ctx.Graph() 50 if err != nil { 51 c.Ui.Error(fmt.Sprintf("Error creating graph: %s", err)) 52 return 1 53 } 54 55 c.Ui.Output(terraform.GraphDot(g)) 56 57 return 0 58 } 59 60 func (c *GraphCommand) Help() string { 61 helpText := ` 62 Usage: terraform graph [options] PATH 63 64 Outputs the visual graph of Terraform resources. If the path given is 65 the path to a configuration, the dependency graph of the resources are 66 shown. If the path is a plan file, then the dependency graph of the 67 plan itself is shown. 68 69 The graph is outputted in DOT format. The typical program that can 70 read this format is GraphViz, but many web services are also available 71 to read this format. 72 73 ` 74 return strings.TrimSpace(helpText) 75 } 76 77 func (c *GraphCommand) Synopsis() string { 78 return "Create a visual graph of Terraform resources" 79 }