mirror of
https://github.com/itchyny/mmv.git
synced 2025-12-26 22:24:58 +08:00
add --dry-run option
This commit is contained in:
parent
407f4d2b0c
commit
8cd5297856
3 changed files with 13 additions and 5 deletions
|
|
@ -48,7 +48,9 @@ Options:
|
|||
fs.PrintDefaults()
|
||||
}
|
||||
var showVersion bool
|
||||
var dryRun bool
|
||||
fs.BoolVar(&showVersion, "version", false, "print version")
|
||||
fs.BoolVar(&dryRun, "dry-run", false, "only show the operation that would have been performed")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
if err == flag.ErrHelp {
|
||||
return exitCodeOK
|
||||
|
|
@ -64,14 +66,14 @@ Options:
|
|||
fmt.Fprintf(os.Stderr, "usage: %s file ...\n", name)
|
||||
return exitCodeErr
|
||||
}
|
||||
if err := rename(args); err != nil {
|
||||
if err := rename(args, dryRun); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %s\n", name, err)
|
||||
return exitCodeErr
|
||||
}
|
||||
return exitCodeOK
|
||||
}
|
||||
|
||||
func rename(args []string) error {
|
||||
func rename(args []string, dryRun bool) error {
|
||||
xs := make(map[string]bool, len(args))
|
||||
for _, src := range args {
|
||||
if xs[src] {
|
||||
|
|
@ -122,5 +124,5 @@ func rename(args []string) error {
|
|||
for i, src := range args {
|
||||
files[src] = got[i]
|
||||
}
|
||||
return mmv.Rename(files)
|
||||
return mmv.Rename(files, dryRun)
|
||||
}
|
||||
|
|
|
|||
8
mmv.go
8
mmv.go
|
|
@ -8,11 +8,17 @@ import (
|
|||
)
|
||||
|
||||
// Rename multiple files.
|
||||
func Rename(files map[string]string) error {
|
||||
func Rename(files map[string]string, dryRun bool) error {
|
||||
rs, err := buildRenames(files)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dryRun {
|
||||
for _, r := range rs {
|
||||
fmt.Printf("%s => %s\n", r.src, r.dst)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for i, r := range rs {
|
||||
if err := doRename(r.src, r.dst); err != nil {
|
||||
// undo on error not to leave the temporary files
|
||||
|
|
|
|||
|
|
@ -307,7 +307,7 @@ func TestRename(t *testing.T) {
|
|||
require.NoError(t, setupFiles(tc.contents))
|
||||
rs, _ := buildRenames(clone(tc.files))
|
||||
assert.Equal(t, tc.count, len(rs))
|
||||
err = Rename(tc.files)
|
||||
err = Rename(tc.files, false)
|
||||
if tc.err == "" {
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue