-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
44 lines (36 loc) · 917 Bytes
/
cache.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/j-keck/zfs-snap-diff/pkg/fs"
"github.com/j-keck/zfs-snap-diff/pkg/scanner"
"os"
)
func cacheFileVersions(versions []scanner.FileVersion) error {
j, err := json.Marshal(versions)
if err != nil {
return err
}
cacheDir, err := fs.CacheDir()
if err != nil {
return err
}
_, err = cacheDir.WriteFile("zsd.cache", j, 0644)
return err
}
func loadCachedFileVersions() ([]scanner.FileVersion, error) {
cacheDir, err := fs.CacheDir()
if err != nil {
return nil, err
}
b, err := cacheDir.ReadFile("zsd.cache")
if os.IsNotExist(err) {
return nil, errors.New("cached file-versions not found - try the 'list' action at first")
} else if err != nil {
return nil, fmt.Errorf("unable to load cached file-version - %v", err)
}
versions := make([]scanner.FileVersion, 0)
err = json.Unmarshal(b, &versions)
return versions, err
}