diff --git a/fs/config.go b/fs/config.go index 2f01046ce..2f4902cfb 100644 --- a/fs/config.go +++ b/fs/config.go @@ -85,7 +85,6 @@ var ( lowLevelRetries = pflag.IntP("low-level-retries", "", 10, "Number of low level retries to do.") updateOlder = pflag.BoolP("update", "u", false, "Skip files that are newer on the destination.") noGzip = pflag.BoolP("no-gzip-encoding", "", false, "Don't set Accept-Encoding: gzip.") - dedupeMode = pflag.StringP("dedupe-mode", "", "interactive", "Dedupe mode interactive|skip|first|newest|oldest|rename.") maxDepth = pflag.IntP("max-depth", "", -1, "If set limits the recursion depth to this.") ignoreSize = pflag.BoolP("ignore-size", "", false, "Ignore size when skipping use mod-time or checksum.") noTraverse = pflag.BoolP("no-traverse", "", false, "Don't traverse destination file system on copy.") @@ -237,7 +236,6 @@ type ConfigInfo struct { LowLevelRetries int UpdateOlder bool // Skip files that are newer on the destination NoGzip bool // Disable compression - DedupeMode DeduplicateMode MaxDepth int IgnoreSize bool NoTraverse bool @@ -355,23 +353,6 @@ func LoadConfig() { Config.DeleteDuring = *deleteDuring Config.DeleteAfter = *deleteAfter - switch strings.ToLower(*dedupeMode) { - case "interactive": - Config.DedupeMode = DeduplicateInteractive - case "skip": - Config.DedupeMode = DeduplicateSkip - case "first": - Config.DedupeMode = DeduplicateFirst - case "newest": - Config.DedupeMode = DeduplicateNewest - case "oldest": - Config.DedupeMode = DeduplicateOldest - case "rename": - Config.DedupeMode = DeduplicateRename - default: - log.Fatalf(`Unknown mode for --dedupe-mode %q.`, *dedupeMode) - } - switch { case *deleteBefore && (*deleteDuring || *deleteAfter), *deleteDuring && *deleteAfter: diff --git a/fs/operations.go b/fs/operations.go index 48c149982..5740ac37d 100644 --- a/fs/operations.go +++ b/fs/operations.go @@ -14,6 +14,7 @@ import ( "sync/atomic" "time" + "github.com/ogier/pflag" "github.com/pkg/errors" "golang.org/x/text/unicode/norm" @@ -871,8 +872,8 @@ const ( DeduplicateRename // rename the objects ) -func (mode DeduplicateMode) String() string { - switch mode { +func (x DeduplicateMode) String() string { + switch x { case DeduplicateInteractive: return "interactive" case DeduplicateSkip: @@ -889,6 +890,35 @@ func (mode DeduplicateMode) String() string { return "unknown" } +// Set a DeduplicateMode from a string +func (x *DeduplicateMode) Set(s string) error { + switch strings.ToLower(s) { + case "interactive": + *x = DeduplicateInteractive + case "skip": + *x = DeduplicateSkip + case "first": + *x = DeduplicateFirst + case "newest": + *x = DeduplicateNewest + case "oldest": + *x = DeduplicateOldest + case "rename": + *x = DeduplicateRename + default: + return errors.Errorf("Unknown mode for dedupe %q.", s) + } + return nil +} + +// Type of the value +func (x *DeduplicateMode) Type() string { + return "string" +} + +// Check it satisfies the interface +var _ pflag.Value = (*DeduplicateMode)(nil) + // Deduplicate interactively finds duplicate files and offers to // delete all but one or rename them to be different. Only useful with // Google Drive which can have duplicate file names. diff --git a/rclone.go b/rclone.go index a706ebccc..3592a0035 100644 --- a/rclone.go +++ b/rclone.go @@ -4,7 +4,6 @@ package main // FIXME only attach the remote flags when using a remote??? -// FIXME could do the same for dedupe // would probably mean bringing all the flags in to here? Or define some flagsets in fs... import ( @@ -32,6 +31,7 @@ var ( version bool logFile = pflag.StringP("log-file", "", "", "Log everything to this file") retries = pflag.IntP("retries", "", 3, "Retry operations this many times if they fail") + dedupeMode = fs.DeduplicateInteractive ) var rootCmd = &cobra.Command{ @@ -82,6 +82,7 @@ func init() { lslCmd, md5sumCmd, sha1sumCmd, sizeCmd, mkdirCmd, rmdirCmd, purgeCmd, deleteCmd, checkCmd, dedupeCmd, configCmd, authorizeCmd, cleanupCmd, versionCmd) + dedupeCmd.Flags().VarP(&dedupeMode, "dedupe-mode", "", "Dedupe mode interactive|skip|first|newest|oldest|rename.") cobra.OnInitialize(initConfig) } @@ -446,7 +447,7 @@ Google Drive which can have duplicate file names.`, checkArgs(1, 1, cmd, args) fdst := NewFsSrc(args[1]) run(false, cmd, func() error { - return fs.Deduplicate(fdst, fs.Config.DedupeMode) + return fs.Deduplicate(fdst, dedupeMode) }) }, }