state: LocalState supports alternate output path

This commit is contained in:
Mitchell Hashimoto 2015-02-21 16:11:47 -08:00
parent 34864a64a5
commit 5c356f35b9
2 changed files with 52 additions and 13 deletions

View File

@ -8,9 +8,14 @@ import (
// LocalState manages a state storage that is local to the filesystem. // LocalState manages a state storage that is local to the filesystem.
type LocalState struct { type LocalState struct {
// Path is the path to read the state from. PathOut is the path to
// write the state to. If PathOut is not specified, Path will be used.
// If PathOut already exists, it will be overwritten.
Path string Path string
PathOut string
state *terraform.State state *terraform.State
written bool
} }
// StateReader impl. // StateReader impl.
@ -24,13 +29,23 @@ func (s *LocalState) State() *terraform.State {
func (s *LocalState) WriteState(state *terraform.State) error { func (s *LocalState) WriteState(state *terraform.State) error {
s.state = state s.state = state
f, err := os.Create(s.Path) path := s.PathOut
if path == "" {
path = s.Path
}
f, err := os.Create(path)
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
return terraform.WriteState(s.state, f) if err := terraform.WriteState(s.state, f); err != nil {
return err
}
s.written = true
return nil
} }
// PersistState for LocalState is a no-op since WriteState always persists. // PersistState for LocalState is a no-op since WriteState always persists.
@ -42,22 +57,31 @@ func (s *LocalState) PersistState() error {
// StateRefresher impl. // StateRefresher impl.
func (s *LocalState) RefreshState() error { func (s *LocalState) RefreshState() error {
f, err := os.Open(s.Path) // If we've never loaded before, read from Path, otherwise we
// read from PathOut.
path := s.Path
if s.written && s.PathOut != "" {
path = s.PathOut
}
f, err := os.Open(path)
if err != nil { if err != nil {
// It is okay if the file doesn't exist, we treat that as a nil state // It is okay if the file doesn't exist, we treat that as a nil state
if os.IsNotExist(err) { if !os.IsNotExist(err) {
s.state = nil
return nil
}
return err return err
} }
defer f.Close()
state, err := terraform.ReadState(f) f = nil
}
var state *terraform.State
if f != nil {
defer f.Close()
state, err = terraform.ReadState(f)
if err != nil { if err != nil {
return err return err
} }
}
s.state = state s.state = state
return nil return nil

View File

@ -14,6 +14,21 @@ func TestLocalState(t *testing.T) {
TestState(t, ls) TestState(t, ls)
} }
func TestLocalState_pathOut(t *testing.T) {
f, err := ioutil.TempFile("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
f.Close()
defer os.Remove(f.Name())
ls := testLocalState(t)
ls.PathOut = f.Name()
defer os.Remove(ls.Path)
TestState(t, ls)
}
func TestLocalState_nonExist(t *testing.T) { func TestLocalState_nonExist(t *testing.T) {
ls := &LocalState{Path: "ishouldntexist"} ls := &LocalState{Path: "ishouldntexist"}
if err := ls.RefreshState(); err != nil { if err := ls.RefreshState(); err != nil {