From 716132431a95ef90f88839ff5937c1040132ddea Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 15 Feb 2017 15:44:53 -0800 Subject: [PATCH] command/init: initialize backend even if not set in the config We need to initialize the backend even if the config has no backend set. This allows `init` to work when unsetting a previously set backend. Without this, there was no way to unset a backend. --- command/init.go | 14 +++++++---- command/init_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/command/init.go b/command/init.go index 3ffd2898c..d20e883bc 100644 --- a/command/init.go +++ b/command/init.go @@ -125,13 +125,17 @@ func (c *InitCommand) Run(args []string) int { } // If we're requesting backend configuration and configure it - hasBackend := conf.Terraform != nil && conf.Terraform.Backend != nil - if flagBackend && hasBackend { + if flagBackend { header = true - c.Ui.Output(c.Colorize().Color(fmt.Sprintf( - "[reset][bold]" + - "Initializing the backend..."))) + // Only output that we're initializing a backend if we have + // something in the config. We can be UNSETTING a backend as well + // in which case we choose not to show this. + if conf.Terraform != nil && conf.Terraform.Backend != nil { + c.Ui.Output(c.Colorize().Color(fmt.Sprintf( + "[reset][bold]" + + "Initializing the backend..."))) + } opts := &BackendOpts{ ConfigPath: path, diff --git a/command/init_test.go b/command/init_test.go index 4c8e4908e..6030b877e 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -1,6 +1,7 @@ package command import ( + "io/ioutil" "os" "path/filepath" "strings" @@ -236,6 +237,63 @@ func TestInit_backend(t *testing.T) { } } +func TestInit_backendUnset(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + copy.CopyDir(testFixturePath("init-backend"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + { + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + }, + } + + // Init + args := []string{} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + if _, err := os.Stat(filepath.Join(DefaultDataDir, DefaultStateFilename)); err != nil { + t.Fatalf("err: %s", err) + } + } + + { + // Unset + if err := ioutil.WriteFile("main.tf", []byte(""), 0644); err != nil { + t.Fatalf("err: %s", err) + } + + // Run it again + defer testInteractiveInput(t, []string{"yes", "yes"})() + + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + }, + } + + args := []string{} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + s := testStateRead(t, filepath.Join( + DefaultDataDir, DefaultStateFilename)) + if !s.Backend.Empty() { + t.Fatal("should not have backend config") + } + } +} + func TestInit_backendConfigFile(t *testing.T) { // Create a temporary working directory that is empty td := tempDir(t)