-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.go
197 lines (160 loc) · 5.06 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
"github.com/spf13/cobra"
)
type Github struct {
Assets []Asset `json:"assets"`
}
type Asset struct {
URL string `json:"url"`
ID int64 `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Label interface{} `json:"label"`
Uploader Uploader `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int64 `json:"size"`
DownloadCount int64 `json:"download_count"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
BrowserDownloadURL string `json:"browser_download_url"`
}
type Uploader struct {
Login string `json:"login"`
ID int64 `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}
// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
path, _ := cmd.Flags().GetString("path")
downloadBinary(path)
},
}
func downloadBinary(path string) {
fmt.Println("Updating SDK...")
http.DefaultTransport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
var github Github
resp, err := http.Get("https://api.github.com/repos/booscaaa/cleanv/releases/latest")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&github); err != nil {
fmt.Println("There was a problem with the SDK update")
fmt.Println(err)
return
}
for _, asset := range github.Assets {
if runtime.GOOS == "windows" {
if asset.Name == "cleanv.exe" {
downloadToWindows(path, fmt.Sprint(asset.ID))
}
} else {
if asset.Name == "cleanv" {
downloadToLinux(path, fmt.Sprint(asset.ID))
}
}
}
fmt.Println("Cleanv command line successfully updated!")
// }
}
func downloadToLinux(path, asset string) {
client := http.Client{}
req, err := http.NewRequest("GET", "https://api.github.com/repos/booscaaa/cleanv/releases/assets/"+asset, nil)
if err != nil {
fmt.Println(err)
}
req.Header = http.Header{
"Accept": []string{"application/octet-stream"},
}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
out, _ := os.Create(path + "/clenvnew")
defer out.Close()
io.Copy(out, resp.Body)
os.Rename(path+"/clenvnew", path+"/cleanv")
os.Chmod(path+"/cleanv", 0777)
exec.Command("cleanv", "completion", "bash", ">", "/tmp/completion").Run()
}
func downloadToWindows(path, asset string) {
client := http.Client{}
req, err := http.NewRequest("GET", "https://api.github.com/repos/booscaaa/cleanv/releases/assets/"+asset, nil)
if err != nil {
fmt.Println(err)
}
req.Header = http.Header{
"Accept": []string{"application/octet-stream"},
}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
out, _ := os.Create(path + "/clenvnew.exe")
defer out.Close()
io.Copy(out, resp.Body)
fmt.Println()
fmt.Println("Run the command: rename " + path + "/clenvnew.exe " + path + "/cleanv.exe")
}
func init() {
rootCmd.AddCommand(updateCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// updateCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
var path string
if runtime.GOOS == "windows" {
path = "C:/cleanv"
} else {
path = "/usr/local/cleanv"
}
updateCmd.Flags().StringP("path", "p", path, "Cleanv sdk path")
}