/
get.go
338 lines (310 loc) · 9.37 KB
/
get.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package get
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/olekukonko/tablewriter"
Z "github.com/rwxrob/bonzai/z"
"github.com/rwxrob/help"
"log"
"net/http"
"os"
"runtime"
"sort"
"strings"
"text/template"
"time"
)
var (
httpTimeout = 30 * time.Second
)
func httpClient(timeout *time.Duration) http.Client {
cl := http.Client{}
cl.Timeout = *timeout
return cl
}
var Cmd = &Z.Cmd{
Name: `get`,
Summary: `install executables and applications on the host system [requires internet]`,
Description: `
The *get* command downloads a tools or applications from that providers releases or
downloads page. Typically, tools are downloaded as a binary for fast and efficient access
on the host platform.
`,
Other: []Z.Section{
{
Title: "Examples",
Body: `
ds get - list all available tools
ds get arkade - download the Arkade binary`,
},
},
Commands: []*Z.Cmd{
// imported commands
help.Cmd,
// local
},
Call: func(_ *Z.Cmd, args ...string) error {
tools := MakeTools()
arch, opSystem := GetClientArch()
sort.Sort(tools)
if len(args) == 0 {
ListToolsTable(tools)
return nil
}
tool := args[0]
log.Printf("Looking up version for %q\n", tool)
t, err := getTool(tool, tools)
if err != nil {
return err
}
version := t.Version
if version == "" {
version = "latest"
}
_, err = Download(&t, arch, opSystem, version)
if err != nil {
return err
}
err = PrintPostInstallMessage(t)
if err != nil {
return err
}
return nil
},
}
// FindGithubRelease retrieves a response from GitHub's API for any valid repository
// in JSON format.
func FindGithubRelease(owner, repo string) ([]*GithubAPIReleasesResponse, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", owner, repo)
cl := httpClient(&httpTimeout)
r, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
res, err := cl.Do(r)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
}
var release []*GithubAPIReleasesResponse
//var r2 any
err = json.NewDecoder(res.Body).Decode(&release)
if err != nil {
return nil, fmt.Errorf("failed to decode release with err: %s", err)
}
return release, nil
}
func PrintPostInstallMessage(t Tool) error {
lt := ToolLocal{
Name: t.Name,
Path: fmt.Sprintf("%s/%s/%s", os.Getenv("HOME"), toolFilePath, t.Name),
BinPath: fmt.Sprintf("%s", toolFilePath),
}
msg, err := PostInstallationMessage(lt)
if err != nil {
return err
}
fmt.Printf("%s\n", msg)
return nil
}
// ListToolsTable returns a list of all supported tools in tabular format.
func ListToolsTable(tools Tools) {
table := tablewriter.NewWriter(os.Stdout)
table.SetColWidth(60)
table.SetHeader([]string{"Tool", "Description"})
count := 0
for _, tool := range tools {
table.Append([]string{tool.Name, tool.Description})
count++
}
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgGreenColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.Normal},
)
table.SetColumnColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiGreenColor},
tablewriter.Colors{tablewriter.Normal, tablewriter.Normal},
)
table.SetRowLine(true)
table.SetCaption(true, fmt.Sprintf("%d tools are currently supported.\n", count))
table.Render()
}
// getTool retrieves tool information from the all the available Tool structs
// and if a valid entry is found returns it.
func getTool(tool string, tools Tools) (Tool, error) {
for _, t := range tools {
if tool == t.Name {
return Tool{
Name: t.Name,
Repo: t.Repo,
Owner: t.Owner,
Version: t.Version,
Description: t.Description,
NonBinary: t.NonBinary,
BinaryTemplate: t.BinaryTemplate,
}, nil
}
}
return Tool{}, fmt.Errorf("error: %q not found", tool)
}
// GetBinaryName returns the name of a binary for the given tool or an
// error if the tool's template cannot be parsed or executed.
func GetBinaryName(tool *Tool, os, arch, version string) (string, error) {
if len(tool.BinaryTemplate) > 0 {
var err error
t := template.New(tool.Name + "_binaryname")
t = t.Funcs(templateFuncs)
t, err = t.Parse(tool.BinaryTemplate)
if err != nil {
return "", err
}
var buf bytes.Buffer
ver := toolVersion(tool, version)
if err := t.Execute(&buf, map[string]string{
"OS": os,
"Arch": arch,
"Name": tool.Name,
"Version": ver,
"VersionNumber": strings.TrimPrefix(ver, "v"),
}); err != nil {
return "", err
}
res := strings.TrimSpace(buf.String())
fmt.Printf("[DEBUG] binaryName %q\n", res)
return res, nil
}
return "", errors.New("BinaryTemplate is not set")
}
func toolVersion(tool *Tool, version string) string {
ver := tool.Version
if len(version) > 0 {
ver = version
}
return ver
}
// GetClientArch retrieves the host systems architecture and operating system.
// It will change the naming to a consistent format for linux/amd64. Further
// transformations of the architecture and operating system are then done elsewhere
// from a common base.
func GetClientArch() (arch, os string) {
os = runtime.GOOS
if runtime.GOOS == "linux" {
os = "linux"
}
arch = runtime.GOARCH
if runtime.GOARCH == "amd64" {
arch = "x86_64"
}
return arch, os
}
// GetDownloadURL returns the downloadable assets from GitHub for use in other
// functions.
func GetDownloadURL(tool Tool, arch, opSystem, version string) (string, error) {
releases, err := FindGithubRelease(tool.Owner, tool.Repo)
if err != nil {
return "", err
}
if version == "latest" {
// get then latest tag which is always index 0
version = releases[0].TagName
}
binaryName, err := GetBinaryName(&tool, opSystem, arch, version)
if err != nil {
return "", err
}
defer func() {
log.Printf("Found version %q\n", version)
}()
for _, release := range releases {
if release.Name == version || release.TagName == version {
for _, asset := range release.Assets {
if asset.Name == binaryName {
return asset.BrowserDownloadUrl, nil
}
}
}
}
return "", fmt.Errorf("no download URL found for %s", tool.Name)
}
// GithubAPIReleasesResponse is taken from the GitHub Releases API
// ref: https://docs.github.com/en/rest/releases/releases#list-releases
type GithubAPIReleasesResponse struct {
Url string `json:"url"`
AssetsUrl string `json:"assets_url"`
UploadUrl string `json:"upload_url"`
HtmlUrl string `json:"html_url"`
Id int `json:"id"`
Author struct {
Login string `json:"login"`
Id int `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"`
} `json:"author"`
NodeId string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Assets []struct {
Url string `json:"url"`
Id int `json:"id"`
NodeId string `json:"node_id"`
Name string `json:"name"`
Label string `json:"label"`
Uploader struct {
Login string `json:"login"`
Id int `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"`
} `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int `json:"size"`
DownloadCount int `json:"download_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BrowserDownloadUrl string `json:"browser_download_url"`
} `json:"assets"`
TarballUrl string `json:"tarball_url"`
ZipballUrl string `json:"zipball_url"`
Body string `json:"body"`
MentionsCount int `json:"mentions_count,omitempty"`
}