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.
This commit is contained in:
James Bardin 2019-05-29 12:58:04 -05:00
parent c017149b31
commit 06dfc4abd8
2 changed files with 23 additions and 1 deletions

View File

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

View File

@ -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="<nil>", got path="%v"`, cfg["path"])
}
}
func TestInit_backendConfigKVReInitWithConfigDiff(t *testing.T) {