fix incorrect current state in local backend

Forgot to remove the currentState field, which was not always set. The
current state should always just be read from the environment file.

Always return the default state name when we can't determine the state.
This commit is contained in:
James Bardin 2017-02-24 08:53:37 -05:00
parent 06663991d1
commit fbc11c7961
1 changed files with 16 additions and 16 deletions

View File

@ -55,10 +55,6 @@ type Local struct {
// we only want to create a single instance of the local state // we only want to create a single instance of the local state
state state.State state state.State
// the name of the current state
currentState string
// ContextOpts are the base context options to set when initializing a
// Terraform context. Many of these will be overridden or merged by // Terraform context. Many of these will be overridden or merged by
// Operation. See Operation for more details. // Operation. See Operation for more details.
ContextOpts *terraform.ContextOpts ContextOpts *terraform.ContextOpts
@ -135,31 +131,37 @@ func (b *Local) States() ([]string, string, error) {
// the listing always start with "default" // the listing always start with "default"
envs := []string{backend.DefaultStateName} envs := []string{backend.DefaultStateName}
current := b.currentState current, err := b.currentStateName()
if current == "" { if err != nil {
name, err := b.currentStateName() return nil, "", err
if err != nil {
return nil, "", err
}
current = name
} }
entries, err := ioutil.ReadDir(filepath.Join(b.workingDir, DefaultEnvDir)) entries, err := ioutil.ReadDir(filepath.Join(b.workingDir, DefaultEnvDir))
// no error if there's no envs configured // no error if there's no envs configured
if os.IsNotExist(err) { if os.IsNotExist(err) {
return envs, current, nil return envs, backend.DefaultStateName, nil
} }
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
currentExists := false
var listed []string var listed []string
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() { if entry.IsDir() {
listed = append(listed, filepath.Base(entry.Name())) name := filepath.Base(entry.Name())
if name == current {
currentExists = true
}
listed = append(listed, name)
} }
} }
// current was out of sync for some reason, so return defualt
if !currentExists {
current = backend.DefaultStateName
}
sort.Strings(listed) sort.Strings(listed)
envs = append(envs, listed...) envs = append(envs, listed...)
@ -254,8 +256,6 @@ func (b *Local) ChangeState(name string) error {
return err return err
} }
b.currentState = name
// remove the current state so it's reloaded on the next call to State // remove the current state so it's reloaded on the next call to State
b.state = nil b.state = nil
@ -421,7 +421,7 @@ func (b *Local) statePath() (string, error) {
path := DefaultStateFilename path := DefaultStateFilename
if current != backend.DefaultStateName && current != "" { if current != backend.DefaultStateName && current != "" {
path = filepath.Join(b.workingDir, DefaultEnvDir, b.currentState, DefaultStateFilename) path = filepath.Join(b.workingDir, DefaultEnvDir, current, DefaultStateFilename)
} }
return path, nil return path, nil
} }