/
get.go
211 lines (172 loc) · 5.63 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
// Copyright (c) arkade author(s) 2022. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package cmd
import (
"errors"
"fmt"
"os"
"os/signal"
"sort"
"strconv"
"strings"
"syscall"
units "github.com/docker/go-units"
"github.com/morikuni/aec"
"github.com/spf13/cobra"
"github.com/alexellis/arkade/pkg"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
)
// MakeGet creates the Get command to download software
func MakeGet() *cobra.Command {
tools := get.MakeTools()
sort.Sort(tools)
var validToolOptions []string = make([]string, len(tools))
for _, t := range tools {
validToolOptions = append(validToolOptions, t.Name)
}
var command = &cobra.Command{
Use: "get",
Short: `The get command downloads a tool`,
Long: `The get command downloads a CLI or application from the specific tool's
releases or downloads page. The tool is usually downloaded in binary format
and provides a fast and easy alternative to a package manager.`,
Example: ` arkade get helm
# Options for the download
arkade get linkerd2 --stash=false
arkade get kubectl --progress=false
# Override the version
arkade get kubectl@v1.19.3
arkade get terraform --version=1.7.4
# Override the OS
arkade get helm --os darwin --arch aarch64
arkade get helm --os linux --arch armv7l
# Get a complete list of CLIs to download:
arkade get`,
SilenceUsage: true,
Aliases: []string{"g", "d", "download"},
ValidArgs: validToolOptions,
}
clientArch, clientOS := env.GetClientArch()
command.Flags().Bool("progress", true, "Display a progress bar")
command.Flags().StringP("format", "o", "", "Format format of the list of tools (table/markdown/list)")
command.Flags().String("path", "", "Leave empty to store in HOME/.arkade/bin/, otherwise give a path for the resulting binaries")
command.Flags().StringP("version", "v", "", "Download a specific version")
command.Flags().String("arch", clientArch, "CPU architecture for the tool")
command.Flags().String("os", clientOS, "Operating system for the tool")
command.Flags().Bool("quiet", false, "Suppress most additional format")
command.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
format, _ := command.Flags().GetString("format")
if len(format) > 0 {
if get.TableFormat(format) == get.MarkdownStyle {
get.CreateToolsTable(tools, get.MarkdownStyle)
} else if get.TableFormat(format) == get.ListStyle {
for _, r := range tools {
fmt.Printf("%s\n", r.Name)
}
} else {
get.CreateToolsTable(tools, get.TableStyle)
}
} else {
get.CreateToolsTable(tools, get.TableStyle)
}
return nil
}
version := ""
if command.Flags().Changed("version") {
version, _ = command.Flags().GetString("version")
}
downloadURLs, err := get.GetDownloadURLs(tools, args, version)
if err != nil {
return err
}
movePath, _ := command.Flags().GetString("path")
progress, _ := command.Flags().GetBool("progress")
quiet, _ := command.Flags().GetBool("quiet")
if quiet && !command.Flags().Changed("progress") {
progress = false
}
if p, ok := os.LookupEnv("ARKADE_PROGRESS"); ok {
b, err := strconv.ParseBool(p)
if err != nil {
return fmt.Errorf("ARKADE_PROGRESS is not a valid boolean")
}
progress = b
}
movePath = os.ExpandEnv(movePath)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChan
os.Exit(2)
}()
var outFilePath string
var localToolsStore []get.ToolLocal
arch, _ := command.Flags().GetString("arch")
if err := get.ValidateArch(arch); err != nil {
return err
}
operatingSystem, _ := command.Flags().GetString("os")
if err := get.ValidateOS(operatingSystem); err != nil {
return err
}
for _, tool := range downloadURLs {
if !quiet {
fmt.Printf("Downloading: %s\n", tool.Name)
}
outFilePath, _, err = get.Download(&tool,
arch,
operatingSystem,
version,
movePath,
progress,
quiet)
// handle 404 error gracefully
if errors.Is(err, &get.ErrNotFound{}) {
extra := ""
// 1. The tool either has an explicit GitHub URL
// 2. or has no URL in the URLTemplate meaning it's on GitHub
// 3. or there is no URLTemplate because a BinaryTemplate was used instead, meaning the tool is on GitHub
if strings.Contains(tool.URLTemplate, "https://github.com/") ||
!strings.Contains(tool.URLTemplate, "https://") ||
len(tool.URLTemplate) == 0 {
extra = fmt.Sprintf(`
* View the %s releases page: %s`, tool.Name, fmt.Sprintf("https://github.com/%s/%s/releases", tool.Owner, tool.Repo))
}
fmt.Fprintf(os.Stderr, `
The requested version of %s is not available or configured in arkade for %s/%s
* Check if a binary is available from the project for your Operating System%s
* Feel free to raise an issue at https://github.com/alexellis/arkade/issues for help
`, tool.Name, operatingSystem, arch, extra)
return err
}
if err != nil {
return err
}
localToolsStore = append(localToolsStore, get.ToolLocal{Name: tool.Name, Path: outFilePath})
if !quiet {
size := ""
stat, err := os.Stat(outFilePath)
if err == nil {
size = "(" + units.HumanSize(float64(stat.Size())) + ")"
}
fmt.Printf("\nWrote: %s %s\n\n", outFilePath, size)
}
}
nl := ""
if !quiet {
nl = "\n"
msg, err := get.PostInstallationMsg(movePath, localToolsStore)
if err != nil {
return err
}
fmt.Printf("%s\n", msg)
}
if !quiet {
fmt.Printf("%s%s\n", nl, aec.Bold.Apply(pkg.SupportMessageShort))
}
return err
}
return command
}