cli: Fix overly broad auto-approve argument

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.
This commit is contained in:
Alisdair McDiarmid 2021-02-23 10:03:15 -05:00
parent 8d9a08e3a1
commit b7f54b30d5
4 changed files with 16 additions and 9 deletions

View File

@ -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
@ -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
@ -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()

View File

@ -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
@ -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 {

View File

@ -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,

View File

@ -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
@ -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")