From 1646310e68e303714c455df285d739e6070e313d Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 30 Jan 2017 15:42:35 -0500 Subject: [PATCH] Allow a non-existent state file A missing state file was allowed, and treated as an empty state. --- state/local.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/state/local.go b/state/local.go index f143609ec..01d696442 100644 --- a/state/local.go +++ b/state/local.go @@ -1,6 +1,7 @@ package state import ( + "bytes" "encoding/json" "fmt" "io" @@ -161,10 +162,19 @@ func (s *LocalState) RefreshState() error { // we haven't written a state file yet, so load from Path f, err := os.Open(s.Path) if err != nil { - return err + // It is okay if the file doesn't exist, we treat that as a nil state + if !os.IsNotExist(err) { + return err + } + + // we need a non-nil reader for ReadState and an empty buffer works + // to return EOF immediately + reader = bytes.NewBuffer(nil) + + } else { + defer f.Close() + reader = f } - defer f.Close() - reader = f } else { // we have a state file, make sure we're at the start s.stateFileOut.Seek(0, os.SEEK_SET)