-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathremove.go
More file actions
175 lines (151 loc) · 5.01 KB
/
Copy pathremove.go
File metadata and controls
175 lines (151 loc) · 5.01 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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/urfave/cli/v3"
"github.com/zeebo/errs"
)
var (
errRemoveFailed = errs.Class("removal failed")
)
func removeCommand() *cli.Command {
return &cli.Command{
Name: "remove",
Aliases: []string{"del"},
Usage: "Remove binaries",
Action: func(_ context.Context, c *cli.Command) error {
config, err := loadConfig()
if err != nil {
return errRemoveFailed.Wrap(err)
}
return removeBinaries(config, arrStringToArrBinaryEntry(c.Args().Slice()))
},
}
}
func removeBinaries(config *config, bEntries []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()
// Try to find the binary by name or by matching user.FullName
binaryPath, trackedBEntry, err := findBinaryByNameOrFullName(installDir, bEntry.Name)
if err != nil {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Warning: '%s' does not exist or was not installed by dbin: %v\n", bEntry.Name, err)
}
return
}
licensePath := filepath.Join(config.LicenseDir, fmt.Sprintf("%s_LICENSE", filepath.Base(parseBinaryEntry(trackedBEntry, false))))
if !fileExists(binaryPath) {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Warning: '%s' does not exist in %s\n", bEntry.Name, installDir)
}
return
}
if trackedBEntry.PkgID == "" {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Warning: '%s' was not installed by dbin\n", bEntry.Name)
}
return
}
if err := runDeintegrationHooks(config, binaryPath); err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "Error running deintegration hooks for '%s': %v\n", bEntry.Name, err)
}
mutex.Lock()
removeErrors = append(removeErrors, err.Error())
mutex.Unlock()
return
}
err = os.Remove(binaryPath)
if err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "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)
}
// Remove corresponding license file if it exists
if config.CreateLicenses && fileExists(licensePath) {
if err := os.Remove(licensePath); err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "Warning: Failed to remove license file %s: %v\n", licensePath, err)
}
// Non-fatal error
} else if verbosityLevel >= normalVerbosity {
fmt.Printf("Removed license file %s\n", licensePath)
}
}
}
}(bEntry)
}
wg.Wait()
if len(removeErrors) > 0 {
return errRemoveFailed.New(strings.Join(removeErrors, "\n"))
}
return nil
}
// findBinaryByNameOrFullName searches for a binary in installDir by its name or by matching the user.FullName xattr.
func findBinaryByNameOrFullName(installDir, name string) (string, binaryEntry, error) {
// First, try direct path
binaryPath := filepath.Join(installDir, filepath.Base(name))
if fileExists(binaryPath) {
trackedBEntry, err := readEmbeddedBEntry(binaryPath)
if err == nil && trackedBEntry.Name != "" {
return binaryPath, trackedBEntry, nil
}
}
// If direct path fails, scan directory for matching user.FullName
entries, err := os.ReadDir(installDir)
if err != nil {
return "", binaryEntry{}, errFileAccess.Wrap(err)
}
// Normalize the input name for comparison
inputBEntry := stringToBinaryEntry(name)
inputFullName := parseBinaryEntry(inputBEntry, false)
for _, entry := range entries {
if entry.IsDir() {
continue
}
binaryPath = filepath.Join(installDir, entry.Name())
if !isExecutable(binaryPath) || isSymlink(binaryPath) {
continue
}
trackedBEntry, err := readEmbeddedBEntry(binaryPath)
if err != nil || trackedBEntry.Name == "" {
continue
}
trackedFullName := parseBinaryEntry(trackedBEntry, false)
if trackedFullName == inputFullName || trackedBEntry.Name == filepath.Base(name) {
return binaryPath, trackedBEntry, nil
}
}
return "", binaryEntry{}, errFileNotFound.New("binary '%s' not found in %s", name, installDir)
}
func runDeintegrationHooks(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, false); err != nil {
return errRemoveFailed.Wrap(err)
}
} else if hookCommands, exists := config.Hooks.Commands["*"]; exists {
if err := executeHookCommand(config, &hookCommands, ext, binaryPath, false); err != nil {
return errRemoveFailed.Wrap(err)
}
}
}
return nil
}