Skip to content

Commit

Permalink
cli: Remove positional plan argument from graph
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alisdair committed Feb 2, 2021
1 parent ca23a09 commit 888f36a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
15 changes: 7 additions & 8 deletions command/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,22 @@ func (c *GraphCommand) Run(args []string) int {
var graphTypeStr string
var moduleDepth int
var verbose bool
var planPath string

args = c.Meta.process(args)
cmdFlags := c.Meta.defaultFlagSet("graph")
cmdFlags.BoolVar(&drawCycles, "draw-cycles", false, "draw-cycles")
cmdFlags.StringVar(&graphTypeStr, "type", "", "type")
cmdFlags.IntVar(&moduleDepth, "module-depth", -1, "module-depth")
cmdFlags.BoolVar(&verbose, "verbose", false, "verbose")
cmdFlags.StringVar(&planPath, "plan", "", "plan")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

args = cmdFlags.Args()
var planPath string
if len(args) > 0 {
planPath = args[0]
args = args[1:]
}
configPath, err := ModulePath(args)
configPath, err := ModulePath(cmdFlags.Args())
if err != nil {
c.Ui.Error(err.Error())
return 1
Expand Down Expand Up @@ -170,7 +166,7 @@ func (c *GraphCommand) Help() string {
Usage: terraform graph [options]
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
read this format is GraphViz, but many web services are also available
Expand All @@ -184,6 +180,9 @@ Usage: terraform graph [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.
This helps when diagnosing cycle errors.
Expand Down
2 changes: 1 addition & 1 deletion command/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestGraph_plan(t *testing.T) {
}

args := []string{
planPath,
"-plan", planPath,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
Expand Down
11 changes: 7 additions & 4 deletions website/docs/cli/commands/graph.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ The output is in the DOT format, which can be used by

Usage: `terraform graph [options]`

Outputs the visual dependency graph of Terraform resources represented by the
configuration in the current working directory.
Outputs the visual execution graph of Terraform resources according to
either the current configuration or an execution plan.

The graph is outputted in DOT format. The typical program that can
read this format is GraphViz, but many web services are also available
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
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
argument.

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.
This helps when diagnosing cycle errors.

Expand All @@ -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
by GraphViz:

```shell
```shellsession
$ terraform graph | dot -Tsvg > graph.svg
```

Expand Down

0 comments on commit 888f36a

Please sign in to comment.