Skip to content

Commit

Permalink
add flag -U/--force-undo
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Jun 24, 2018
1 parent bccca80 commit 05c17c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
20 changes: 17 additions & 3 deletions brename.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,24 @@ type Options struct {
OverwriteMode int

Undo bool
ForceUndo bool
LastOpDetailFile string
}

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()
Expand Down Expand Up @@ -230,7 +241,7 @@ func getOptions(cmd *cobra.Command) *Options {

OverwriteMode: overwriteMode,

Undo: getFlagBool(cmd, "undo"),
Undo: false,
LastOpDetailFile: ".brename_detail.txt",
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 05c17c5

Please sign in to comment.