Skip to content

Commit

Permalink
Add -I flag threshold config option
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-newey committed Nov 2, 2024
1 parent 56b41fc commit 05a04ee
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A wrapper for the "rm" command with soft-deletes, config-based deletion, debug i
## "GNU Like" command line arguments

- `-i` Interactivly prompt before each deletion request
- `-I` Prompt if deleting more three files
- `-I` Prompt if deleting more than the interactive threshold of files (default 3)
- `--help` Display help information (without deletion)
- `--version` Display version information (without deletion)

Expand Down Expand Up @@ -117,4 +117,9 @@ soft:
# accidental deletion through 2rm
protected:
- ".ssh/"
# when using the -I flag without any arguments, the user will be prompted
# for confirmation before deleting each file if the number of files is
# greater or equal to this threshold
# default is 3 files/directories
interactive: 10
```
3 changes: 2 additions & 1 deletion src/cli/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Mark FILE(s) for deletion.
"GNU Like" OPTION(s):
-i Prompt before every deletion request
-I Prompt once before deleting more than three files
-I Prompt once before deleting more than the interactive
threshold (default 3)
-f, --force Bypass protections
Expand Down
1 change: 1 addition & 0 deletions src/config/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestParsingConfig(t *testing.T) {
Protected: []string{
".ssh/",
},
Interactive: 10,
}

assertConfig(t, "valid.yml", expectedConfig)
Expand Down
16 changes: 16 additions & 0 deletions src/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type Config struct {
// any file paths that match these patterns will be protected from deletion
// protected files cannot be deleted without the --bypass-protected flag
Protected []string

// when using the -I flag without any arguments, the user will be prompted
// for confirmation before deleting each file if the number of files is
// greater or equal to this threshold
// default is 3 files/directories
Interactive int
}

func (config Config) ShouldHardDelete(path string) bool {
Expand Down Expand Up @@ -77,6 +83,16 @@ func (config Config) SoftDeleteDir() string {
return "/tmp/2rm/"
}

func (config Config) InteractiveThreshold() int {
const DEFAULT_INTERACTIVE_THRESHOLD = 3

if config.Interactive == 0 {
return DEFAULT_INTERACTIVE_THRESHOLD
}

return config.Interactive
}

func matchesPattern(pattern string, path string) bool {
// we put a wildcard at the start so that we don't have to match full
// paths
Expand Down
3 changes: 1 addition & 2 deletions src/patches/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

const TRASH_DIR_PERMISSIONS = 0755
const INTERACTIVE_THRESHOLD = 3

func RmPatch(arguments []string, config models.Config) {
silent := util.InArray(arguments, cli.SILENT_CLA)
Expand Down Expand Up @@ -117,7 +116,7 @@ func deletePaths(paths []string, config models.Config, arguments []string) {

hasInteraciveCla := util.InArray(arguments, cli.INTERACTIVE_CLA)
hasGroupInteractiveCla := util.InArray(arguments, cli.INTERACTIVE_GROUP_CLA)
isInteractiveGroup := hasGroupInteractiveCla && len(paths) >= INTERACTIVE_THRESHOLD
isInteractiveGroup := hasGroupInteractiveCla && len(paths) >= config.InteractiveThreshold()
isInteractive := hasInteraciveCla || isInteractiveGroup

for _, path := range paths {
Expand Down
1 change: 1 addition & 0 deletions tests/assets/configs/valid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ soft:
- "*.bak"
protected:
- ".ssh/"
interactive: 10

0 comments on commit 05a04ee

Please sign in to comment.