mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Allow a non-existent state file
A missing state file was allowed, and treated as an empty state.
This commit is contained in:
parent
3fdcbda3aa
commit
1646310e68
@ -1,6 +1,7 @@
|
|||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -161,10 +162,19 @@ func (s *LocalState) RefreshState() error {
|
|||||||
// we haven't written a state file yet, so load from Path
|
// we haven't written a state file yet, so load from Path
|
||||||
f, err := os.Open(s.Path)
|
f, err := os.Open(s.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// It is okay if the file doesn't exist, we treat that as a nil state
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
return 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()
|
defer f.Close()
|
||||||
reader = f
|
reader = f
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// we have a state file, make sure we're at the start
|
// we have a state file, make sure we're at the start
|
||||||
s.stateFileOut.Seek(0, os.SEEK_SET)
|
s.stateFileOut.Seek(0, os.SEEK_SET)
|
||||||
|
Loading…
Reference in New Issue
Block a user