-
Notifications
You must be signed in to change notification settings - Fork 78
/
filesystem_state.go
112 lines (94 loc) · 2.43 KB
/
filesystem_state.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package local
import (
"encoding/json"
"io/fs"
"io/ioutil"
"os"
"time"
"github.com/zclconf/go-cty/cty"
"gopkg.in/yaml.v3"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/log"
)
func init() {
states.AddToBackends("local", NewFileSystemState)
}
var _ states.StateStorage = &FileSystemState{}
type FileSystemState struct {
// state Path is in the same dir where command line is invoked
Path string
}
func NewFileSystemState() states.StateStorage {
return &FileSystemState{}
}
const KusionState = "kusion_state.json"
func (f *FileSystemState) ConfigSchema() cty.Type {
config := map[string]cty.Type{
"path": cty.String,
}
return cty.Object(config)
}
func (f *FileSystemState) Configure(obj cty.Value) error {
var path cty.Value
if path = obj.GetAttr("path"); !path.IsNull() && path.AsString() != "" {
f.Path = path.AsString()
} else {
f.Path = KusionState
}
return nil
}
func (f *FileSystemState) GetLatestState(query *states.StateQuery) (*states.State, error) {
// create a new state file if no file exists
file, err := os.OpenFile(f.Path, os.O_RDWR|os.O_CREATE, fs.ModePerm)
if err != nil {
return nil, err
}
defer file.Close()
jsonFile, err := ioutil.ReadFile(f.Path)
if err != nil {
return nil, err
}
if len(jsonFile) != 0 {
state := &states.State{}
// JSON is a subset of YAML.
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64 universally.
// go-yaml does the right thing.
err = yaml.Unmarshal(jsonFile, state)
if err != nil {
return nil, err
}
return state, nil
} else {
log.Infof("file %s is empty. Skip unmarshal json", f.Path)
return nil, nil
}
}
func (f *FileSystemState) Apply(state *states.State) error {
now := time.Now()
// don't change createTime in the state
oldState, err := f.GetLatestState(nil)
if err != nil {
return err
}
if oldState == nil || oldState.CreateTime.IsZero() {
state.CreateTime = now
} else {
state.CreateTime = oldState.CreateTime
}
state.ModifiedTime = now
jsonByte, err := json.MarshalIndent(state, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(f.Path, jsonByte, fs.ModePerm)
}
func (f *FileSystemState) Delete(id string) error {
log.Infof("Delete state file:%s", f.Path)
err := os.Remove(f.Path)
if err != nil {
return err
}
return nil
}