-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
132 lines (113 loc) · 2.62 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package update
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"github.com/jobtalk/thor/vars"
)
const (
GITHUB_API = "https://api.github.com/repos/jobtalk/thor/tags"
)
var client = &http.Client{}
type commit struct {
Sha string `json:"sha"`
URL string `json:"url"`
}
type tag struct {
Name string `json:"name"`
ZipballURL string `json:"zipball_url"`
TarballURL string `json:"tarball_url"`
Commit commit `json:"commit"`
}
func (t tag) String() string {
bin, err := json.MarshalIndent(t, "", " ")
if err != nil {
return ""
}
return string(bin)
}
func checkENV() bool {
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
return false
}
if runtime.GOARCH != "amd64" {
return false
}
return true
}
type Update struct{}
func (c *Update) Run(args []string) int {
var platform string
tags := []tag{}
resp, err := client.Get(GITHUB_API)
if err != nil {
log.Println(err)
return 255
}
bin, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return 255
}
defer resp.Body.Close()
if err := json.Unmarshal(bin, &tags); err != nil {
log.Println(err)
return 255
}
if !checkENV() {
fmt.Printf("Sorry, this architecture not supported (%s %s).\n", runtime.GOOS, runtime.GOARCH)
fmt.Println("Please try manual update.")
return 255
}
if runtime.GOOS == "darwin" {
platform = "darwin-amd64"
} else if runtime.GOOS == "linux" {
platform = "linux-amd64"
}
latest := tags[0].Name
if *vars.VERSION == latest {
fmt.Println("this version is latest")
return 0
}
if latest == "" {
fmt.Println("can not get latest version")
return 255
}
binaryURL := fmt.Sprintf("https://github.com/jobtalk/thor/releases/download/%s/thor-%s", latest, platform)
dir, err := filepath.Abs(os.Args[0]) // Get the absolute path at Executing file. Reference:http://stackoverflow.com/questions/18537257/golang-how-to-get-the-directory-of-the-currently-running-file
if err != nil {
log.Println(err)
return 255
}
latestResp, err := client.Get(binaryURL)
if err != nil {
log.Println(err)
return 255
}
latestBin, err := ioutil.ReadAll(latestResp.Body)
if err != nil {
log.Println(err)
return 255
}
if err := ioutil.WriteFile(dir, latestBin, 0755); err != nil {
fmt.Printf("Check parmission: %s\n", dir)
return 255
}
fmt.Println("update successed")
return 0
}
func (c *Update) Synopsis() string {
msg := "Upgrade apex to the latest stable release\n"
msg += "options:\n"
msg += " <node>\n"
msg += "===================================================\n"
return msg
}
func (c *Update) Help() string {
return c.Synopsis()
}