Skip to content

Commit

Permalink
Add support for -d and --dir flags
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-newey committed Nov 18, 2024
1 parent 129f95d commit 101a4d5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A wrapper for the "rm" command with soft-deletes, config-based deletion, debug i
- `-I` Prompt if deleting more than the interactive threshold of files (default 3)
- `-r`, `-R`, `--recursive` Recursively delete a directory of files
- `-v`, `--verbose` Emit additional verbose information
- `-d`, `--dir` Only delete empty directories
- `--help` Display help information (without deletion)
- `--version` Display version information (without deletion)

Expand All @@ -26,7 +27,6 @@ A wrapper for the "rm" command with soft-deletes, config-based deletion, debug i

## Unsupported command line arguments

- `-d`, `--dir` Only delete empty directories
- `--interactive[=WHEN]` Interactive with a custom threshold
- `--one-file-system` Do not allow cross-file-system deletes
- `-f`, `--force` Bypass protections
Expand Down
4 changes: 4 additions & 0 deletions src/cli/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ const VERBOSE_SHORT_CLA = "-v"
// gnu rm CLI arguments
const INTERACTIVE_CLA = "-i"
const INTERACTIVE_GROUP_CLA = "-I"

const DIR_CLA = "-d"
const DIR_CLA_LONG = "--dir"

const HELP_CLA = "--help"
const VERSION_CLA = "--version"
2 changes: 2 additions & 0 deletions src/cli/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Mark FILE(s) for deletion.
-v, --verbose Add additional information to the output
-d, --dir Only remove empty directories
--help Display this help and (without deleting anything)
--version Output version information (without deleting anything)
Expand Down
13 changes: 13 additions & 0 deletions src/patches/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func deletePaths(paths []string, config models.Config, arguments []string) {
isInteractiveGroup := hasGroupInteractiveCla && len(paths) >= config.InteractiveThreshold()
isInteractive := hasInteraciveCla || isInteractiveGroup

onlyEmptyDirs := util.InArray(arguments, cli.DIR_CLA)
if !onlyEmptyDirs {
onlyEmptyDirs = util.InArray(arguments, cli.DIR_CLA_LONG)
}

hasVerboseCla := util.InArray(arguments, cli.VERBOSE_CLA)
if !hasVerboseCla {
hasVerboseCla = util.InArray(arguments, cli.VERBOSE_SHORT_CLA)
Expand Down Expand Up @@ -151,6 +156,14 @@ func deletePaths(paths []string, config models.Config, arguments []string) {
continue
}

if onlyEmptyDirs {
isDirEmpty := util.IsDirectoryEmpty(absolutePath)
if !isDirEmpty {
fmt.Println("cannot remove '", path, "': Directory not empty")
continue
}
}

isConfigHardDelete := config.ShouldHardDelete(absolutePath)
isConfigSoftDelete := config.ShouldSoftDelete(absolutePath)

Expand Down
9 changes: 9 additions & 0 deletions src/util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ func ListFiles(directory string) []string {

return fileNames
}

func IsDirectoryEmpty(directory string) bool {
files, err := os.ReadDir(directory)
if err != nil {
return true
}

return len(files) == 0
}

0 comments on commit 101a4d5

Please sign in to comment.