-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresume.go
65 lines (55 loc) · 1.46 KB
/
resume.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
package main
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"github.com/alphadose/haxmap"
)
var mep = haxmap.New[string, int]()
type CachedLogProgress struct {
LogURL string `json:"log_url"`
Index int `json:"index"`
}
type MapCache struct {
Entries []CachedLogProgress `json:"entries"`
}
func SaveResume() {
serializedMap := MapCache{}
mep.ForEach(func(key string, value int) bool {
entry := CachedLogProgress{LogURL: key, Index: value}
serializedMap.Entries = append(serializedMap.Entries, entry)
return true // return `true` to continue iteration and `false` to break iteration
})
file, err := os.OpenFile(flags.resumeFilename, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
Abort(err.Error())
}
defer file.Close()
serialized, err := json.Marshal(serializedMap)
if err != nil {
Abort(err.Error())
}
file.Write(serialized)
slog.Info("Saved cache", "filename", flags.resumeFilename)
}
func LoadResume() {
file, err := os.OpenFile(flags.resumeFilename, os.O_RDONLY, 0644)
if err != nil {
return
}
defer file.Close()
serializedMap := MapCache{}
decoder := json.NewDecoder(file)
err = decoder.Decode(&serializedMap)
if err != nil {
Abort(err.Error())
}
for _, entry := range serializedMap.Entries {
mep.Set(entry.LogURL, entry.Index)
}
mep.ForEach(func(key string, value int) bool {
slog.Info(fmt.Sprintf("Loaded %s at position %d", key, value))
return true // return `true` to continue iteration and `false` to break iteration
})
}