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.
This commit is contained in:
parent
fa2fd0bf01
commit
716132431a
|
@ -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
|
||||
|
||||
// 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,
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue