-
Notifications
You must be signed in to change notification settings - Fork 3
/
remove.go
116 lines (100 loc) · 3.65 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
116
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
)
// removeBinaries processes each binary, removing only those that pass validation.
func removeBinaries(config *Config, binaries []string, verbosityLevel Verbosity, metadata map[string]interface{}) error {
var wg sync.WaitGroup
var removeErrors []string
var mutex sync.Mutex
installDir := config.InstallDir
// Loop over the binaries and remove the valid ones
for _, binaryName := range binaries {
wg.Add(1)
go func(binaryName string) {
defer wg.Done()
installPath := filepath.Join(installDir, filepath.Base(binaryName))
// Get the full name of the binary installed
fullBinaryName, err := getFullName(installPath)
if err != nil {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Warning: Failed to retrieve full name for '%s'. Skipping removal.\n", binaryName)
}
return
}
// Compare the base name of the given binary and the full binary name
if filepath.Base(binaryName) != filepath.Base(fullBinaryName) {
// Use the base name of fullBinaryName for removal
installPath = filepath.Join(installDir, filepath.Base(fullBinaryName))
}
// Remove everything after the # (including the #) (if it exists)
parts := strings.SplitN(installPath, "#", 2)
installPath = parts[0]
// Check if the binary exists before proceeding
if !fileExists(installPath) {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Warning: '%s' does not exist in %s. Skipping removal.\n", binaryName, installDir)
}
return
}
// Validate if the binary was installed by checking the full binary name
if fullBinaryName == "" {
if verbosityLevel >= normalVerbosity {
fmt.Fprintf(os.Stderr, "Skipping '%s': it was not installed by dbin\n", binaryName)
}
return
}
// Run deintegration hooks before removing the binary
if err := runDeintegrationHooks(config, installPath, verbosityLevel, metadata); err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
}
// Add error to the list in a thread-safe way
mutex.Lock()
removeErrors = append(removeErrors, err.Error())
mutex.Unlock()
return
}
// Remove the binary
err = os.Remove(installPath)
if err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "error: failed to remove '%s' from %s. %v\n", binaryName, installDir, err)
}
// Add error to the list in a thread-safe way
mutex.Lock()
removeErrors = append(removeErrors, fmt.Sprintf("failed to remove '%s' from %s: %v", binaryName, installDir, err))
mutex.Unlock()
} else if verbosityLevel <= extraVerbose {
fmt.Printf("'%s' removed from %s\n", binaryName, installDir)
}
}(binaryName)
}
// Wait for all goroutines to finish
wg.Wait()
// Return concatenated errors if any occurred
if len(removeErrors) > 0 {
return fmt.Errorf(strings.Join(removeErrors, "\n"))
}
return nil
}
// runDeintegrationHooks runs the deintegration hooks for binaries which need to be deintegrated
func runDeintegrationHooks(config *Config, binaryPath string, verbosityLevel Verbosity, metadata map[string]interface{}) error {
if config.UseIntegrationHooks {
// Infer the file extension from the binaryPath
ext := filepath.Ext(binaryPath)
if hookCommands, exists := config.Hooks.Commands[ext]; exists {
// Execute user-defined deintegration hooks
for _, cmd := range hookCommands.DeintegrationCommands {
if err := executeHookCommand(config, cmd, binaryPath, ext, config.UseIntegrationHooks, verbosityLevel, metadata); err != nil {
return err
}
}
}
}
return nil
}