This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
checksum.go
85 lines (67 loc) · 1.45 KB
/
checksum.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
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/blang/semver"
"github.com/sirupsen/logrus"
)
const checksumsFile = "/usr/watchdog/factorio-checksums.json"
type checksums struct {
sha256 map[string]string
loaded bool
}
func (c *checksums) loadChecksums() {
if c.loaded {
return
}
c.sha256 = map[string]string{}
f, err := os.Open(checksumsFile)
if err != nil && errors.Is(err, os.ErrNotExist) {
c.loaded = true
return
}
err = json.NewDecoder(f).Decode(&c.sha256)
if err != nil {
logrus.Errorln(err)
}
c.loaded = true
}
func (c *checksums) saveChecksums() error {
f, err := os.OpenFile(checksumsFile, os.O_WRONLY, os.ModeAppend)
if err != nil && errors.Is(err, os.ErrNotExist) {
f, err = os.Create(checksumsFile)
}
if err != nil {
return err
}
defer f.Close()
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
err = encoder.Encode(c.sha256)
if err != nil {
return err
}
return nil
}
func (c *checksums) getChecksum(version semver.Version) (string, error) {
c.loadChecksums()
checksum, ok := c.sha256[version.String()]
if ok && checksum != "" {
return checksum, nil
}
checksum, err := factorioGetChecksum(fmt.Sprintf("https://www.factorio.com/get-download/%s/headless/linux64", version))
if err != nil {
return "", err
}
if checksum == "" {
return "", nil
}
c.sha256[version.String()] = checksum
err = c.saveChecksums()
if err != nil {
return "", err
}
return checksum, nil
}