-
Notifications
You must be signed in to change notification settings - Fork 5
/
pairing_store.go
69 lines (53 loc) · 1.11 KB
/
pairing_store.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
package statuskeycardgo
import (
"encoding/json"
"os"
"path/filepath"
)
type pairingStore struct {
path string
values map[string]*PairingInfo
}
func newPairingStore(storage string) (*pairingStore, error) {
p := &pairingStore{path: storage}
b, err := os.ReadFile(p.path)
if err != nil {
if os.IsNotExist(err) {
parent := filepath.Dir(p.path)
err = os.MkdirAll(parent, 0750)
if err != nil {
return nil, err
}
p.values = map[string]*PairingInfo{}
} else {
return nil, err
}
} else {
err = json.Unmarshal(b, &p.values)
if err != nil {
return nil, err
}
}
return p, nil
}
func (p *pairingStore) save() error {
b, err := json.Marshal(p.values)
if err != nil {
return err
}
err = os.WriteFile(p.path, b, 0640)
if err != nil {
return err
}
return nil
}
func (p *pairingStore) store(instanceUID string, pairing *PairingInfo) error {
p.values[instanceUID] = pairing
return p.save()
}
func (p *pairingStore) get(instanceUID string) *PairingInfo {
return p.values[instanceUID]
}
func (p *pairingStore) delete(instanceUID string) {
delete(p.values, instanceUID)
}