// Package cmd implemnts the rclone command // // It is in a sub package so it's internals can be re-used elsewhere package cmd // FIXME only attach the remote flags when using a remote??? // would probably mean bringing all the flags in to here? Or define some flagsets in fs... import ( "fmt" "log" "os" "path" "regexp" "runtime" "runtime/pprof" "time" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/ncw/rclone/fs" ) // Globals var ( // Flags cpuProfile = fs.StringP("cpuprofile", "", "", "Write cpu profile to file") memProfile = fs.StringP("memprofile", "", "", "Write memory profile to file") statsInterval = fs.DurationP("stats", "", time.Minute*1, "Interval between printing stats, e.g 500ms, 60s, 5m. (0 to disable)") dataRateUnit = fs.StringP("stats-unit", "", "bytes", "Show data rate in stats as either 'bits' or 'bytes'/s") version bool retries = fs.IntP("retries", "", 3, "Retry operations this many times if they fail") // Errors errorCommandNotFound = errors.New("command not found") errorUncategorized = errors.New("uncategorized error") errorNotEnoughArguments = errors.New("not enough arguments") errorTooManyArguents = errors.New("too many arguments") errorUsageError = errors.New("usage error") ) const ( exitCodeSuccess = iota exitCodeUsageError exitCodeUncategorizedError exitCodeDirNotFound exitCodeFileNotFound exitCodeRetryError exitCodeNoRetryError exitCodeFatalError ) // Root is the main rclone command var Root = &cobra.Command{ Use: "rclone", Short: "Sync files and directories to and from local and remote object stores - " + fs.Version, Long: ` Rclone is a command line program to sync files and directories to and from various cloud storage systems and using file transfer services, such as: * Amazon Drive * Amazon S3 * Backblaze B2 * Box * Dropbox * FTP * Google Cloud Storage * Google Drive * HTTP * Hubic * Microsoft Azure Blob Storage * Microsoft OneDrive * Openstack Swift / Rackspace cloud files / Memset Memstore * pCloud * QingStor * SFTP * Webdav / Owncloud / Nextcloud * Yandex Disk * The local filesystem Features * MD5/SHA1 hashes checked at all times for file integrity * Timestamps preserved on files * Partial syncs supported on a whole file basis * Copy mode to just copy new/changed files * Sync (one way) mode to make a directory identical * Check mode to check for file hash equality * Can sync to and from network, eg two different cloud accounts See the home page for installation, usage, documentation, changelog and configuration walkthroughs. * https://rclone.org/ `, PersistentPostRun: func(cmd *cobra.Command, args []string) { fs.Debugf("rclone", "Version %q finishing with parameters %q", fs.Version, os.Args) runAtExitFunctions() }, } // runRoot implements the main rclone command with no subcommands func runRoot(cmd *cobra.Command, args []string) { if version { ShowVersion() resolveExitCode(nil) } else { _ = Root.Usage() fmt.Fprintf(os.Stderr, "Command not found.\n") resolveExitCode(errorCommandNotFound) } } func init() { Root.Run = runRoot Root.Flags().BoolVarP(&version, "version", "V", false, "Print the version number") cobra.OnInitialize(initConfig) } // ShowVersion prints the version to stdout func ShowVersion() { fmt.Printf("rclone %s\n", fs.Version) fmt.Printf("- os/arch: %s/%s\n", runtime.GOOS, runtime.GOARCH) fmt.Printf("- go version: %s\n", runtime.Version()) } // newFsFile creates a dst Fs from a name but may point to a file. // // It returns a string with the file name if points to a file func newFsFile(remote string) (fs.Fs, string) { fsInfo, configName, fsPath, err := fs.ParseRemote(remote) if err != nil { fs.Stats.Error(err) log.Fatalf("Failed to create file system for %q: %v", remote, err) } f, err := fsInfo.NewFs(configName, fsPath) switch err { case fs.ErrorIsFile: return f, path.Base(fsPath) case nil: return f, "" default: fs.Stats.Error(err) log.Fatalf("Failed to create file system for %q: %v", remote, err) } return nil, "" } // newFsSrc creates a src Fs from a name // // It returns a string with the file name if limiting to one file // // This can point to a file func newFsSrc(remote string) (fs.Fs, string) { f, fileName := newFsFile(remote) if fileName != "" { if !fs.Config.Filter.InActive() { err := errors.Errorf("Can't limit to single files when using filters: %v", remote) fs.Stats.Error(err) log.Fatalf(err.Error()) } // Limit transfers to this file err := fs.Config.Filter.AddFile(fileName) if err != nil { fs.Stats.Error(err) log.Fatalf("Failed to limit to single file %q: %v", remote, err) } // Set --no-traverse as only one file fs.Config.NoTraverse = true } return f, fileName } // newFsDst creates a dst Fs from a name // // This must point to a directory func newFsDst(remote string) fs.Fs { f, err := fs.NewFs(remote) if err != nil { fs.Stats.Error(err) log.Fatalf("Failed to create file system for %q: %v", remote, err) } return f } // NewFsSrcDst creates a new src and dst fs from the arguments func NewFsSrcDst(args []string) (fs.Fs, fs.Fs) { fsrc, _ := newFsSrc(args[0]) fdst := newFsDst(args[1]) fs.CalculateModifyWindow(fdst, fsrc) return fsrc, fdst } // NewFsSrcDstFiles creates a new src and dst fs from the arguments // If src is a file then srcFileName and dstFileName will be non-empty func NewFsSrcDstFiles(args []string) (fsrc fs.Fs, srcFileName string, fdst fs.Fs, dstFileName string) { fsrc, srcFileName = newFsSrc(args[0]) // If copying a file... dstRemote := args[1] if srcFileName != "" { dstRemote, dstFileName = fs.RemoteSplit(dstRemote) if dstRemote == "" { dstRemote = "." } if dstFileName == "" { log.Fatalf("%q is a directory", args[1]) } } fdst = newFsDst(dstRemote) fs.CalculateModifyWindow(fdst, fsrc) return } // NewFsSrc creates a new src fs from the arguments func NewFsSrc(args []string) fs.Fs { fsrc, _ := newFsSrc(args[0]) fs.CalculateModifyWindow(fsrc) return fsrc } // NewFsDst creates a new dst fs from the arguments // // Dst fs-es can't point to single files func NewFsDst(args []string) fs.Fs { fdst := newFsDst(args[0]) fs.CalculateModifyWindow(fdst) return fdst } // NewFsDstFile creates a new dst fs with a destination file name from the arguments func NewFsDstFile(args []string) (fdst fs.Fs, dstFileName string) { dstRemote, dstFileName := fs.RemoteSplit(args[0]) if dstRemote == "" { dstRemote = "." } if dstFileName == "" { log.Fatalf("%q is a directory", args[0]) } fdst = newFsDst(dstRemote) fs.CalculateModifyWindow(fdst) return } // ShowStats returns true if the user added a `--stats` flag to the command line. // // This is called by Run to override the default value of the // showStats passed in. func ShowStats() bool { statsIntervalFlag := pflag.Lookup("stats") return statsIntervalFlag != nil && statsIntervalFlag.Changed } // Run the function with stats and retries if required func Run(Retry bool, showStats bool, cmd *cobra.Command, f func() error) { var err error var stopStats chan struct{} if !showStats && ShowStats() { showStats = true } if showStats { stopStats = StartStats() } for try := 1; try <= *retries; try++ { err = f() if !Retry || (err == nil && !fs.Stats.Errored()) { if try > 1 { fs.Errorf(nil, "Attempt %d/%d succeeded", try, *retries) } break } if fs.IsFatalError(err) { fs.Errorf(nil, "Fatal error received - not attempting retries") break } if fs.IsNoRetryError(err) { fs.Errorf(nil, "Can't retry this error - not attempting retries") break } if err != nil { fs.Errorf(nil, "Attempt %d/%d failed with %d errors and: %v", try, *retries, fs.Stats.GetErrors(), err) } else { fs.Errorf(nil, "Attempt %d/%d failed with %d errors", try, *retries, fs.Stats.GetErrors()) } if try < *retries { fs.Stats.ResetErrors() } } if showStats { close(stopStats) } if err != nil { log.Printf("Failed to %s: %v", cmd.Name(), err) resolveExitCode(err) } if showStats && (fs.Stats.Errored() || *statsInterval > 0) { fs.Stats.Log() } fs.Debugf(nil, "Go routines at exit %d\n", runtime.NumGoroutine()) if fs.Stats.Errored() { resolveExitCode(fs.Stats.GetLastError()) } } // CheckArgs checks there are enough arguments and prints a message if not func CheckArgs(MinArgs, MaxArgs int, cmd *cobra.Command, args []string) { if len(args) < MinArgs { _ = cmd.Usage() fmt.Fprintf(os.Stderr, "Command %s needs %d arguments mininum\n", cmd.Name(), MinArgs) // os.Exit(1) resolveExitCode(errorNotEnoughArguments) } else if len(args) > MaxArgs { _ = cmd.Usage() fmt.Fprintf(os.Stderr, "Command %s needs %d arguments maximum\n", cmd.Name(), MaxArgs) // os.Exit(1) resolveExitCode(errorTooManyArguents) } } // StartStats prints the stats every statsInterval // // It returns a channel which should be closed to stop the stats. func StartStats() chan struct{} { stopStats := make(chan struct{}) if *statsInterval > 0 { go func() { ticker := time.NewTicker(*statsInterval) for { select { case <-ticker.C: fs.Stats.Log() case <-stopStats: ticker.Stop() return } } }() } return stopStats } // initConfig is run by cobra after initialising the flags func initConfig() { // Start the logger fs.InitLogging() // Load the rest of the config now we have started the logger fs.LoadConfig() // Write the args for debug purposes fs.Debugf("rclone", "Version %q starting with parameters %q", fs.Version, os.Args) // Setup CPU profiling if desired if *cpuProfile != "" { fs.Infof(nil, "Creating CPU profile %q\n", *cpuProfile) f, err := os.Create(*cpuProfile) if err != nil { fs.Stats.Error(err) log.Fatal(err) } err = pprof.StartCPUProfile(f) if err != nil { fs.Stats.Error(err) log.Fatal(err) } AtExit(func() { pprof.StopCPUProfile() }) } // Setup memory profiling if desired if *memProfile != "" { AtExit(func() { fs.Infof(nil, "Saving Memory profile %q\n", *memProfile) f, err := os.Create(*memProfile) if err != nil { fs.Stats.Error(err) log.Fatal(err) } err = pprof.WriteHeapProfile(f) if err != nil { fs.Stats.Error(err) log.Fatal(err) } err = f.Close() if err != nil { fs.Stats.Error(err) log.Fatal(err) } }) } if m, _ := regexp.MatchString("^(bits|bytes)$", *dataRateUnit); m == false { fs.Errorf(nil, "Invalid unit passed to --stats-unit. Defaulting to bytes.") fs.Config.DataRateUnit = "bytes" } else { fs.Config.DataRateUnit = *dataRateUnit } } func resolveExitCode(err error) { if err == nil { os.Exit(exitCodeSuccess) } err = errors.Cause(err) switch { case err == fs.ErrorDirNotFound: os.Exit(exitCodeDirNotFound) case err == fs.ErrorObjectNotFound: os.Exit(exitCodeFileNotFound) case err == errorUncategorized: os.Exit(exitCodeUncategorizedError) case fs.ShouldRetry(err): os.Exit(exitCodeRetryError) case fs.IsNoRetryError(err): os.Exit(exitCodeNoRetryError) case fs.IsFatalError(err): os.Exit(exitCodeFatalError) default: os.Exit(exitCodeUsageError) } }