/
manager.go
288 lines (237 loc) · 7.13 KB
/
manager.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
package dep
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/mholt/archiver/v3"
"github.com/spotinst/spotctl/internal/log"
"github.com/spotinst/spotctl/internal/survey"
)
type manager struct {
survey survey.Interface
}
func NewManager(survey survey.Interface) Manager {
return &manager{
survey: survey,
}
}
func (x *manager) Install(ctx context.Context, dep Dependency, options ...InstallOption) error {
log.Debugf("Ensuring required dependency %q", dep.Executable())
opts := initDefaultOptions()
for _, opt := range options {
opt(opts)
}
present, err := isPresent(opts.BinaryDir, dep)
if err != nil {
return err
}
if !shouldInstallDep(opts.InstallPolicy, present) {
if present {
log.Debugf("Dependency %q already present on machine", dep.Executable())
return nil
}
return fmt.Errorf("dep: dependency %q is not present with "+
"install policy of %q", dep.Executable(), InstallNever)
}
return x.installWithConfirm(ctx, dep, opts)
}
func (x *manager) InstallBulk(ctx context.Context, deps []Dependency, options ...InstallOption) error {
log.Debugf("Ensuring required dependencies")
opts := initDefaultOptions()
for _, opt := range options {
opt(opts)
}
var missing []Dependency
for _, dep := range deps {
present, err := isPresent(opts.BinaryDir, dep)
if err != nil {
return err
}
if !shouldInstallDep(opts.InstallPolicy, present) {
if present {
log.Debugf("Dependency %q already present on machine", dep.Executable())
continue
}
return fmt.Errorf("dep: dependency %q is not present with "+
"install policy of %q", dep.Executable(), InstallNever)
}
missing = append(missing, dep)
}
return x.installWithSelect(ctx, missing, opts)
}
func (x *manager) DependencyPresent(dep Dependency, options ...InstallOption) (bool, error) {
opts := initDefaultOptions()
for _, opt := range options {
opt(opts)
}
return isPresent(opts.BinaryDir, dep)
}
func isPresent(binaryDir string, dep Dependency) (bool, error) {
path := filepath.Join(binaryDir, dep.Executable())
log.Debugf("Checking dependency existence %q", path)
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return false, err
}
present := err == nil
return present, nil
}
// shouldInstallDep returns whether we should install a Dependency according to
// the presence and install policy.
func shouldInstallDep(installPolicy InstallPolicy, present bool) bool {
if installPolicy == InstallNever {
return false
}
if installPolicy == InstallAlways ||
(installPolicy == InstallIfNotPresent && (!present)) {
return true
}
return false
}
// Lookup looks up for an executable named file in the directories named by the
// PATH environment variable. If found, it returns the absolute path to the binary
// executable file. Otherwise, an error is returned.
func (x *manager) lookup(ctx context.Context, dep Dependency, binaryDir string) (string, error) {
if err := x.initPath(binaryDir); err != nil {
return "", err
}
path, _ := exec.LookPath(dep.Executable())
return path, nil
}
var initPathOnce sync.Once
func (x *manager) initPath(binaryDir string) error {
var err error
initPathOnce.Do(func() {
log.Debugf("Initializing PATH (adding %q)", binaryDir)
path := os.Getenv("PATH")
dirs := filepath.SplitList(path)
for _, dir := range dirs {
if dir == binaryDir {
return // already exists
}
}
err = os.Setenv("PATH", strings.Join(append([]string{binaryDir}, dirs...), ":"))
})
return err
}
func (x *manager) installWithConfirm(ctx context.Context, dep Dependency, opts *InstallOptions) error {
if !opts.Noninteractive {
if ok := x.confirm(dep); !ok {
log.Debugf("Aborting installation of dependency %q", dep.Executable())
return nil
}
}
return x.install(ctx, dep, opts)
}
func (x *manager) installWithSelect(ctx context.Context, deps []Dependency, opts *InstallOptions) error {
if !opts.Noninteractive {
deps = x.selectMulti(deps)
}
for _, dep := range deps {
if err := x.install(ctx, dep, opts); err != nil {
return err
}
}
return nil
}
func (x *manager) install(ctx context.Context, dep Dependency, opts *InstallOptions) error {
if opts.DryRun {
log.Debugf("Would install %q to %q", dep.Executable(), opts.BinaryDir)
return nil
}
log.Infof("Installing dependency %q", dep.Executable())
url, err := dep.URL()
if err != nil {
return fmt.Errorf("dep: unable to render url: %w", err)
}
executable := filepath.Join(opts.BinaryDir, dep.Executable())
ext, archive := checkArchive(url.Path)
p := fmt.Sprintf("spotctl-*-%s-%s%s", dep.Name(), dep.Version(), ext)
f, err := ioutil.TempFile(os.TempDir(), p)
if err != nil {
return err
}
defer func() {
f.Close()
os.Remove(f.Name())
}()
intermediate := f.Name()
log.Debugf("Downloading dependency %q to %q", dep.Executable(), intermediate)
if err = download(url, intermediate); err != nil {
return err
}
if archive {
d := strings.TrimSuffix(intermediate, ext)
log.Debugf("Unarchiving dependency %q to %q", dep.Executable(), d)
if err = archiver.Unarchive(intermediate, d); err != nil {
return fmt.Errorf("dep: unable to unarchive file: %w", err)
}
defer os.Remove(d)
fi, err := os.Stat(d)
if err != nil {
return fmt.Errorf("dep: unable to describe file: %w", err)
}
if fi.Mode().IsDir() {
intermediate = filepath.Join(d, dep.UpstreamBinaryName())
}
}
log.Debugf("Copying dependency from %q to %q", intermediate, executable)
if err = copyFile(intermediate, executable); err != nil {
return fmt.Errorf("dep: unable to copy file: %w", err)
}
// Make it executable.
if err = os.Chmod(executable, 0755); err != nil {
return fmt.Errorf("dep: unable to set file as executable: %w", err)
}
// Create a symlink to command name.
command := filepath.Join(opts.BinaryDir, dep.Name())
if _, err = os.Stat(command); err == nil {
_ = os.Remove(command)
} else if !os.IsNotExist(err) {
return fmt.Errorf("dep: unable to stat %q: %w", command, err)
}
log.Debugf("Creating symlink %q to %q", command, executable)
return os.Symlink(dep.Executable(), command)
}
func (x *manager) confirm(dep Dependency) bool {
input := &survey.Input{
Message: fmt.Sprintf("Install missing required dependency: %s (v%s)", dep.Name(), dep.Version()),
Help: "Spot CLI would like to install missing required dependency",
Default: "true",
}
ok, _ := x.survey.Confirm(input)
return ok
}
func (x *manager) selectMulti(deps []Dependency) []Dependency {
var out []Dependency
depOpts := make([]interface{}, len(deps))
for i, dep := range deps {
depOpts[i] = fmt.Sprintf("%s (v%s)", dep.Name(), dep.Version())
}
input := &survey.Select{
Message: "Install missing required dependencies (deselect to avoid auto installing)",
Help: "Spot CLI would like to install missing required dependencies",
Options: depOpts,
Defaults: depOpts,
Transform: survey.TransformOnlyId,
}
depNames, err := x.survey.SelectMulti(input)
if err != nil {
return out
}
depMap := make(map[string]Dependency)
for _, dep := range deps {
depMap[dep.Name()] = dep
}
for _, name := range depNames {
if dep, ok := depMap[name]; ok {
out = append(out, dep)
}
}
return out
}