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
/
factorio.go
73 lines (56 loc) · 1.45 KB
/
factorio.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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io"
"net/http"
"time"
"github.com/sirupsen/logrus"
)
func factorioGetChecksum(url string) (string, error) {
checksum := ""
logrus.Debugln("downloading", url, "for checksum check")
//Open a new SHA256 hash interface to write to
hash := sha256.New()
resp, err := http.Get(url)
if err != nil {
return checksum, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return "", nil
} else if resp.StatusCode != http.StatusOK {
return "", errors.New(resp.Status)
}
_, err = io.Copy(hash, resp.Body)
if err != nil {
return checksum, err
}
//Get the 32 bytes hash
hashInBytes := hash.Sum(nil)
//Convert the bytes to a string
checksum = hex.EncodeToString(hashInBytes)
logrus.Debugln("got checksum", checksum)
return checksum, nil
}
var myClient = &http.Client{Timeout: 10 * time.Second}
func GetAvailableVersions() (AvailableVersions, error) {
var availableVersions AvailableVersions
r, err := myClient.Get("https://www.factorio.com/updater/get-available-versions?apiVersion=2")
if err != nil {
return availableVersions, err
}
defer r.Body.Close()
err = json.NewDecoder(r.Body).Decode(&availableVersions)
return availableVersions, err
}
type AvailableVersions struct {
CoreLinuxHeadless64 []AvailableVersionsStep `json:"core-linux_headless64"`
}
type AvailableVersionsStep struct {
From string
To string
Stable string
}