From 05c17c58e82e8d2f340e81f6e3ed30c05b565824 Mon Sep 17 00:00:00 2001 From: Wei Shen Date: Sun, 24 Jun 2018 20:39:40 +0800 Subject: [PATCH] add flag -U/--force-undo --- README.md | 7 ++++++- brename.go | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 535a6a9..8f9394e 100755 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ Examples: Flags: -d, --dry-run print rename operations but do not run -F, --exclude-filters strings exclude file filter(s) (regular expression, case ignored). multiple values supported, e.g., -F ".html" -F ".htm", but ATTENTION: comma in filter is treated as separater of multiple filters + -U, --force-undo continue undo even when some operation failed -h, --help help for brename -i, --ignore-case ignore case -e, --ignore-ext ignore file extension. i.e., replacement does not change file extension @@ -427,10 +428,14 @@ Take a directory for example: [WARN] checking: [ overwriting newly renamed path ] 'brename_linux_386.tar.gz' -> 'brename_unix-like_386.tar.gz' (will NOT be overwrited) [INFO] 1 path(s) to be renamed -1. **Undo** the LAST successful operation, yes it's COOL! (`-u/--undo`) +1. **Undo** the LAST successful operation, yes it's COOL! (`-u/--undo`, `-U/--force-undo`) brename -u + # if you make more changes after renaming, you can + brename -U + + ## Real-world examples diff --git a/brename.go b/brename.go index 1388efe..15d7337 100644 --- a/brename.go +++ b/brename.go @@ -84,6 +84,7 @@ type Options struct { OverwriteMode int Undo bool + ForceUndo bool LastOpDetailFile string } @@ -91,6 +92,16 @@ var reNR = regexp.MustCompile(`\{(NR|nr)\}`) var reKV = regexp.MustCompile(`\{(KV|kv)\}`) func getOptions(cmd *cobra.Command) *Options { + undo := getFlagBool(cmd, "undo") + forceUndo := getFlagBool(cmd, "force-undo") + if undo || forceUndo { + return &Options{ + Undo: true, // set it true even only force-undo given + ForceUndo: forceUndo, + LastOpDetailFile: ".brename_detail.txt", + } + } + version := getFlagBool(cmd, "version") if version { checkVersion() @@ -230,7 +241,7 @@ func getOptions(cmd *cobra.Command) *Options { OverwriteMode: overwriteMode, - Undo: getFlagBool(cmd, "undo"), + Undo: false, LastOpDetailFile: ".brename_detail.txt", } } @@ -274,6 +285,7 @@ func init() { RootCmd.Flags().IntP("overwrite-mode", "o", 0, "overwrite mode (0 for reporting error, 1 for overwrite, 2 for not renaming) (default 0)") RootCmd.Flags().BoolP("undo", "u", false, "undo the LAST successful operation") + RootCmd.Flags().BoolP("force-undo", "U", false, "continue undo even when some operation failed") RootCmd.Example = ` 1. dry run and showing potential dangerous operations brename -p "abc" -d @@ -500,8 +512,10 @@ Special replacement symbols: err = os.Rename(op.target, op.source) if err != nil { log.Errorf(`fail to rename: '%s' -> '%s': %s`, op.source, op.target, err) - log.Infof("%d path(s) renamed", n) - os.Exit(1) + if !opt.ForceUndo { + log.Infof("%d path(s) renamed", n) + os.Exit(1) + } } n++ log.Infof("rename back: '%s' -> '%s'", op.target, op.source)