forked from alexellis/arkade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
143 lines (114 loc) · 3.45 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
// Copyright (c) arkade author(s) 2020. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package cmd
import (
"fmt"
"os"
"os/signal"
"sort"
"strconv"
"syscall"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// 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
arkade get linkerd2 --stash=false
arkade get terraform --version=0.12.0
arkade get kubectl --progress=false
# Get a complete list of CLIs to download:
arkade get --help`,
SilenceUsage: true,
Aliases: []string{"g"},
ValidArgs: validToolOptions,
}
command.Flags().Bool("progress", true, "Display a progress bar")
command.Flags().Bool("stash", true, "When set to true, stash binary in HOME/.arkade/bin/, otherwise store in /tmp/")
command.Flags().StringP("version", "v", "", "Download a specific version")
command.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
const arkadeGet = `Use "arkade get TOOL" to download a tool or application:`
buf := ""
for _, t := range tools {
buf = buf + t.Name + "\n"
}
fmt.Println(arkadeGet + "\n" + buf)
return nil
}
var tool *get.Tool
if len(args) == 1 {
for _, t := range tools {
if t.Name == args[0] {
tool = &t
break
}
}
}
if tool == nil {
return fmt.Errorf("cannot get tool: %s", args[0])
}
fmt.Printf("Downloading %s\n", tool.Name)
arch, operatingSystem := env.GetClientArch()
version := ""
if command.Flags().Changed("version") {
version, _ = command.Flags().GetString("version")
}
stash, _ := command.Flags().GetBool("stash")
progress, _ := command.Flags().GetBool("progress")
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
}
dlMode := get.DownloadTempDir
if stash {
dlMode = get.DownloadArkadeDir
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-signalChan:
os.Exit(2)
}
}()
outFilePath, finalName, err := get.Download(tool, arch, operatingSystem, version, dlMode, progress)
if err != nil {
return errors.Wrap(err, "check with the vendor whether this tool is available for your system")
}
fmt.Printf("Tool written to: %s\n\n", outFilePath)
if dlMode == get.DownloadTempDir {
fmt.Printf(`Run the following to copy to install the tool:
chmod +x %s
sudo install -m 755 %s /usr/local/bin/%s
`, outFilePath, outFilePath, finalName)
} else {
fmt.Printf(`# Add (%s) to your PATH variable
export PATH=$PATH:$HOME/.arkade/bin/
# Test the binary:
%s
# Or install with:
sudo mv %s /usr/local/bin/
`, finalName, outFilePath, outFilePath)
}
return err
}
return command
}