-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall.go
173 lines (149 loc) · 5.13 KB
/
install.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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/urfave/cli/v3"
"github.com/hedzr/progressbar"
"github.com/hedzr/progressbar/cursor"
)
func installCommand() *cli.Command {
return &cli.Command{
Name: "install",
Aliases: []string{"add"},
Usage: "Install binaries",
Action: func(ctx context.Context, c *cli.Command) error {
config, err := loadConfig()
if err != nil {
return err
}
uRepoIndex := fetchRepoIndex(config)
return installBinaries(context.Background(), config, arrStringToArrBinaryEntry(c.Args().Slice()), getVerbosityLevel(c), uRepoIndex)
},
}
}
func installBinaries(ctx context.Context, config *Config, bEntries []binaryEntry, verbosityLevel Verbosity, uRepoIndex []binaryEntry) error {
cursor.Hide()
defer cursor.Show()
var wg sync.WaitGroup
var errors []string
urls, checksums, err := findURL(config, bEntries, verbosityLevel, uRepoIndex)
if err != nil {
return err
}
// Only create the progress bar if not in silent mode
var bar progressbar.MultiPB
var tasks *progressbar.Tasks
if verbosityLevel >= normalVerbosity {
bar = progressbar.New()
tasks = progressbar.NewTasks(bar)
defer tasks.Close()
}
binaryNameMaxlen := 0
for _, bEntry := range bEntries {
if binaryNameMaxlen < len(bEntry.Name) {
binaryNameMaxlen = len(bEntry.Name)
}
}
termWidth := getTerminalWidth()
for i, bEntry := range bEntries {
wg.Add(1)
url := urls[i]
checksum := checksums[i]
destination := filepath.Join(config.InstallDir, filepath.Base(bEntry.Name))
// Skip fetch if URL is "!not_found"
if url == "!not_found" {
errors = append(errors, fmt.Sprintf("error: didn't find download URL for [%s]", bEntry.Name))
wg.Done()
continue
}
if verbosityLevel >= normalVerbosity {
barTitle := fmt.Sprintf("Installing %s", bEntry.Name)
pbarOpts := []progressbar.Opt{
progressbar.WithBarStepper(config.ProgressbarStyle),
}
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, exitCh <-chan struct{}) {
defer wg.Done()
_, fetchErr := fetchBinaryFromURLToDest(ctx, bar, url, checksum, destination)
if fetchErr != nil {
errors = append(errors, fmt.Sprintf("error: error fetching binary %s: %v", bEntry.Name, fetchErr))
return
}
if err := os.Chmod(destination, 0755); err != nil {
errors = append(errors, fmt.Sprintf("error: error making binary executable %s: %v", destination, err))
return
}
if err := runIntegrationHooks(config, destination, verbosityLevel, uRepoIndex); err != nil {
errors = append(errors, fmt.Sprintf("error: [%s] could not be handled by its default hooks: %v", bEntry.Name, err))
return
}
binInfo, _ := getBinaryInfo(config, bEntry, uRepoIndex)
if err := embedBEntry(destination, *binInfo); err != nil {
errors = append(errors, fmt.Sprintf("error: failed to add fullName property to the binary's xattr %s: %v", destination, err))
return
}
}),
)
} else {
go func(bEntry binaryEntry, url, checksum, destination string) {
defer wg.Done()
_, fetchErr := fetchBinaryFromURLToDest(ctx, nil, url, checksum, destination)
if fetchErr != nil {
errors = append(errors, fmt.Sprintf("error: error fetching binary %s: %v", bEntry.Name, fetchErr))
return
}
if err := os.Chmod(destination, 0755); err != nil {
errors = append(errors, fmt.Sprintf("error: error making binary executable %s: %v", destination, err))
return
}
if err := runIntegrationHooks(config, destination, verbosityLevel, uRepoIndex); err != nil {
errors = append(errors, fmt.Sprintf("error: [%s] could not be handled by its default hooks: %v", bEntry.Name, err))
return
}
binInfo, _ := getBinaryInfo(config, bEntry, uRepoIndex)
if err := embedBEntry(destination, *binInfo); err != nil {
errors = append(errors, fmt.Sprintf("error: failed to add fullName property to the binary's xattr %s: %v", destination, err))
return
}
if verbosityLevel >= normalVerbosity {
fmt.Printf("Successfully installed [%s]\n", binInfo.Name+"#"+binInfo.PkgId)
}
}(bEntry, url, checksum, destination)
}
}
wg.Wait()
if len(errors) > 0 {
var errN = uint8(0)
for _, errMsg := range errors {
errN += 1
fmt.Printf("%d. %v\n", errN, errMsg)
}
}
return nil
}
func runIntegrationHooks(config *Config, binaryPath string, verbosityLevel Verbosity, uRepoIndex []binaryEntry) error {
if config.UseIntegrationHooks {
ext := filepath.Ext(binaryPath)
if hookCommands, exists := config.Hooks.Commands[ext]; exists {
for _, cmd := range hookCommands.IntegrationCommands {
if err := executeHookCommand(config, cmd, binaryPath, ext, config.UseIntegrationHooks, verbosityLevel, uRepoIndex); err != nil {
return err
}
}
}
}
return nil
}