-
Notifications
You must be signed in to change notification settings - Fork 7
/
local.go
51 lines (44 loc) · 1.09 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 store
import (
"encoding/json"
"os"
)
// LocalCommandStore implements CommandStore for a
// local file as storage device.
type LocalCommandStore struct {
loc string
}
var _ CommandStore = (*LocalCommandStore)(nil)
// NewLocalCommandStore creates a new instance of
// LocalCommandStore with the passed file location
// as stoage destination.
func NewLocalCommandStore(loc string) *LocalCommandStore {
return &LocalCommandStore{loc}
}
// NewDefault returns a new LocalCommandStore with
// default file location (".commandCache.json").
func NewDefault() *LocalCommandStore {
return NewLocalCommandStore(".commandCache.json")
}
func (lcs *LocalCommandStore) Store(cmds map[string]string) (err error) {
f, err := os.Create(lcs.loc)
if err != nil {
return
}
defer f.Close()
err = json.NewEncoder(f).Encode(cmds)
return
}
func (lcs *LocalCommandStore) Load() (cmds map[string]string, err error) {
cmds = map[string]string{}
f, err := os.Open(lcs.loc)
if err != nil {
if os.IsNotExist(err) {
err = nil
}
return
}
defer f.Close()
err = json.NewDecoder(f).Decode(&cmds)
return
}