Merge pull request #15289 from hashicorp/jbardin/init-pwd

init with target dir should still write .terraform in the current directory
This commit is contained in:
James Bardin 2017-06-14 16:59:35 -04:00 committed by GitHub
commit 06d4247a75
3 changed files with 8 additions and 21 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

@ -305,12 +305,12 @@ func TestInit_targetSubdir(t *testing.T) {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 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) t.Fatalf("err: %s", err)
} }
// a data directory should not have been added to out working dir // 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) t.Fatalf("err: %s", err)
} }
} }

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
} }