@@ -15,22 +15,17 @@ import (
15
15
)
16
16
17
17
const TRASH_DIR_PERMISSIONS = 0755
18
+ const INTERACTIVE_THRESHOLD = 3
18
19
19
20
func RmPatch (arguments []string , config models.Config ) {
20
- forceHardDelete := util .InArray (arguments , cli .HARD_DELETE_CLA )
21
- forceSoftDelete := util .InArray (arguments , cli .SOFT_DELETE_CLA )
22
21
silent := util .InArray (arguments , cli .SILENT_CLA )
23
22
dryRun := util .InArray (arguments , cli .DRY_RUN_CLA )
24
- bypassProtected := util .InArray (arguments , cli .BYPASS_PROTECTED_CLA )
25
- overwrite := util .InArray (arguments , cli .OVERWRITE_CLA )
26
23
shouldNotify := util .InArray (arguments , cli .NOTIFICATION_CLA )
27
24
28
25
requestingHelp := util .InArray (arguments , cli .HELP_CLA )
29
26
requestingVersion := util .InArray (arguments , cli .VERSION_CLA )
30
27
31
- actionedArgs := removeUnNeededArguments (
32
- removeDangerousArguments (arguments ),
33
- )
28
+ actionedArgs := removeDangerousArguments (arguments )
34
29
35
30
if requestingHelp {
36
31
cli .PrintHelp ()
@@ -54,36 +49,7 @@ func RmPatch(arguments []string, config models.Config) {
54
49
return
55
50
}
56
51
57
- for _ , path := range filePaths {
58
- absolutePath := relativeToAbsolute (path )
59
- isTmp := isTmpPath (absolutePath )
60
-
61
- isProtected := config .IsProtected (absolutePath )
62
- if isProtected && bypassProtected {
63
- fmt .Println ("Cannot delete protected file:" , absolutePath )
64
- fmt .Println ("Use the --bypass-protected flag to force deletion" )
65
- continue
66
- }
67
-
68
- isConfigHardDelete := config .ShouldHardDelete (absolutePath )
69
- isConfigSoftDelete := config .ShouldSoftDelete (absolutePath )
70
-
71
- // overwriting a file is not exclusive to hard/soft deletes
72
- // meaning that you can overwrite the contents of a file with zeros and
73
- // also soft delete it
74
- // I have made this decision because I think soft-deleting an
75
- // overwritten file has auditing/logging use cases
76
- // e.g. Who deleted this file? When was it deleted?
77
- // if we hard deleted the file, we would lose this information
78
- isConfigOverwrite := config .ShouldOverwrite (absolutePath )
79
- if overwrite || isConfigOverwrite {
80
- overwriteFile (absolutePath )
81
- }
82
-
83
- shouldHardDelete := isTmp || forceHardDelete || isConfigHardDelete && ! isConfigSoftDelete && ! forceSoftDelete
84
-
85
- deletePath (absolutePath , shouldHardDelete , config , arguments )
86
- }
52
+ deletePaths (filePaths , config , arguments )
87
53
88
54
if shouldNotify {
89
55
fileNames := strings .Join (filePaths , ", " )
@@ -94,28 +60,6 @@ func RmPatch(arguments []string, config models.Config) {
94
60
}
95
61
}
96
62
97
- func removeUnNeededArguments (arguments []string ) []string {
98
- returnedArguments := []string {}
99
- unNeededArguments := []string {
100
- "-r" ,
101
- cli .HARD_DELETE_CLA ,
102
- cli .SOFT_DELETE_CLA ,
103
- cli .SILENT_CLA ,
104
- cli .DRY_RUN_CLA ,
105
- cli .BYPASS_PROTECTED_CLA ,
106
- cli .OVERWRITE_CLA ,
107
- cli .NOTIFICATION_CLA ,
108
- }
109
-
110
- for _ , arg := range arguments {
111
- if ! util .InArray (unNeededArguments , arg ) {
112
- returnedArguments = append (returnedArguments , arg )
113
- }
114
- }
115
-
116
- return returnedArguments
117
- }
118
-
119
63
func removeDangerousArguments (arguments []string ) []string {
120
64
// I have excluded the root slash as a forbidden argument just incase
121
65
// you make a typo like rm ./myDirectory /
@@ -165,20 +109,61 @@ func backupFileName(path string) string {
165
109
return result + ".bak"
166
110
}
167
111
168
- func deletePath (path string , hard bool , config models.Config , arguments []string ) {
169
- isInteractive := util .InArray (arguments , cli .INTERACTIVE_CLA )
112
+ func deletePaths (paths []string , config models.Config , arguments []string ) {
113
+ forceHardDelete := util .InArray (arguments , cli .HARD_DELETE_CLA )
114
+ forceSoftDelete := util .InArray (arguments , cli .SOFT_DELETE_CLA )
115
+ bypassProtected := util .InArray (arguments , cli .BYPASS_PROTECTED_CLA )
116
+ overwrite := util .InArray (arguments , cli .OVERWRITE_CLA )
170
117
171
- if isInteractive {
172
- fmt .Println ("Are you sure you want to delete" , path , "? (y/n)" )
173
- var response string
174
- fmt .Scanln (& response )
118
+ hasInteraciveCla := util .InArray (arguments , cli .INTERACTIVE_CLA )
119
+ hasGroupInteractiveCla := util .InArray (arguments , cli .INTERACTIVE_GROUP_CLA )
120
+ isInteractiveGroup := hasGroupInteractiveCla && len (paths ) >= INTERACTIVE_THRESHOLD
121
+ isInteractive := hasInteraciveCla || isInteractiveGroup
122
+
123
+ for _ , path := range paths {
124
+ if isInteractive {
125
+ fmt .Println ("Are you sure you want to delete" , path , "? (y/n)" )
126
+ var response string
127
+ fmt .Scanln (& response )
128
+
129
+ if response != "y" && response != "yes" {
130
+ fmt .Println ("Skipping file" , path )
131
+ continue
132
+ }
133
+ }
175
134
176
- if response != "y" && response != "yes" {
177
- fmt .Println ("Exiting without removing file(s)." )
178
- return
135
+ absolutePath := relativeToAbsolute (path )
136
+ isTmp := isTmpPath (absolutePath )
137
+
138
+ isProtected := config .IsProtected (absolutePath )
139
+ if isProtected && bypassProtected {
140
+ fmt .Println ("Cannot delete protected file:" , absolutePath )
141
+ fmt .Println ("Use the --bypass-protected flag to force deletion" )
142
+ continue
143
+ }
144
+
145
+ isConfigHardDelete := config .ShouldHardDelete (absolutePath )
146
+ isConfigSoftDelete := config .ShouldSoftDelete (absolutePath )
147
+
148
+ // overwriting a file is not exclusive to hard/soft deletes
149
+ // meaning that you can overwrite the contents of a file with zeros and
150
+ // also soft delete it
151
+ // I have made this decision because I think soft-deleting an
152
+ // overwritten file has auditing/logging use cases
153
+ // e.g. Who deleted this file? When was it deleted?
154
+ // if we hard deleted the file, we would lose this information
155
+ isConfigOverwrite := config .ShouldOverwrite (absolutePath )
156
+ if overwrite || isConfigOverwrite {
157
+ overwriteFile (absolutePath )
179
158
}
159
+
160
+ shouldHardDelete := isTmp || forceHardDelete || isConfigHardDelete && ! isConfigSoftDelete && ! forceSoftDelete
161
+
162
+ deletePath (absolutePath , shouldHardDelete , config , arguments )
180
163
}
164
+ }
181
165
166
+ func deletePath (path string , hard bool , config models.Config , arguments []string ) {
182
167
if hard {
183
168
hardDelete (path )
184
169
} else {
@@ -225,6 +210,12 @@ func softDelete(filePath string, tempDir string) {
225
210
}
226
211
227
212
err = os .Remove (absoluteSrcPath )
213
+ if err != nil {
214
+ fmt .Println ("Error deleting file:" , err )
215
+
216
+ // pause the program so the user can see the error message
217
+ fmt .Scanln ()
218
+ }
228
219
}
229
220
230
221
func hardDelete (filePath string ) {
0 commit comments