-
Notifications
You must be signed in to change notification settings - Fork 261
/
state.go
91 lines (75 loc) · 2.24 KB
/
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
package statefile
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/google/uuid"
"github.com/turbot/go-kit/files"
"github.com/turbot/steampipe/pkg/filepaths"
"github.com/turbot/steampipe/pkg/migrate"
)
const StateStructVersion = 20220411
// State is a struct containing installation state
type State struct {
LastCheck string `json:"last_checked"` // an RFC3339 encoded time stamp
InstallationID string `json:"installation_id"` // a UUIDv4 string
StructVersion int64 `json:"struct_version"`
}
func newState() State {
return State{
InstallationID: newInstallationID(),
StructVersion: StateStructVersion,
}
}
func LoadState() (State, error) {
currentState := newState()
if !files.FileExists(filepaths.StateFilePath()) {
return currentState, nil
}
stateFileContent, err := os.ReadFile(filepaths.StateFilePath())
if err != nil {
fmt.Println("Could not read update state file")
return currentState, err
}
err = json.Unmarshal(stateFileContent, ¤tState)
if err != nil {
fmt.Println("Could not parse update state file")
return currentState, err
}
return currentState, nil
}
// Save the state
// NOTE: this updates the last checked time
func (s *State) Save() error {
// set the struct version
s.StructVersion = StateStructVersion
s.LastCheck = nowTimeString()
// ensure internal dirs exists
_ = os.MkdirAll(filepaths.EnsureInternalDir(), os.ModePerm)
stateFilePath := filepath.Join(filepaths.EnsureInternalDir(), filepaths.StateFileName())
// if there is an existing file it must be bad/corrupt, so delete it
_ = os.Remove(stateFilePath)
// save state file
file, _ := json.MarshalIndent(s, "", " ")
return os.WriteFile(stateFilePath, file, 0644)
}
// IsValid checks whether the struct was correctly deserialized,
// by checking if the StructVersion is populated
func (s *State) IsValid() bool {
return s.StructVersion > 0
}
func (s *State) MigrateFrom() migrate.Migrateable {
// save the existing property values to the new legacy properties
s.StructVersion = StateStructVersion
s.LastCheck = nowTimeString()
s.InstallationID = newInstallationID()
return s
}
func newInstallationID() string {
return uuid.New().String()
}
func nowTimeString() string {
return time.Now().Format(time.RFC3339)
}