From 4f6ab067fefd8b2816ce4a59a25b4f37ed51e452 Mon Sep 17 00:00:00 2001 From: hudson-newey Date: Sat, 2 Nov 2024 22:26:36 +1000 Subject: [PATCH] Add --help and --version flags --- README.md | 14 +++++++++++--- src/cli/args.go | 2 ++ src/cli/docs.go | 39 +++++++++++++++++++++++++++++++++++++++ src/patches/rm.go | 25 ++++++++----------------- 4 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 src/cli/docs.go diff --git a/README.md b/README.md index c709844..0d7bf2f 100644 --- a/README.md +++ b/README.md @@ -4,23 +4,31 @@ A wrapper for the "rm" command with soft-deletes, config-based deletion, debug information, and saner defaults +## "GNU Like" command line arguments + +- `-i` Interactivly prompt before each deletion request +- `--help` Display help information (without deletion) +- `--version` Display version information (without deletion) + ## Additional command line arguments - `--overwrite` Overwrite the disk location location with zeros - `--hard` Do not soft-delete file -- `--soft` Soft delete a file. A backup will be stored in `/tmp/2rm` +- `--soft` Soft delete a file and store a backup (default `/tmp/2rm`) - `--silent` Do not print out additional information priduced by 2rm. This is useful for scripting situations - `--dry-run` Perform a dry run and show all the files that would be deleted - `--bypass-protected` Using this flag will allow you to delete a file protected by the 2rm config - `--notify` Send a system notification once deletion is complete +- `--force` Bypass protections ## Unsupported command line arguments - `-r`, `-R`, `--recursive` Recursively delete a directory of files -- `-d` Only delete empty directories -- `-v` Emit additional verbose information +- `-d`, `--dir` Only delete empty directories +- `-v`, `--verbose` Emit additional verbose information - `--version` Show version information - `--help` Show help information +- `-I` Prompt if deleting more than three files - `--interactive[=WHEN]` Interactive with a custom threshold - `--one-file-system` Do not allow cross-file-system deletes - `-f`, `--force` (partially implemented) diff --git a/src/cli/args.go b/src/cli/args.go index c709aae..35cb905 100644 --- a/src/cli/args.go +++ b/src/cli/args.go @@ -11,3 +11,5 @@ const NOTIFICATION_CLA = "--notify" // gnu rm CLI arguments const INTERACTIVE_CLA = "-i" +const HELP_CLA = "--help" +const VERSION_CLA = "--version" diff --git a/src/cli/docs.go b/src/cli/docs.go new file mode 100644 index 0000000..a13cd87 --- /dev/null +++ b/src/cli/docs.go @@ -0,0 +1,39 @@ +package cli + +import "fmt" + +const VERSION = "2rm 0.1.0" + +const HELP_DOCS = ` +Usage: rm [OPTION]... [FILE]... +Mark FILE(s) for deletion. + +"GNU Like" OPTION(s): +-i Prompt before every deletion request +--force Bypass 2rm protections + +2rm OPTION(s) Flags: +--overwrite Overwrite the disk location location with zeros +--hard Do not soft-delete FILE(s) +--soft Soft delete a file and a store backup (default /tmp/2rm) +--silent Do not print out additional information priduced by 2rm. This is useful for scripting situations +--dry-run Perform a dry run and show all the files that would be deleted +--bypass-protected Using this flag will allow you to delete a file protected by the 2rm config +--notify Send a system notification once deletion is complete + +By default, 2rm will soft-delete a file and store a backup (default /tmp/2rm) + +You can create a 2rm config file under ~/.local/share/2rm/config.yml that will +allow you to protect files/directories, set a custom backup location, and +configure directories that should always be hard-deleted. + +Use "man 2rm" for more information. +` + +func PrintVersion() { + fmt.Println(VERSION) +} + +func PrintHelp() { + fmt.Print(HELP_DOCS) +} diff --git a/src/patches/rm.go b/src/patches/rm.go index 2747185..69889bc 100644 --- a/src/patches/rm.go +++ b/src/patches/rm.go @@ -25,13 +25,18 @@ func RmPatch(arguments []string, config models.Config) { overwrite := util.InArray(arguments, cli.OVERWRITE_CLA) shouldNotify := util.InArray(arguments, cli.NOTIFICATION_CLA) + requestingHelp := util.InArray(arguments, cli.HELP_CLA) + requestingVersion := util.InArray(arguments, cli.VERSION_CLA) + actionedArgs := removeUnNeededArguments( removeDangerousArguments(arguments), ) - if shouldPassthrough(actionedArgs) { - command := "rm " + strings.Join(actionedArgs, " ") - util.Execute(command) + if requestingHelp { + cli.PrintHelp() + return + } else if requestingVersion { + cli.PrintVersion() return } @@ -89,20 +94,6 @@ func RmPatch(arguments []string, config models.Config) { } } -// sometimes we want to pass through the arguments to the original rm command -// e.g. when executing --help or --version -func shouldPassthrough(arguments []string) bool { - passthroughArguments := []string{"--help", "--version"} - - for _, arg := range arguments { - if util.InArray(passthroughArguments, arg) { - return true - } - } - - return false -} - func removeUnNeededArguments(arguments []string) []string { returnedArguments := []string{} unNeededArguments := []string{