Skip to content

Commit

Permalink
cli: Fix overly broad auto-approve argument
Browse files Browse the repository at this point in the history
The auto-approve argument was part of the arguments.Operation type,
which resulted in adding a silent -auto-approve flag to plan and
refresh. This was unintended, and is fixed in this commit by moving the
flag to the arguments.Apply type and updating the downstream callers.
  • Loading branch information
alisdair committed Feb 23, 2021
1 parent 8d9a08e commit b7f54b3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
11 changes: 8 additions & 3 deletions command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *ApplyCommand) Run(rawArgs []string) int {
}

// Build the operation request
opReq, opDiags := c.OperationRequest(be, view, planFile, args.Operation)
opReq, opDiags := c.OperationRequest(be, view, planFile, args.Operation, args.AutoApprove)
diags = diags.Append(opDiags)

// Collect variable value and add them to the operation request
Expand Down Expand Up @@ -226,7 +226,12 @@ func (c *ApplyCommand) PrepareBackend(planFile *planfile.Reader, args *arguments
return be, diags
}

func (c *ApplyCommand) OperationRequest(be backend.Enhanced, view views.Apply, planFile *planfile.Reader, args *arguments.Operation,
func (c *ApplyCommand) OperationRequest(
be backend.Enhanced,
view views.Apply,
planFile *planfile.Reader,
args *arguments.Operation,
autoApprove bool,
) (*backend.Operation, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics

Expand All @@ -237,7 +242,7 @@ func (c *ApplyCommand) OperationRequest(be backend.Enhanced, view views.Apply, p

// Build the operation
opReq := c.Operation(be)
opReq.AutoApprove = args.AutoApprove
opReq.AutoApprove = autoApprove
opReq.ConfigDir = "."
opReq.Destroy = c.Destroy
opReq.Hooks = view.Hooks()
Expand Down
4 changes: 4 additions & 0 deletions command/arguments/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Apply struct {
Operation *Operation
Vars *Vars

// AutoApprove skips the manual verification step for the apply operation.
AutoApprove bool

// InputEnabled is used to disable interactive input for unspecified
// variable and backend config values. Default is true.
InputEnabled bool
Expand All @@ -34,6 +37,7 @@ func ParseApply(args []string) (*Apply, tfdiags.Diagnostics) {
}

cmdFlags := extendedFlagSet("apply", apply.State, apply.Operation, apply.Vars)
cmdFlags.BoolVar(&apply.AutoApprove, "auto-approve", false, "auto-approve")
cmdFlags.BoolVar(&apply.InputEnabled, "input", true, "input")

if err := cmdFlags.Parse(args); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions command/arguments/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ func TestParseApply_basicValid(t *testing.T) {
"defaults": {
nil,
&Apply{
AutoApprove: false,
InputEnabled: true,
PlanPath: "",
ViewType: ViewHuman,
},
},
"disabled input and plan path": {
[]string{"-input=false", "saved.tfplan"},
"auto-approve, disabled input, and plan path": {
[]string{"-auto-approve", "-input=false", "saved.tfplan"},
&Apply{
AutoApprove: true,
InputEnabled: false,
PlanPath: "saved.tfplan",
ViewType: ViewHuman,
Expand Down
4 changes: 0 additions & 4 deletions command/arguments/extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ type State struct {
// Operation describes arguments which are used to configure how a Terraform
// operation such as a plan or apply executes.
type Operation struct {
// AutoApprove skips the manual verification step for the apply operation.
AutoApprove bool

// Parallelism is the limit Terraform places on total parallel operations
// as it walks the dependency graph.
Parallelism int
Expand Down Expand Up @@ -141,7 +138,6 @@ func extendedFlagSet(name string, state *State, operation *Operation, vars *Vars
}

if operation != nil {
f.BoolVar(&operation.AutoApprove, "auto-approve", false, "auto-approve")
f.IntVar(&operation.Parallelism, "parallelism", DefaultParallelism, "parallelism")
f.BoolVar(&operation.Refresh, "refresh", true, "refresh")
f.Var((*flagStringSlice)(&operation.targetsRaw), "target", "target")
Expand Down

0 comments on commit b7f54b3

Please sign in to comment.