-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
64 lines (55 loc) · 1.46 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
const CACHEFILE = "cache.txt"
// Reads the cached addons and returns a map of addonName: addonID
func getCache() (map[string]int, error) {
cacheMap := make(map[string]int)
file, err := os.Open(CACHEFILE)
if err != nil {
os.Create(CACHEFILE)
return cacheMap, fmt.Errorf("%v not found. Wow! Created it instead", CACHEFILE)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, " ")
if len(parts) == 2 {
releaseID, err := strconv.Atoi(parts[1])
if err != nil {
log.Println("Warning: Skipping line due to error converting releaseID to integer:", err)
continue
}
cacheMap[parts[0]] = releaseID
} else {
log.Println("Warning: Skipping line with invalid format:", line)
}
}
if err := scanner.Err(); err != nil {
return cacheMap, fmt.Errorf("error scanning cache file %v", err)
}
return cacheMap, nil
}
// Dumps the cache to the cache.txt file
func dumpCache(cacheMap map[string]int) error {
file, err := os.Create(CACHEFILE)
if err != nil {
return fmt.Errorf("failed to create cache file %s: %v", CACHEFILE, err)
}
defer file.Close()
for addonName, releaseID := range cacheMap {
line := fmt.Sprintf("%s %d\n", addonName, releaseID)
_, err := file.WriteString(line)
if err != nil {
return fmt.Errorf("failed to write to cache file %s: %v", CACHEFILE, err)
}
}
return nil
}