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.
This commit is contained in:
James Bardin 2017-06-14 15:14:26 -04:00
parent 9706042ddd
commit 55bf19e548
2 changed files with 6 additions and 19 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"sort" "sort"
"strings" "strings"
@ -81,10 +80,6 @@ func (c *InitCommand) Run(args []string) int {
if len(args) == 1 { if len(args) == 1 {
path = args[0] 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 // This will track whether we outputted anything so that we know whether
// to output a newline before the success message // to output a newline before the success message

View File

@ -44,7 +44,8 @@ type Meta struct {
// Protected: commands can set these // 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 dataDir string
// Override certain behavior for tests within this package // 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. // DataDir returns the directory where local data will be stored.
// Defaults to DefaultsDataDir in the current working directory.
func (m *Meta) DataDir() string { func (m *Meta) DataDir() string {
dataDir := DefaultDataDir dataDir := DefaultDataDir
if m.dataDir != "" { if m.dataDir != "" {
@ -498,12 +500,7 @@ func (m *Meta) WorkspaceOverridden() (string, bool) {
return envVar, true return envVar, true
} }
dataDir := m.dataDir envData, err := ioutil.ReadFile(filepath.Join(m.DataDir(), local.DefaultWorkspaceFile))
if m.dataDir == "" {
dataDir = DefaultDataDir
}
envData, err := ioutil.ReadFile(filepath.Join(dataDir, local.DefaultWorkspaceFile))
current := string(bytes.TrimSpace(envData)) current := string(bytes.TrimSpace(envData))
if current == "" { if current == "" {
current = backend.DefaultStateName 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 // SetWorkspace saves the given name as the current workspace in the local
// filesystem. // filesystem.
func (m *Meta) SetWorkspace(name string) error { func (m *Meta) SetWorkspace(name string) error {
dataDir := m.dataDir err := os.MkdirAll(m.DataDir(), 0755)
if m.dataDir == "" {
dataDir = DefaultDataDir
}
err := os.MkdirAll(dataDir, 0755)
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }