forked from codehz/mcpeserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
60 lines (54 loc) · 1.16 KB
/
update.go
File metadata and controls
60 lines (54 loc) · 1.16 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
58
59
60
package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"time"
"gopkg.in/cheggaaa/pb.v1"
)
type releaseInfo struct {
Assets []struct {
URL string `json:"browser_download_url"`
} `json:"assets"`
}
func getServerURL() string {
resp, err := http.Get("https://api.github.com/repos/codehz/mcpeserver/releases/latest")
if err != nil {
panic(err)
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
info := releaseInfo{}
if err = json.Unmarshal(contents, &info); err != nil {
panic(err)
}
return info.Assets[0].URL
}
func fetchBinary(url string, target string) {
out, err := os.OpenFile(target+".tmp", os.O_CREATE|os.O_RDWR, 0755)
if err != nil {
panic(err)
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
bar := pb.StartNew(int(resp.ContentLength))
bar.SetUnits(pb.U_BYTES_DEC)
bar.SetRefreshRate(time.Millisecond * 20)
bar.Prefix("mcpeserver")
bar.Start()
_, err = io.Copy(out, bar.NewProxyReader(resp.Body))
if err != nil {
panic(err)
}
bar.FinishPrint("\033[0;34mUpdate Finished.\033[0m")
os.Rename(target+".tmp", target)
}