cli: Remove positional plan argument from graph

To make the command arguments easier to understand and extend, we are
moving away from positional arguments. This commit changes the graph
command to take a `-plan` flag instead of an optional trailing path.
This commit is contained in:
Alisdair McDiarmid 2021-02-02 13:09:30 -05:00
parent ca23a096d8
commit 888f36aebb
3 changed files with 15 additions and 13 deletions

View File

@ -23,6 +23,7 @@ func (c *GraphCommand) Run(args []string) int {
var graphTypeStr string var graphTypeStr string
var moduleDepth int var moduleDepth int
var verbose bool var verbose bool
var planPath string
args = c.Meta.process(args) args = c.Meta.process(args)
cmdFlags := c.Meta.defaultFlagSet("graph") cmdFlags := c.Meta.defaultFlagSet("graph")
@ -30,19 +31,14 @@ func (c *GraphCommand) Run(args []string) int {
cmdFlags.StringVar(&graphTypeStr, "type", "", "type") cmdFlags.StringVar(&graphTypeStr, "type", "", "type")
cmdFlags.IntVar(&moduleDepth, "module-depth", -1, "module-depth") cmdFlags.IntVar(&moduleDepth, "module-depth", -1, "module-depth")
cmdFlags.BoolVar(&verbose, "verbose", false, "verbose") cmdFlags.BoolVar(&verbose, "verbose", false, "verbose")
cmdFlags.StringVar(&planPath, "plan", "", "plan")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil { if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error())) c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1 return 1
} }
args = cmdFlags.Args() configPath, err := ModulePath(cmdFlags.Args())
var planPath string
if len(args) > 0 {
planPath = args[0]
args = args[1:]
}
configPath, err := ModulePath(args)
if err != nil { if err != nil {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
return 1 return 1
@ -170,7 +166,7 @@ func (c *GraphCommand) Help() string {
Usage: terraform graph [options] Usage: terraform graph [options]
Outputs the visual execution graph of Terraform resources according to Outputs the visual execution graph of Terraform resources according to
configuration files in the current directory. either the current configuration or an execution plan.
The graph is outputted in DOT format. The typical program that can The graph is outputted in DOT format. The typical program that can
read this format is GraphViz, but many web services are also available read this format is GraphViz, but many web services are also available
@ -184,6 +180,9 @@ Usage: terraform graph [options]
Options: Options:
-plan=tfplan Render graph using the specified plan file instead of the
configuration in the current directory.
-draw-cycles Highlight any cycles in the graph with colored edges. -draw-cycles Highlight any cycles in the graph with colored edges.
This helps when diagnosing cycle errors. This helps when diagnosing cycle errors.

View File

@ -150,7 +150,7 @@ func TestGraph_plan(t *testing.T) {
} }
args := []string{ args := []string{
planPath, "-plan", planPath,
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) t.Fatalf("bad: \n%s", ui.ErrorWriter.String())

View File

@ -18,14 +18,14 @@ The output is in the DOT format, which can be used by
Usage: `terraform graph [options]` Usage: `terraform graph [options]`
Outputs the visual dependency graph of Terraform resources represented by the Outputs the visual execution graph of Terraform resources according to
configuration in the current working directory. either the current configuration or an execution plan.
The graph is outputted in DOT format. The typical program that can The graph is outputted in DOT format. The typical program that can
read this format is GraphViz, but many web services are also available read this format is GraphViz, but many web services are also available
to read this format. to read this format.
The -type flag can be used to control the type of graph shown. Terraform The `-type` flag can be used to control the type of graph shown. Terraform
creates different graphs for different operations. See the options below creates different graphs for different operations. See the options below
for the list of types supported. The default type is "plan" if a for the list of types supported. The default type is "plan" if a
configuration is given, and "apply" if a plan file is passed as an configuration is given, and "apply" if a plan file is passed as an
@ -33,6 +33,9 @@ argument.
Options: Options:
* `-plan=tfplan` - Render graph using the specified plan file instead of the
configuration in the current directory.
* `-draw-cycles` - Highlight any cycles in the graph with colored edges. * `-draw-cycles` - Highlight any cycles in the graph with colored edges.
This helps when diagnosing cycle errors. This helps when diagnosing cycle errors.
@ -48,7 +51,7 @@ The output of `terraform graph` is in the DOT format, which can
easily be converted to an image by making use of `dot` provided easily be converted to an image by making use of `dot` provided
by GraphViz: by GraphViz:
```shell ```shellsession
$ terraform graph | dot -Tsvg > graph.svg $ terraform graph | dot -Tsvg > graph.svg
``` ```