-
Notifications
You must be signed in to change notification settings - Fork 2
/
persistence.go
96 lines (82 loc) · 2.14 KB
/
persistence.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net"
"os"
"time"
)
type Persistence interface {
LoadLeases() (map[FixedV4]*Lease, error)
PersistLeases(map[FixedV4]*Lease) error
}
type FilePersistenceLease struct {
Hostname string
IP string
Mac string
Expiration time.Time
}
type FilePersistence struct {
path string
}
func NewFilePersistence(path string) *FilePersistence {
return &FilePersistence{path}
}
// Load on-disk json leases into our in-memory format
func (p *FilePersistence) decode(orig map[string]*FilePersistenceLease) map[FixedV4]*Lease {
result := map[FixedV4]*Lease{}
for _, lease := range orig {
result[IpToFixedV4(net.ParseIP(lease.IP))] = &Lease{
Mac: StrToMac(lease.Mac),
Hostname: lease.Hostname,
IP: IpToFixedV4(net.ParseIP(lease.IP)),
Expiration: lease.Expiration,
}
}
return result
}
// Encoded in-memory leases into our on-disk json format
func (p *FilePersistence) encode(leases map[FixedV4]*Lease) map[string]*FilePersistenceLease {
result := map[string]*FilePersistenceLease{}
for _, lease := range leases {
result[lease.IP.String()] = &FilePersistenceLease{
Mac: lease.Mac.String(),
Hostname: lease.Hostname,
IP: lease.IP.String(),
Expiration: lease.Expiration,
}
}
return result
}
func (p *FilePersistence) LoadLeases() (map[FixedV4]*Lease, error) {
leases := map[FixedV4]*Lease{}
contents, err := ioutil.ReadFile(p.path)
if err != nil {
// If file doesn't exist, don't surface it as an error,
// and instead just return an empty list
if errors.Is(err, os.ErrNotExist) {
return leases, nil
}
return nil, err
}
fromFile := map[string]*FilePersistenceLease{}
err = json.Unmarshal(contents, &fromFile)
if err != nil {
return nil, err
}
return p.decode(fromFile), nil
}
func (p *FilePersistence) PersistLeases(leases map[FixedV4]*Lease) error {
encoded := p.encode(leases)
payload, err := json.MarshalIndent(encoded, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(p.path, payload, 0644)
if err != nil {
log.Printf("Failed persisting leases to %v: %v", p.path, err)
}
return err
}