/
local.go
51 lines (46 loc) · 1.19 KB
/
local.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package state
import (
"encoding/json"
"io/ioutil"
"os"
log "github.com/sirupsen/logrus"
)
// LocalBackend is an implementation of State that performs all operations
// locally. This is the "default" state type
type LocalBackend struct {
state *State
Path string
}
// GetState returns state from local source
func (s LocalBackend) GetState() *State {
if s.state.Empty() {
if _, err := os.Stat(s.Path); os.IsNotExist(err) {
log.Debugf("State file not found, using empty state")
return s.state
}
stateFile, err := os.Open(s.Path)
if err != nil {
log.Fatalf("Could not open state file %s", s.Path)
}
defer stateFile.Close()
byteValue, err := ioutil.ReadAll(stateFile)
if err != nil {
log.Fatalf("Failed to read data from state file %s", s.Path)
}
json.Unmarshal(byteValue, &s.state)
}
return s.state
}
// WriteState writes state to local path
func (s LocalBackend) WriteState(st *State) error {
log.Debugf("Writing state to %s\n", s.Path)
data, err := json.MarshalIndent(st, "", " ")
if err != nil {
log.Fatal("Failed to Load State for Writing")
}
err = ioutil.WriteFile(s.Path, data, 0644)
if err != nil {
log.Fatal("Failed to write state to file")
}
return nil
}