From 05a04ee7e8cb4dfeba30e963278d1fa56a37a725 Mon Sep 17 00:00:00 2001 From: hudson-newey Date: Sat, 2 Nov 2024 22:58:03 +1000 Subject: [PATCH] Add -I flag threshold config option --- README.md | 7 ++++++- src/cli/docs.go | 3 ++- src/config/parse_test.go | 1 + src/models/config.go | 16 ++++++++++++++++ src/patches/rm.go | 3 +-- tests/assets/configs/valid.yml | 1 + 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ebf9284..6b05ce9 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 ``` diff --git a/src/cli/docs.go b/src/cli/docs.go index 0ded80d..42e19c9 100644 --- a/src/cli/docs.go +++ b/src/cli/docs.go @@ -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 diff --git a/src/config/parse_test.go b/src/config/parse_test.go index fca42a1..86d4312 100644 --- a/src/config/parse_test.go +++ b/src/config/parse_test.go @@ -41,6 +41,7 @@ func TestParsingConfig(t *testing.T) { Protected: []string{ ".ssh/", }, + Interactive: 10, } assertConfig(t, "valid.yml", expectedConfig) diff --git a/src/models/config.go b/src/models/config.go index 3dbc930..b9d717b 100644 --- a/src/models/config.go +++ b/src/models/config.go @@ -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 { @@ -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 diff --git a/src/patches/rm.go b/src/patches/rm.go index 9ab6ce5..512caa4 100644 --- a/src/patches/rm.go +++ b/src/patches/rm.go @@ -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) @@ -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 { diff --git a/tests/assets/configs/valid.yml b/tests/assets/configs/valid.yml index 11127e9..cb5631b 100644 --- a/tests/assets/configs/valid.yml +++ b/tests/assets/configs/valid.yml @@ -11,3 +11,4 @@ soft: - "*.bak" protected: - ".ssh/" +interactive: 10