-
Notifications
You must be signed in to change notification settings - Fork 47
/
localstate.go
107 lines (84 loc) · 2.64 KB
/
localstate.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
package dstask
// this file represents the interface to the state specific to the PC dstask is
// on is stored. This is very minimal at the moment -- just the current
// context. It will probably remain that way.
import (
"encoding/gob"
"errors"
"os"
"path/filepath"
)
// State models our local context for serialisation and deserialisation from
// our state file.
type State struct {
// Context is an implicit command line that changes the behavior or display
// of some commands.
Context Query
}
// Persistent DB of UUID -> ID to ensure that tasks have a persistent ID
// local to this machine for their lifetime. This is important to ensure
// the correct task is targeted between operations. Historically, each task
// stored its preferred ID but this resulted in merge conflicts when 2
// machines were using dstask concurrently on the same repository.
type IdsMap map[string]int
// Save serialises State to disk as gob binary data.
func (state State) Save(stateFilePath string) {
os.MkdirAll(filepath.Dir(stateFilePath), os.ModePerm)
mustWriteGob(stateFilePath, &state)
}
// LoadState reads the state file, if it exists. Otherwise a default State is returned.
func LoadState(stateFilePath string) State {
if _, err := os.Stat(stateFilePath); os.IsNotExist(err) {
return State{}
}
state := State{}
mustReadGob(stateFilePath, &state)
return state
}
// SetContext sets a context on State, with some validation.
func (state *State) SetContext(context Query) error {
if len(context.IDs) != 0 {
return errors.New("context cannot contain IDs")
}
if context.Text != "" {
return errors.New("context cannot contain text")
}
state.Context = context
return nil
}
func mustWriteGob(filePath string, object interface{}) {
file, err := os.Create(filePath)
if err != nil {
ExitFail("Failed to open %s for writing: ", filePath)
}
defer file.Close()
encoder := gob.NewEncoder(file)
err = encoder.Encode(object)
if err != nil {
ExitFail("Failed to encode state gob: %s, %s", filePath, err)
}
}
func mustReadGob(filePath string, object interface{}) {
file, err := os.Open(filePath)
if err != nil {
ExitFail("Failed to open %s for reading: ", filePath)
}
defer file.Close()
decoder := gob.NewDecoder(file)
err = decoder.Decode(object)
if err != nil {
ExitFail("Failed to parse state gob: %s, %s", filePath, err)
}
}
func (ids *IdsMap) Save(idsFilePath string) {
os.MkdirAll(filepath.Dir(idsFilePath), os.ModePerm)
mustWriteGob(idsFilePath, &ids)
}
func LoadIds(idsFilePath string) IdsMap {
if _, err := os.Stat(idsFilePath); os.IsNotExist(err) {
return make(IdsMap)
}
ids := make(IdsMap, 1000)
mustReadGob(idsFilePath, &ids)
return ids
}