/
update.go
76 lines (62 loc) · 1.71 KB
/
update.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
package main
import (
"errors"
"fmt"
"log"
"net/http"
"runtime"
"gitlab.com/piLights/dioder-rpc/configuration"
"gitlab.com/piLights/dioder-rpc/logging"
"github.com/inconshreveable/go-update"
)
//UPDATEURL is the URL from which the updates for the OS and architecture are fetched from
const UPDATEURL = "https://pilights.gitlab.io/dioder-rpc/dioderAPI_" + runtime.GOOS + "_" + runtime.GOARCH
var errFileNoFound = errors.New("File not found")
//startUpdate starts the updateProcess
func startUpdate() {
fmt.Println("Starting update...")
if configuration.DioderConfiguration.Debug {
logging.LogChan <- fmt.Sprintf("Downloading from %s\n", configuration.DioderConfiguration.UpdateURL)
}
error := updateBinary(configuration.DioderConfiguration.UpdateURL)
if error != nil {
fmt.Println("Updating failed!")
log.Fatal(error)
}
fmt.Println("Updated successfully")
}
//updateBinary updates the executable binary
func updateBinary(url string) error {
// Fetch the Hash-Sum
/*checksumResponse, error := http.Get(url + ".sha256")
if error != nil {
return error
}
defer checksumResponse.Body.Close()
if checksumResponse.StatusCode != 200 {
return errFileNoFound
}
checksum, error := ioutil.ReadAll(checksumResponse.Body)
if error != nil {
return error
}*/
// request the new file
response, error := http.Get(url)
if error != nil {
return error
}
defer response.Body.Close()
if response.StatusCode != 200 {
return errFileNoFound
}
error = update.Apply(response.Body, update.Options{
//Checksum: checksum,
})
if error != nil {
rollbackError := update.RollbackError(error)
if rollbackError != nil {
fmt.Printf("Failed to rollback from bad update: %v\n", rollbackError)
}
}
return error
}