-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.go
87 lines (74 loc) · 2.68 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
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"sync"
)
// installBinary fetches and installs the binary, logging based on verbosity levels.
func installBinary(ctx context.Context, binaryName, installDir, trackerFile string, verbosityLevel Verbosity, repositories, metadataURLs []string) error {
url, checksum, err := findURL(binaryName, trackerFile, repositories, metadataURLs)
if err != nil {
// Return the error directly without printing/logging
return err
}
destination := filepath.Join(installDir, filepath.Base(binaryName))
_, err = fetchBinaryFromURLToDest(ctx, url, checksum, destination)
if err != nil {
// Return the error directly without printing/logging
return fmt.Errorf("error fetching binary %s: %v", binaryName, err)
}
if verbosityLevel >= normalVerbosity {
fmt.Printf("Successfully downloaded %s and put it at %s\n", binaryName, destination)
}
if trackerFile != "" {
err = addToTrackerFile(trackerFile, binaryName, installDir)
if err != nil {
// Return the error directly without printing/logging
return fmt.Errorf("failed to update tracker file for %s: %v", binaryName, err)
}
}
return nil
}
// multipleInstall installs multiple binaries concurrently, respecting verbosity levels.
func multipleInstall(ctx context.Context, binaries []string, installDir, trackerFile string, verbosityLevel Verbosity, repositories, metadataURLs []string) error {
var wg sync.WaitGroup
errChan := make(chan error, len(binaries))
for _, binaryName := range binaries {
wg.Add(1)
go func(binaryName string) {
defer wg.Done()
if err := installBinary(ctx, binaryName, installDir, trackerFile, verbosityLevel, repositories, metadataURLs); err != nil {
errChan <- err
}
}(binaryName)
}
go func() {
wg.Wait()
close(errChan)
}()
var errors []string
for err := range errChan {
errors = append(errors, err.Error())
}
if len(errors) > 0 {
// Join errors with newline character
finalErr := strings.Join(errors, "\n")
if verbosityLevel >= silentVerbosityWithErrors {
return fmt.Errorf(finalErr)
}
}
return nil
}
// installCommand installs one or more binaries based on the verbosity level.
func installCommand(binaries []string, installDir, trackerFile string, verbosityLevel Verbosity, repositories, metadataURLs []string) error {
if len(binaries) == 1 {
return installBinary(context.Background(), binaries[0], installDir, trackerFile, verbosityLevel, repositories, metadataURLs)
} else if len(binaries) > 1 {
// Remove duplicates before processing
binaries = removeDuplicates(binaries)
return multipleInstall(context.Background(), binaries, installDir, trackerFile, verbosityLevel, repositories, metadataURLs)
}
return nil
}