Skip to content

Commit 9b26fd7

Browse files
committed
Show correct warnings for non-existant files
1 parent b0f3497 commit 9b26fd7

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

src/patches/rm.go

+27-18
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
const TRASH_DIR_PERMISSIONS = 0755
1818

1919
func RmPatch(arguments []string, config models.Config) {
20-
silent := util.InArray(arguments, cli.SILENT_CLA)
21-
dryRun := util.InArray(arguments, cli.DRY_RUN_CLA)
2220
shouldNotify := util.InArray(arguments, cli.NOTIFICATION_CLA)
2321

2422
requestingHelp := util.InArray(arguments, cli.HELP_CLA)
@@ -35,19 +33,6 @@ func RmPatch(arguments []string, config models.Config) {
3533
}
3634

3735
filePaths := extractFilePaths(actionedArgs)
38-
39-
// a debug statement is useful for scripts, it provides explicit feedback
40-
// and prints exactly what files were backed up / moved to the trash can
41-
// after deletion
42-
debugStatement := strings.Join(filePaths, " ")
43-
if !silent {
44-
fmt.Println(debugStatement)
45-
}
46-
47-
if dryRun {
48-
return
49-
}
50-
5136
deletePaths(filePaths, config, arguments)
5237

5338
if shouldNotify {
@@ -118,6 +103,7 @@ func deletePaths(paths []string, config models.Config, arguments []string) {
118103
bypassProtected := util.InArray(arguments, cli.BYPASS_PROTECTED_CLA)
119104
overwrite := util.InArray(arguments, cli.OVERWRITE_CLA)
120105
silent := util.InArray(arguments, cli.SILENT_CLA)
106+
dryRun := util.InArray(arguments, cli.DRY_RUN_CLA)
121107

122108
hasInteraciveCla := util.InArray(arguments, cli.INTERACTIVE_CLA)
123109
hasGroupInteractiveCla := util.InArray(arguments, cli.INTERACTIVE_GROUP_CLA)
@@ -134,7 +120,13 @@ func deletePaths(paths []string, config models.Config, arguments []string) {
134120
hasVerboseCla = util.InArray(arguments, cli.VERBOSE_SHORT_CLA)
135121
}
136122

123+
removedFiles := []string{}
137124
for _, path := range paths {
125+
if !util.PathExists(path) {
126+
fmt.Println("2rm: Cannot remove '" + path + "': No such file or directory")
127+
continue
128+
}
129+
138130
if isInteractive {
139131
fmt.Println("Are you sure you want to delete", path, "? (y/n)")
140132
var response string
@@ -175,21 +167,38 @@ func deletePaths(paths []string, config models.Config, arguments []string) {
175167
// e.g. Who deleted this file? When was it deleted?
176168
// if we hard deleted the file, we would lose this information
177169
isConfigOverwrite := config.ShouldOverwrite(absolutePath)
178-
if overwrite || isConfigOverwrite {
170+
if overwrite || isConfigOverwrite && !dryRun {
179171
overwriteFile(absolutePath)
180172
}
181173

182174
shouldHardDelete := isTmp || forceHardDelete || isConfigHardDelete && !isConfigSoftDelete && !forceSoftDelete
183175

184-
deletePath(absolutePath, shouldHardDelete, config)
176+
deletePath(absolutePath, shouldHardDelete, dryRun, config)
185177

186178
if hasVerboseCla && !silent {
187179
fmt.Printf("removed '%s'\n", path)
188180
}
181+
182+
removedFiles = append(removedFiles, path)
183+
}
184+
185+
for _, path := range removedFiles {
186+
fmt.Print(path)
189187
}
188+
// do an empty print line last so that when the shell returns to a
189+
// prompt the prompt will be on its own line
190+
fmt.Println()
190191
}
191192

192-
func deletePath(path string, hard bool, config models.Config) {
193+
func deletePath(path string, hard bool, dryRun bool, config models.Config) {
194+
// I break out during dry runs at the last possible point so that dry
195+
// runs are as close to real runs as possible
196+
// all the same debug information, deletion prompts and errors will be
197+
// shown during dry runs
198+
if dryRun {
199+
return
200+
}
201+
193202
if hard {
194203
hardDelete(path)
195204
} else {

src/util/files.go

+5
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ func IsDirectoryEmpty(directory string) bool {
5454

5555
return len(files) == 0
5656
}
57+
58+
func PathExists(path string) bool {
59+
_, err := os.Stat(path)
60+
return err == nil
61+
}

0 commit comments

Comments
 (0)