-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathremove.go
115 lines (98 loc) · 3.11 KB
/
remove.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"context"
"github.com/urfave/cli/v3"
)
func removeCommand() *cli.Command {
return &cli.Command{
Name: "remove",
Aliases: []string{"del"},
Usage: "Remove binaries",
Action: func(ctx context.Context, c *cli.Command) error {
config, err := loadConfig()
if err != nil {
return err
}
uRepoIndex := fetchRepoIndex(config)
return removeBinaries(config, arrStringToArrBinaryEntry(c.Args().Slice()), getVerbosityLevel(c), uRepoIndex)
},
}
}
func removeBinaries(config *Config, bEntries []binaryEntry, verbosityLevel Verbosity, uRepoIndex []binaryEntry) error {
var wg sync.WaitGroup
var removeErrors []string
var mutex sync.Mutex
installDir := config.InstallDir
for _, bEntry := range bEntries {
wg.Add(1)
go func(bEntry binaryEntry) {
defer wg.Done()
installPath := filepath.Join(installDir, filepath.Base(bEntry.Name))
trackedBEntry, err := readEmbeddedBEntry(installPath)
if err != nil {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Warning: Failed to retrieve full name for '%s#%s'. Skipping removal.\n", bEntry.Name, bEntry.PkgId)
}
return
}
if filepath.Base(bEntry.Name) != filepath.Base(trackedBEntry.Name) {
installPath = filepath.Join(installDir, filepath.Base(trackedBEntry.Name))
}
if !fileExists(installPath) {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Warning: '%s' does not exist in %s. Skipping removal.\n", bEntry.Name, installDir)
}
return
}
if trackedBEntry.PkgId == "" {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Skipping '%s': it was not installed by dbin\n", bEntry.Name)
}
return
}
if err := runDeintegrationHooks(config, installPath, verbosityLevel, uRepoIndex); err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
}
mutex.Lock()
removeErrors = append(removeErrors, err.Error())
mutex.Unlock()
return
}
err = os.Remove(installPath)
if err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "error: failed to remove '%s' from %s. %v\n", bEntry.Name, installDir, err)
}
mutex.Lock()
removeErrors = append(removeErrors, fmt.Sprintf("failed to remove '%s' from %s: %v", bEntry.Name, installDir, err))
mutex.Unlock()
} else if verbosityLevel >= silentVerbosityWithErrors {
fmt.Printf("'%s' removed from %s\n", bEntry.Name, installDir)
}
}(bEntry)
}
wg.Wait()
if len(removeErrors) > 0 {
return fmt.Errorf(strings.Join(removeErrors, "\n"))
}
return nil
}
func runDeintegrationHooks(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.DeintegrationCommands {
if err := executeHookCommand(config, cmd, binaryPath, ext, false, verbosityLevel, uRepoIndex); err != nil {
return err
}
}
}
}
return nil
}