forked from webp-sh/webp_server_go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
57 lines (51 loc) · 1.29 KB
/
Copy pathupdate.go
File metadata and controls
57 lines (51 loc) · 1.29 KB
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
package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/staktrace/go-update"
"io/ioutil"
"net/http"
"runtime"
)
func autoUpdate() {
defer func() {
if err := recover(); err != nil {
log.Errorf("Download error: %s", err)
}
}()
var api = "https://api.github.com/repos/webp-sh/webp_server_go/releases/latest"
type Result struct {
TagName string `json:"tag_name"`
}
var res Result
log.Debugf("Requesting to %s", api)
resp1, _ := http.Get(api)
data1, _ := ioutil.ReadAll(resp1.Body)
_ = json.Unmarshal(data1, &res)
var gitVersion = res.TagName
if gitVersion > version {
log.Infof("Time to update! New version %s found", gitVersion)
} else {
log.Debug("No new version found.")
return
}
var filename = fmt.Sprintf("webp-server-%s-%s", runtime.GOOS, runtime.GOARCH)
if runtime.GOOS == "windows" {
filename += ".exe"
}
log.Info("Downloading binary to update...")
resp, _ := http.Get(releaseUrl + filename)
if resp.StatusCode != 200 {
log.Debugf("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
return
}
err := update.Apply(resp.Body, update.Options{})
if err != nil {
// error handling
log.Errorf("Update error. %v", err)
} else {
log.Info("Update complete. Please restart to apply changes.")
}
_ = resp.Body.Close()
}