From 06dfc4abd8f132fb4909b0dd2c8f0c63bd422d6a Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 29 May 2019 12:58:04 -0500 Subject: [PATCH] allow setting -backend-config='' to unset override There is currently no way to unset -backend-config during init, since not setting that option assumes the user will use the saved config. Allow setting `-backend-config=""` to specify no overrides. --- command/init.go | 8 +++++++- command/init_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/command/init.go b/command/init.go index b8c8faab5..c4e9554df 100644 --- a/command/init.go +++ b/command/init.go @@ -299,7 +299,7 @@ func (c *InitCommand) Run(args []string) int { if back == nil { // If we didn't initialize a backend then we'll try to at least - // instantiate one. This might fail if it wasn't already initalized + // instantiate one. This might fail if it wasn't already initialized // by a previous run, so we must still expect that "back" may be nil // in code that follows. var backDiags tfdiags.Diagnostics @@ -675,6 +675,12 @@ func (c *InitCommand) backendConfigOverrideBody(flags rawFlags, schema *configsc synthVals = make(map[string]cty.Value) } + if len(items) == 1 && items[0].Value == "" { + // Explicitly remove all -backend-config options. + // We do this by setting an empty but non-nil ConfigOverrides. + return configs.SynthBody("-backend-config=''", synthVals), diags + } + for _, item := range items { eq := strings.Index(item.Value, "=") diff --git a/command/init_test.go b/command/init_test.go index bda5fe06e..dfae2e02a 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -454,6 +454,22 @@ func TestInit_backendConfigKVReInit(t *testing.T) { if cfg["path"] != "test" { t.Fatalf(`expected backend path="test", got path="%v"`, cfg["path"]) } + + // override the -backend-config options by settings + args = []string{"-input=false", "-backend-config", ""} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + // make sure the backend is configured how we expect + configState = testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) + cfg = map[string]interface{}{} + if err := json.Unmarshal(configState.Backend.ConfigRaw, &cfg); err != nil { + t.Fatal(err) + } + if cfg["path"] != nil { + t.Fatalf(`expected backend path="", got path="%v"`, cfg["path"]) + } } func TestInit_backendConfigKVReInitWithConfigDiff(t *testing.T) {