Skip to content
This repository was archived by the owner on Sep 29, 2018. It is now read-only.

Commit 38e2c40

Browse files
committed
Self update
1 parent d6df07f commit 38e2c40

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,48 @@ func (c *runCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (
124124
return subcommands.ExitSuccess
125125
}
126126

127+
type updateCmd struct {
128+
path string
129+
}
130+
131+
func (*updateCmd) Name() string {
132+
return "update"
133+
}
134+
135+
func (*updateCmd) Synopsis() string {
136+
return "Update Self"
137+
}
138+
139+
func (*updateCmd) Usage() string {
140+
return "update [-target]\n"
141+
}
142+
143+
func (c *updateCmd) SetFlags(f *flag.FlagSet) {
144+
f.StringVar(&c.path, "path", "./mcpeserver", "Download target")
145+
}
146+
147+
func (c *updateCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (ret subcommands.ExitStatus) {
148+
defer func() {
149+
if r := recover(); r != nil {
150+
fmt.Println("\033[5;91mError: \n", r)
151+
ret = subcommands.ExitFailure
152+
}
153+
}()
154+
printInfo("Get Latest Release...")
155+
url := getServerURL()
156+
printPair("URL", url)
157+
fetchBinary(url, c.path)
158+
return subcommands.ExitSuccess
159+
}
160+
127161
func main() {
128162
subcommands.Register(subcommands.HelpCommand(), "")
129163
subcommands.Register(subcommands.FlagsCommand(), "")
130164
subcommands.Register(subcommands.CommandsCommand(), "")
131165
subcommands.Register(&downloadCmd{}, "")
132166
subcommands.Register(&unpackCmd{}, "")
133167
subcommands.Register(&runCmd{}, "")
168+
subcommands.Register(&updateCmd{}, "")
134169

135170
flag.Parse()
136171
ctx := context.Background()

update.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"io/ioutil"
7+
"net/http"
8+
"os"
9+
"time"
10+
11+
"gopkg.in/cheggaaa/pb.v1"
12+
)
13+
14+
type releaseInfo struct {
15+
Assets []struct {
16+
URL string `json:"browser_download_url"`
17+
} `json:"assets"`
18+
}
19+
20+
func getServerURL() string {
21+
resp, err := http.Get("https://api.github.com/repos/codehz/mcpeserver/releases/latest")
22+
if err != nil {
23+
panic(err)
24+
}
25+
defer resp.Body.Close()
26+
contents, err := ioutil.ReadAll(resp.Body)
27+
if err != nil {
28+
panic(err)
29+
}
30+
info := releaseInfo{}
31+
if err = json.Unmarshal(contents, &info); err != nil {
32+
panic(err)
33+
}
34+
return info.Assets[0].URL
35+
}
36+
37+
func fetchBinary(url string, target string) {
38+
out, err := os.OpenFile(target+".tmp", os.O_CREATE|os.O_RDWR, 0755)
39+
if err != nil {
40+
panic(err)
41+
}
42+
defer out.Close()
43+
resp, err := http.Get(url)
44+
if err != nil {
45+
panic(err)
46+
}
47+
defer resp.Body.Close()
48+
49+
bar := pb.StartNew(int(resp.ContentLength))
50+
bar.SetUnits(pb.U_BYTES_DEC)
51+
bar.SetRefreshRate(time.Millisecond * 20)
52+
bar.Prefix("mcpeserver")
53+
bar.Start()
54+
_, err = io.Copy(out, bar.NewProxyReader(resp.Body))
55+
if err != nil {
56+
panic(err)
57+
}
58+
bar.FinishPrint("\033[0;34mUpdate Finished.\033[0m")
59+
os.Rename(target+".tmp", target)
60+
}

0 commit comments

Comments
 (0)