@@ -36,7 +36,6 @@ func RmPatch(arguments []string, config models.Config) {
36
36
}
37
37
38
38
filePaths := extractFilePaths (actionedArgs )
39
- extractedArguments := extractArguments (actionedArgs )
40
39
41
40
// a debug statement is useful for scripts, it provides explicit feedback
42
41
// and prints exactly what files were backed up / moved to the trash can
@@ -76,14 +75,9 @@ func RmPatch(arguments []string, config models.Config) {
76
75
overwriteFile (absolutePath )
77
76
}
78
77
79
- if isTmp || forceHardDelete || isConfigHardDelete && ! isConfigSoftDelete && ! forceSoftDelete {
80
- hardDelete ([]string {path }, extractedArguments )
81
- } else {
82
- // this function will return the default soft delete directory
83
- // if the user has not specified one in their config file
84
- softDeleteDir := config .SoftDeleteDir ()
85
- softDelete ([]string {path }, extractedArguments , softDeleteDir )
86
- }
78
+ shouldHardDelete := isTmp || forceHardDelete || isConfigHardDelete && ! isConfigSoftDelete && ! forceSoftDelete
79
+
80
+ deletePath (absolutePath , shouldHardDelete , config , arguments )
87
81
}
88
82
89
83
if shouldNotify {
@@ -160,18 +154,6 @@ func extractFilePaths(input []string) []string {
160
154
return filePaths
161
155
}
162
156
163
- func extractArguments (input []string ) []string {
164
- arguments := []string {}
165
-
166
- for _ , str := range input {
167
- if strings .HasPrefix (str , "-" ) {
168
- arguments = append (arguments , str )
169
- }
170
- }
171
-
172
- return arguments
173
- }
174
-
175
157
func isTmpPath (absolutePath string ) bool {
176
158
return strings .HasPrefix (absolutePath , "/tmp" )
177
159
}
@@ -192,10 +174,34 @@ func backupFileName(path string) string {
192
174
return result + ".bak"
193
175
}
194
176
177
+ func deletePath (path string , hard bool , config models.Config , arguments []string ) {
178
+ isInteractive := util .InArray (arguments , cli .INTERACTIVE_CLA )
179
+
180
+ if isInteractive {
181
+ fmt .Println ("Are you sure you want to delete" , path , "? (y/n)" )
182
+ var response string
183
+ fmt .Scanln (& response )
184
+
185
+ if response != "y" && response != "yes" {
186
+ fmt .Println ("Exiting without removing file(s)." )
187
+ return
188
+ }
189
+ }
190
+
191
+ if hard {
192
+ hardDelete (path )
193
+ } else {
194
+ // this function will return the default soft delete directory
195
+ // if the user has not specified one in their config file
196
+ softDeletePath := config .SoftDeleteDir ()
197
+ softDelete (path , softDeletePath )
198
+ }
199
+ }
200
+
195
201
// by default, we want to delete files to /tmp/2rm
196
202
// however, if the user has specified a different directory in their config file
197
203
// we use that instead
198
- func softDelete (filePaths [] string , arguments [] string , tempDir string ) {
204
+ func softDelete (filePath string , tempDir string ) {
199
205
deletedTimestamp := time .Now ().Format (time .RFC3339 )
200
206
backupDirectory := tempDir + deletedTimestamp
201
207
@@ -216,22 +222,25 @@ func softDelete(filePaths []string, arguments []string, tempDir string) {
216
222
}
217
223
}
218
224
219
- commandArguments := strings . Join ( arguments , " " )
225
+ absoluteSrcPath := relativeToAbsolute ( filePath )
220
226
221
- for _ , path := range filePaths {
222
- absoluteSrcPath := relativeToAbsolute (path )
223
-
224
- backupFileName := backupFileName (path )
225
- backupLocation := backupDirectory + "/" + backupFileName
227
+ backupFileName := backupFileName (filePath )
228
+ backupLocation := backupDirectory + "/" + backupFileName
226
229
227
- moveCommand := "mv " + commandArguments + " " + absoluteSrcPath + " " + backupLocation
228
- util .Execute (moveCommand )
230
+ err = util .CopyFile (absoluteSrcPath , backupLocation )
231
+ if err != nil {
232
+ fmt .Println ("Error moving file to trash:" , err )
233
+ return
229
234
}
235
+
236
+ err = os .Remove (absoluteSrcPath )
230
237
}
231
238
232
- func hardDelete (filePaths []string , arguments []string ) {
233
- command := "rm -r " + strings .Join (arguments , " " ) + " " + strings .Join (filePaths , " " )
234
- util .Execute (command )
239
+ func hardDelete (filePath string ) {
240
+ err := os .RemoveAll (filePath )
241
+ if err != nil {
242
+ fmt .Println ("Error deleting file:" , err )
243
+ }
235
244
}
236
245
237
246
func overwriteFile (filePath string ) {
0 commit comments