From 8e51363f041bd7374562a3a180bddf359091388f Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 16 Oct 2018 07:47:32 -0700 Subject: [PATCH] command: Don't allow -var and -var-file when applying saved plan This reinstates an old behavior that was lost in the reorganization of how we deal with the -var and -var-file options. This fix is verified by TestApply_planVars now passing. --- command/apply.go | 14 ++++++++++++-- command/meta_config.go | 7 +++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/command/apply.go b/command/apply.go index 4b96781bc..5a317b8d6 100644 --- a/command/apply.go +++ b/command/apply.go @@ -56,6 +56,8 @@ func (c *ApplyCommand) Run(args []string) int { return 1 } + var diags tfdiags.Diagnostics + // Get the args. The "maybeInit" flag tracks whether we may need to // initialize the configuration from a remote path. This is true as long // as we have an argument. @@ -112,9 +114,17 @@ func (c *ApplyCommand) Run(args []string) int { if planFile != nil { // Reset the config path for backend loading configPath = "" - } - var diags tfdiags.Diagnostics + if !c.variableArgs.Empty() { + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Can't set variables when applying a saved plan", + "The -var and -var-file options cannot be used when applying a saved plan file, because a saved plan includes the variable values that were set when it was created.", + )) + c.showDiagnostics(diags) + return 1 + } + } // Load the backend var be backend.Enhanced diff --git a/command/meta_config.go b/command/meta_config.go index a3095b1f3..84f127a8b 100644 --- a/command/meta_config.go +++ b/command/meta_config.go @@ -384,6 +384,13 @@ func newRawFlags(flagName string) rawFlags { } } +func (f rawFlags) Empty() bool { + if f.items == nil { + return true + } + return len(*f.items) == 0 +} + func (f rawFlags) AllItems() []rawFlag { return *f.items }