From 55bf19e548a56eb62dc21ace7dd89d7f65f4120e Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 14 Jun 2017 15:14:26 -0400 Subject: [PATCH 1/2] always write to dataDir in the current directory Now that init can take a directory for configuration, the old behavior of writing the .terraform data directory into the target path no longer makes sense. Don't change the dataDir field during init, and write to the default location. Clean up all references to Meta.dataDir, and only use the getter method in case we chose to dynamically override this at some point. --- command/init.go | 5 ----- command/meta.go | 20 ++++++-------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/command/init.go b/command/init.go index 4da1ea049..5d33063e9 100644 --- a/command/init.go +++ b/command/init.go @@ -4,7 +4,6 @@ import ( "fmt" "log" "os" - "path/filepath" "sort" "strings" @@ -81,10 +80,6 @@ func (c *InitCommand) Run(args []string) int { if len(args) == 1 { path = args[0] } - // Set the state out path to be the path requested for the module - // to be copied. This ensures any remote states gets setup in the - // proper directory. - c.Meta.dataDir = filepath.Join(path, DefaultDataDir) // This will track whether we outputted anything so that we know whether // to output a newline before the success message diff --git a/command/meta.go b/command/meta.go index 63b170451..ffd9ec62e 100644 --- a/command/meta.go +++ b/command/meta.go @@ -44,7 +44,8 @@ type Meta struct { // Protected: commands can set these //---------------------------------------------------------- - // Modify the data directory location. Defaults to DefaultDataDir + // Modify the data directory location. This should be accessed through the + // DataDir method. dataDir string // Override certain behavior for tests within this package @@ -159,6 +160,7 @@ func (m *Meta) Colorize() *colorstring.Colorize { } // DataDir returns the directory where local data will be stored. +// Defaults to DefaultsDataDir in the current working directory. func (m *Meta) DataDir() string { dataDir := DefaultDataDir if m.dataDir != "" { @@ -498,12 +500,7 @@ func (m *Meta) WorkspaceOverridden() (string, bool) { return envVar, true } - dataDir := m.dataDir - if m.dataDir == "" { - dataDir = DefaultDataDir - } - - envData, err := ioutil.ReadFile(filepath.Join(dataDir, local.DefaultWorkspaceFile)) + envData, err := ioutil.ReadFile(filepath.Join(m.DataDir(), local.DefaultWorkspaceFile)) current := string(bytes.TrimSpace(envData)) if current == "" { current = backend.DefaultStateName @@ -520,17 +517,12 @@ func (m *Meta) WorkspaceOverridden() (string, bool) { // SetWorkspace saves the given name as the current workspace in the local // filesystem. func (m *Meta) SetWorkspace(name string) error { - dataDir := m.dataDir - if m.dataDir == "" { - dataDir = DefaultDataDir - } - - err := os.MkdirAll(dataDir, 0755) + err := os.MkdirAll(m.DataDir(), 0755) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(dataDir, local.DefaultWorkspaceFile), []byte(name), 0644) + err = ioutil.WriteFile(filepath.Join(m.DataDir(), local.DefaultWorkspaceFile), []byte(name), 0644) if err != nil { return err } From 4f5e92e4c0cd7c0f08e1b1a273756264408201a9 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 14 Jun 2017 15:22:30 -0400 Subject: [PATCH 2/2] reverse init test to check for dataDir in PWD init should always write intternal data to the current directory, even when a path is provided. The inherited behavior no longer applies to the new use of init. --- command/init_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/command/init_test.go b/command/init_test.go index c51e8a4a6..6ccdbadbc 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -305,12 +305,12 @@ func TestInit_targetSubdir(t *testing.T) { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } - if _, err := os.Stat(filepath.Join(td, "source", DefaultDataDir, DefaultStateFilename)); err != nil { + if _, err := os.Stat(filepath.Join(td, DefaultDataDir, DefaultStateFilename)); err != nil { t.Fatalf("err: %s", err) } // a data directory should not have been added to out working dir - if _, err := os.Stat(filepath.Join(td, DefaultDataDir)); !os.IsNotExist(err) { + if _, err := os.Stat(filepath.Join(td, "source", DefaultDataDir)); !os.IsNotExist(err) { t.Fatalf("err: %s", err) } }