-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstall.go
More file actions
215 lines (186 loc) · 5.86 KB
/
install.go
File metadata and controls
215 lines (186 loc) · 5.86 KB
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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/hedzr/progressbar"
"github.com/hedzr/progressbar/cursor"
"github.com/urfave/cli/v3"
"github.com/zeebo/errs"
)
var (
errInstallFailed = errs.Class("installation failed")
)
func installCommand() *cli.Command {
return &cli.Command{
Name: "install",
Aliases: []string{"add"},
Usage: "Install binaries",
Action: func(_ context.Context, c *cli.Command) error {
config, err := loadConfig()
if err != nil {
return err
}
uRepoIndex, err := fetchRepoIndex(config)
if err != nil {
return err
}
return installBinaries(context.Background(), config, arrStringToArrBinaryEntry(c.Args().Slice()), uRepoIndex)
},
}
}
func installBinaries(ctx context.Context, config *config, bEntries []binaryEntry, uRepoIndex []binaryEntry) error {
cursor.Hide()
defer cursor.Show()
// Clean up old .tmp files before installation
if err := cleanInstallCache(config); err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "Warning: Failed to clean up .tmp files in %s: %v\n", config.InstallDir, err)
}
}
var wg sync.WaitGroup
var errors []string
var errorsMu sync.Mutex
// Find URLs for binaries
binResults, err := findURL(bEntries, uRepoIndex, config)
if err != nil {
return errInstallFailed.Wrap(err)
}
filteredResults := make([]binaryEntry, 0, len(binResults))
for _, result := range binResults {
if result.DownloadURL != "!not_found" {
filteredResults = append(filteredResults, result)
}
}
if len(filteredResults) == 0 {
return errInstallFailed.New("no valid binaries found to install")
}
var bar progressbar.MultiPB
var tasks *progressbar.Tasks
if verbosityLevel >= normalVerbosity {
bar = progressbar.New()
tasks = progressbar.NewTasks(bar)
defer tasks.Close()
}
binaryNameMaxlen := 0
for _, result := range filteredResults {
if binaryNameMaxlen < len(result.Name) {
binaryNameMaxlen = len(result.Name)
}
}
termWidth := getTerminalWidth()
for _, result := range filteredResults {
wg.Add(1)
bEntry := result
destination := filepath.Join(config.InstallDir, filepath.Base(bEntry.Name))
if verbosityLevel >= normalVerbosity {
barTitle := fmt.Sprintf("Installing %s", bEntry.Name)
pbarOpts := []progressbar.Opt{
progressbar.WithBarStepper(config.ProgressbarStyle),
progressbar.WithBarResumeable(true),
}
if termWidth < 120 {
barTitle = bEntry.Name
pbarOpts = append(
pbarOpts,
progressbar.WithBarTextSchema(`{{.Bar}} {{.Percent}} | <font color="green">{{.Title}}</font>`),
progressbar.WithBarWidth(termWidth-(binaryNameMaxlen+19)),
)
}
tasks.Add(
progressbar.WithTaskAddBarTitle(barTitle),
progressbar.WithTaskAddBarOptions(pbarOpts...),
progressbar.WithTaskAddOnTaskProgressing(func(bar progressbar.PB, _ <-chan struct{}) (stop bool) {
defer wg.Done()
err := fetchBinaryFromURLToDest(ctx, bar, &bEntry, destination, config)
if err != nil {
errorsMu.Lock()
errors = append(errors, fmt.Sprintf("error fetching binary %s: %v\n", bEntry.Name, err))
errorsMu.Unlock()
return
}
if err := os.Chmod(destination, 0755); err != nil {
errorsMu.Lock()
errors = append(errors, fmt.Sprintf("error making binary executable %s: %v\n", destination, err))
errorsMu.Unlock()
return
}
binInfo := &bEntry
if err := embedBEntry(destination, *binInfo); err != nil {
errorsMu.Lock()
errors = append(errors, fmt.Sprintf("failed to embed the binary's bEntry to its xattr attributes: %v\n", err))
errorsMu.Unlock()
return
}
if err := runIntegrationHooks(config, destination); err != nil {
errorsMu.Lock()
errors = append(errors, fmt.Sprintf("[%s] could not be handled by its default hooks: %v\n", bEntry.Name, err))
errorsMu.Unlock()
return
}
return
}),
)
} else {
go func(bEntry binaryEntry, destination string) {
defer wg.Done()
err := fetchBinaryFromURLToDest(ctx, nil, &bEntry, destination, config)
if err != nil {
errorsMu.Lock()
errors = append(errors, fmt.Sprintf("error fetching binary %s: %v", bEntry.Name, err))
errorsMu.Unlock()
return
}
if err := os.Chmod(destination, 0755); err != nil {
errorsMu.Lock()
errors = append(errors, fmt.Sprintf("error making binary executable %s: %v", destination, err))
errorsMu.Unlock()
return
}
binInfo := &bEntry
if err := embedBEntry(destination, *binInfo); err != nil {
errorsMu.Lock()
errors = append(errors, fmt.Sprintf("failed to embed the binary's bEntry to its xattr attributes: %v\n", err))
errorsMu.Unlock()
return
}
if err := runIntegrationHooks(config, destination); err != nil {
errorsMu.Lock()
errors = append(errors, fmt.Sprintf("[%s] could not be handled by its default hooks: %v", bEntry.Name, err))
errorsMu.Unlock()
return
}
if verbosityLevel >= normalVerbosity {
fmt.Printf("Successfully installed [%s]\n", binInfo.Name+"#"+binInfo.PkgID)
}
}(bEntry, destination)
}
}
wg.Wait()
if len(errors) > 0 {
var errN = uint8(0)
for _, errMsg := range errors {
errN++
fmt.Printf("%d. %v\n", errN, errMsg)
}
return errInstallFailed.New("installation completed with errors")
}
return nil
}
func runIntegrationHooks(config *config, binaryPath string) error {
if config.UseIntegrationHooks {
ext := filepath.Ext(binaryPath)
if hookCommands, exists := config.Hooks.Commands[ext]; exists {
if err := executeHookCommand(config, &hookCommands, ext, binaryPath, true); err != nil {
return errInstallFailed.Wrap(err)
}
} else if hookCommands, exists := config.Hooks.Commands["*"]; exists {
if err := executeHookCommand(config, &hookCommands, ext, binaryPath, true); err != nil {
return errInstallFailed.Wrap(err)
}
}
}
return nil
}