mirror of https://github.com/hashicorp/terraform
parent
ac7206c919
commit
f9cfdf1ebe
@ -0,0 +1,63 @@
|
||||
// Copyright IBM Corp. 2014, 2026
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package arguments
|
||||
|
||||
import "github.com/hashicorp/terraform/internal/tfdiags"
|
||||
|
||||
// Graph represents the command-line arguments for the graph command.
|
||||
type Graph struct {
|
||||
// DrawCycles highlights any cycles in the graph with colored edges.
|
||||
DrawCycles bool
|
||||
|
||||
// GraphType is the type of operation graph to output (plan,
|
||||
// plan-refresh-only, plan-destroy, or apply). Empty string means the
|
||||
// default resource-dependency summary.
|
||||
GraphType string
|
||||
|
||||
// ModuleDepth is a deprecated option that was used in prior versions to
|
||||
// control the depth of modules shown.
|
||||
ModuleDepth int
|
||||
|
||||
// Verbose enables verbose graph output.
|
||||
Verbose bool
|
||||
|
||||
// Plan is the path to a saved plan file to render as a graph.
|
||||
Plan string
|
||||
}
|
||||
|
||||
// ParseGraph processes CLI arguments, returning a Graph value and errors.
|
||||
// If errors are encountered, a Graph value is still returned representing
|
||||
// the best effort interpretation of the arguments.
|
||||
func ParseGraph(args []string) (*Graph, tfdiags.Diagnostics) {
|
||||
var diags tfdiags.Diagnostics
|
||||
graph := &Graph{
|
||||
ModuleDepth: -1,
|
||||
}
|
||||
|
||||
cmdFlags := defaultFlagSet("graph")
|
||||
cmdFlags.BoolVar(&graph.DrawCycles, "draw-cycles", false, "draw-cycles")
|
||||
cmdFlags.StringVar(&graph.GraphType, "type", "", "type")
|
||||
cmdFlags.IntVar(&graph.ModuleDepth, "module-depth", -1, "module-depth")
|
||||
cmdFlags.BoolVar(&graph.Verbose, "verbose", false, "verbose")
|
||||
cmdFlags.StringVar(&graph.Plan, "plan", "", "plan")
|
||||
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
err.Error(),
|
||||
))
|
||||
}
|
||||
|
||||
args = cmdFlags.Args()
|
||||
if len(args) > 0 {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Too many command line arguments",
|
||||
"Expected no positional arguments. Did you mean to use -chdir?",
|
||||
))
|
||||
}
|
||||
|
||||
return graph, diags
|
||||
}
|
||||
@ -0,0 +1,147 @@
|
||||
// Copyright IBM Corp. 2014, 2026
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package arguments
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
)
|
||||
|
||||
func TestParseGraph_valid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *Graph
|
||||
}{
|
||||
"defaults": {
|
||||
nil,
|
||||
&Graph{
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
},
|
||||
"plan type": {
|
||||
[]string{"-type=plan"},
|
||||
&Graph{
|
||||
GraphType: "plan",
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
},
|
||||
"apply type": {
|
||||
[]string{"-type=apply"},
|
||||
&Graph{
|
||||
GraphType: "apply",
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
},
|
||||
"draw-cycles": {
|
||||
[]string{"-draw-cycles", "-type=plan"},
|
||||
&Graph{
|
||||
DrawCycles: true,
|
||||
GraphType: "plan",
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
},
|
||||
"plan file": {
|
||||
[]string{"-plan=tfplan"},
|
||||
&Graph{
|
||||
Plan: "tfplan",
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
},
|
||||
"verbose": {
|
||||
[]string{"-verbose"},
|
||||
&Graph{
|
||||
Verbose: true,
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
},
|
||||
"module-depth": {
|
||||
[]string{"-module-depth=2"},
|
||||
&Graph{
|
||||
ModuleDepth: 2,
|
||||
},
|
||||
},
|
||||
"all flags": {
|
||||
[]string{"-draw-cycles", "-type=plan-destroy", "-plan=tfplan", "-verbose", "-module-depth=3"},
|
||||
&Graph{
|
||||
DrawCycles: true,
|
||||
GraphType: "plan-destroy",
|
||||
Plan: "tfplan",
|
||||
Verbose: true,
|
||||
ModuleDepth: 3,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseGraph(tc.args)
|
||||
if len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
}
|
||||
if diff := cmp.Diff(tc.want, got); diff != "" {
|
||||
t.Fatalf("unexpected result\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseGraph_invalid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *Graph
|
||||
wantDiags tfdiags.Diagnostics
|
||||
}{
|
||||
"unknown flag": {
|
||||
[]string{"-wat"},
|
||||
&Graph{
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
"flag provided but not defined: -wat",
|
||||
),
|
||||
},
|
||||
},
|
||||
"positional argument": {
|
||||
[]string{"extra"},
|
||||
&Graph{
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Too many command line arguments",
|
||||
"Expected no positional arguments. Did you mean to use -chdir?",
|
||||
),
|
||||
},
|
||||
},
|
||||
"too many positional arguments": {
|
||||
[]string{"bad", "bad"},
|
||||
&Graph{
|
||||
ModuleDepth: -1,
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Too many command line arguments",
|
||||
"Expected no positional arguments. Did you mean to use -chdir?",
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, gotDiags := ParseGraph(tc.args)
|
||||
if diff := cmp.Diff(tc.want, got); diff != "" {
|
||||
t.Fatalf("unexpected result\n%s", diff)
|
||||
}
|
||||
tfdiags.AssertDiagnosticsMatch(t, gotDiags, tc.wantDiags)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue