github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/config/configflags/configflags.go (about)

     1  // Package configflags defines the flags used by rclone.  It is
     2  // decoupled into a separate package so it can be replaced.
     3  package configflags
     4  
     5  // Options set by command line flags
     6  import (
     7  	"log"
     8  	"net"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/rclone/rclone/fs"
    13  	"github.com/rclone/rclone/fs/config"
    14  	"github.com/rclone/rclone/fs/config/flags"
    15  	fsLog "github.com/rclone/rclone/fs/log"
    16  	"github.com/rclone/rclone/fs/rc"
    17  	"github.com/sirupsen/logrus"
    18  	"github.com/spf13/pflag"
    19  )
    20  
    21  var (
    22  	// these will get interpreted into fs.Config via SetFlags() below
    23  	verbose         int
    24  	quiet           bool
    25  	dumpHeaders     bool
    26  	dumpBodies      bool
    27  	deleteBefore    bool
    28  	deleteDuring    bool
    29  	deleteAfter     bool
    30  	bindAddr        string
    31  	disableFeatures string
    32  )
    33  
    34  // AddFlags adds the non filing system specific flags to the command
    35  func AddFlags(flagSet *pflag.FlagSet) {
    36  	rc.AddOption("main", fs.Config)
    37  	// NB defaults which aren't the zero for the type should be set in fs/config.go NewConfig
    38  	flags.CountVarP(flagSet, &verbose, "verbose", "v", "Print lots more stuff (repeat for more)")
    39  	flags.BoolVarP(flagSet, &quiet, "quiet", "q", false, "Print as little stuff as possible")
    40  	flags.DurationVarP(flagSet, &fs.Config.ModifyWindow, "modify-window", "", fs.Config.ModifyWindow, "Max time diff to be considered the same")
    41  	flags.IntVarP(flagSet, &fs.Config.Checkers, "checkers", "", fs.Config.Checkers, "Number of checkers to run in parallel.")
    42  	flags.IntVarP(flagSet, &fs.Config.Transfers, "transfers", "", fs.Config.Transfers, "Number of file transfers to run in parallel.")
    43  	flags.StringVarP(flagSet, &config.ConfigPath, "config", "", config.ConfigPath, "Config file.")
    44  	flags.StringVarP(flagSet, &config.CacheDir, "cache-dir", "", config.CacheDir, "Directory rclone will use for caching.")
    45  	flags.BoolVarP(flagSet, &fs.Config.CheckSum, "checksum", "c", fs.Config.CheckSum, "Skip based on checksum (if available) & size, not mod-time & size")
    46  	flags.BoolVarP(flagSet, &fs.Config.SizeOnly, "size-only", "", fs.Config.SizeOnly, "Skip based on size only, not mod-time or checksum")
    47  	flags.BoolVarP(flagSet, &fs.Config.IgnoreTimes, "ignore-times", "I", fs.Config.IgnoreTimes, "Don't skip files that match size and time - transfer all files")
    48  	flags.BoolVarP(flagSet, &fs.Config.IgnoreExisting, "ignore-existing", "", fs.Config.IgnoreExisting, "Skip all files that exist on destination")
    49  	flags.BoolVarP(flagSet, &fs.Config.IgnoreErrors, "ignore-errors", "", fs.Config.IgnoreErrors, "delete even if there are I/O errors")
    50  	flags.BoolVarP(flagSet, &fs.Config.DryRun, "dry-run", "n", fs.Config.DryRun, "Do a trial run with no permanent changes")
    51  	flags.DurationVarP(flagSet, &fs.Config.ConnectTimeout, "contimeout", "", fs.Config.ConnectTimeout, "Connect timeout")
    52  	flags.DurationVarP(flagSet, &fs.Config.Timeout, "timeout", "", fs.Config.Timeout, "IO idle timeout")
    53  	flags.DurationVarP(flagSet, &fs.Config.ExpectContinueTimeout, "expect-continue-timeout", "", fs.Config.ExpectContinueTimeout, "Timeout when using expect / 100-continue in HTTP")
    54  	flags.BoolVarP(flagSet, &dumpHeaders, "dump-headers", "", false, "Dump HTTP headers - may contain sensitive info")
    55  	flags.BoolVarP(flagSet, &dumpBodies, "dump-bodies", "", false, "Dump HTTP headers and bodies - may contain sensitive info")
    56  	flags.BoolVarP(flagSet, &fs.Config.InsecureSkipVerify, "no-check-certificate", "", fs.Config.InsecureSkipVerify, "Do not verify the server SSL certificate. Insecure.")
    57  	flags.BoolVarP(flagSet, &fs.Config.AskPassword, "ask-password", "", fs.Config.AskPassword, "Allow prompt for password for encrypted configuration.")
    58  	flags.FVarP(flagSet, &fs.Config.PasswordCommand, "password-command", "", "Command for supplying password for encrypted configuration.")
    59  	flags.BoolVarP(flagSet, &deleteBefore, "delete-before", "", false, "When synchronizing, delete files on destination before transferring")
    60  	flags.BoolVarP(flagSet, &deleteDuring, "delete-during", "", false, "When synchronizing, delete files during transfer")
    61  	flags.BoolVarP(flagSet, &deleteAfter, "delete-after", "", false, "When synchronizing, delete files on destination after transferring (default)")
    62  	flags.Int64VarP(flagSet, &fs.Config.MaxDelete, "max-delete", "", -1, "When synchronizing, limit the number of deletes")
    63  	flags.BoolVarP(flagSet, &fs.Config.TrackRenames, "track-renames", "", fs.Config.TrackRenames, "When synchronizing, track file renames and do a server side move if possible")
    64  	flags.IntVarP(flagSet, &fs.Config.LowLevelRetries, "low-level-retries", "", fs.Config.LowLevelRetries, "Number of low level retries to do.")
    65  	flags.BoolVarP(flagSet, &fs.Config.UpdateOlder, "update", "u", fs.Config.UpdateOlder, "Skip files that are newer on the destination.")
    66  	flags.BoolVarP(flagSet, &fs.Config.UseServerModTime, "use-server-modtime", "", fs.Config.UseServerModTime, "Use server modified time instead of object metadata")
    67  	flags.BoolVarP(flagSet, &fs.Config.NoGzip, "no-gzip-encoding", "", fs.Config.NoGzip, "Don't set Accept-Encoding: gzip.")
    68  	flags.IntVarP(flagSet, &fs.Config.MaxDepth, "max-depth", "", fs.Config.MaxDepth, "If set limits the recursion depth to this.")
    69  	flags.BoolVarP(flagSet, &fs.Config.IgnoreSize, "ignore-size", "", false, "Ignore size when skipping use mod-time or checksum.")
    70  	flags.BoolVarP(flagSet, &fs.Config.IgnoreChecksum, "ignore-checksum", "", fs.Config.IgnoreChecksum, "Skip post copy check of checksums.")
    71  	flags.BoolVarP(flagSet, &fs.Config.IgnoreCaseSync, "ignore-case-sync", "", fs.Config.IgnoreCaseSync, "Ignore case when synchronizing")
    72  	flags.BoolVarP(flagSet, &fs.Config.NoTraverse, "no-traverse", "", fs.Config.NoTraverse, "Don't traverse destination file system on copy.")
    73  	flags.BoolVarP(flagSet, &fs.Config.NoCheckDest, "no-check-dest", "", fs.Config.NoCheckDest, "Don't check the destination, copy regardless.")
    74  	flags.BoolVarP(flagSet, &fs.Config.NoUpdateModTime, "no-update-modtime", "", fs.Config.NoUpdateModTime, "Don't update destination mod-time if files identical.")
    75  	flags.StringVarP(flagSet, &fs.Config.CompareDest, "compare-dest", "", fs.Config.CompareDest, "Include additional server-side path during comparison.")
    76  	flags.StringVarP(flagSet, &fs.Config.CopyDest, "copy-dest", "", fs.Config.CopyDest, "Implies --compare-dest but also copies files from path into destination.")
    77  	flags.StringVarP(flagSet, &fs.Config.BackupDir, "backup-dir", "", fs.Config.BackupDir, "Make backups into hierarchy based in DIR.")
    78  	flags.StringVarP(flagSet, &fs.Config.Suffix, "suffix", "", fs.Config.Suffix, "Suffix to add to changed files.")
    79  	flags.BoolVarP(flagSet, &fs.Config.SuffixKeepExtension, "suffix-keep-extension", "", fs.Config.SuffixKeepExtension, "Preserve the extension when using --suffix.")
    80  	flags.BoolVarP(flagSet, &fs.Config.UseListR, "fast-list", "", fs.Config.UseListR, "Use recursive list if available. Uses more memory but fewer transactions.")
    81  	flags.Float64VarP(flagSet, &fs.Config.TPSLimit, "tpslimit", "", fs.Config.TPSLimit, "Limit HTTP transactions per second to this.")
    82  	flags.IntVarP(flagSet, &fs.Config.TPSLimitBurst, "tpslimit-burst", "", fs.Config.TPSLimitBurst, "Max burst of transactions for --tpslimit.")
    83  	flags.StringVarP(flagSet, &bindAddr, "bind", "", "", "Local address to bind to for outgoing connections, IPv4, IPv6 or name.")
    84  	flags.StringVarP(flagSet, &disableFeatures, "disable", "", "", "Disable a comma separated list of features.  Use help to see a list.")
    85  	flags.StringVarP(flagSet, &fs.Config.UserAgent, "user-agent", "", fs.Config.UserAgent, "Set the user-agent to a specified string. The default is rclone/ version")
    86  	flags.BoolVarP(flagSet, &fs.Config.Immutable, "immutable", "", fs.Config.Immutable, "Do not modify files. Fail if existing files have been modified.")
    87  	flags.BoolVarP(flagSet, &fs.Config.AutoConfirm, "auto-confirm", "", fs.Config.AutoConfirm, "If enabled, do not request console confirmation.")
    88  	flags.IntVarP(flagSet, &fs.Config.StatsFileNameLength, "stats-file-name-length", "", fs.Config.StatsFileNameLength, "Max file name length in stats. 0 for no limit")
    89  	flags.FVarP(flagSet, &fs.Config.LogLevel, "log-level", "", "Log level DEBUG|INFO|NOTICE|ERROR")
    90  	flags.FVarP(flagSet, &fs.Config.StatsLogLevel, "stats-log-level", "", "Log level to show --stats output DEBUG|INFO|NOTICE|ERROR")
    91  	flags.FVarP(flagSet, &fs.Config.BwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix b|k|M|G or a full timetable.")
    92  	flags.FVarP(flagSet, &fs.Config.BufferSize, "buffer-size", "", "In memory buffer size when reading files for each --transfer.")
    93  	flags.FVarP(flagSet, &fs.Config.StreamingUploadCutoff, "streaming-upload-cutoff", "", "Cutoff for switching to chunked upload if file size is unknown. Upload starts after reaching cutoff or when file ends.")
    94  	flags.FVarP(flagSet, &fs.Config.Dump, "dump", "", "List of items to dump from: "+fs.DumpFlagsList)
    95  	flags.FVarP(flagSet, &fs.Config.MaxTransfer, "max-transfer", "", "Maximum size of data to transfer.")
    96  	flags.DurationVarP(flagSet, &fs.Config.MaxDuration, "max-duration", "", 0, "Maximum duration rclone will transfer data for.")
    97  	flags.IntVarP(flagSet, &fs.Config.MaxBacklog, "max-backlog", "", fs.Config.MaxBacklog, "Maximum number of objects in sync or check backlog.")
    98  	flags.IntVarP(flagSet, &fs.Config.MaxStatsGroups, "max-stats-groups", "", fs.Config.MaxStatsGroups, "Maximum number of stats groups to keep in memory. On max oldest is discarded.")
    99  	flags.BoolVarP(flagSet, &fs.Config.StatsOneLine, "stats-one-line", "", fs.Config.StatsOneLine, "Make the stats fit on one line.")
   100  	flags.BoolVarP(flagSet, &fs.Config.StatsOneLineDate, "stats-one-line-date", "", fs.Config.StatsOneLineDate, "Enables --stats-one-line and add current date/time prefix.")
   101  	flags.StringVarP(flagSet, &fs.Config.StatsOneLineDateFormat, "stats-one-line-date-format", "", fs.Config.StatsOneLineDateFormat, "Enables --stats-one-line-date and uses custom formatted date. Enclose date string in double quotes (\"). See https://golang.org/pkg/time/#Time.Format")
   102  	flags.BoolVarP(flagSet, &fs.Config.Progress, "progress", "P", fs.Config.Progress, "Show progress during transfer.")
   103  	flags.BoolVarP(flagSet, &fs.Config.Cookie, "use-cookies", "", fs.Config.Cookie, "Enable session cookiejar.")
   104  	flags.BoolVarP(flagSet, &fs.Config.UseMmap, "use-mmap", "", fs.Config.UseMmap, "Use mmap allocator (see docs).")
   105  	flags.StringVarP(flagSet, &fs.Config.CaCert, "ca-cert", "", fs.Config.CaCert, "CA certificate used to verify servers")
   106  	flags.StringVarP(flagSet, &fs.Config.ClientCert, "client-cert", "", fs.Config.ClientCert, "Client SSL certificate (PEM) for mutual TLS auth")
   107  	flags.StringVarP(flagSet, &fs.Config.ClientKey, "client-key", "", fs.Config.ClientKey, "Client SSL private key (PEM) for mutual TLS auth")
   108  	flags.FVarP(flagSet, &fs.Config.MultiThreadCutoff, "multi-thread-cutoff", "", "Use multi-thread downloads for files above this size.")
   109  	flags.IntVarP(flagSet, &fs.Config.MultiThreadStreams, "multi-thread-streams", "", fs.Config.MultiThreadStreams, "Max number of streams to use for multi-thread downloads.")
   110  	flags.BoolVarP(flagSet, &fs.Config.UseJSONLog, "use-json-log", "", fs.Config.UseJSONLog, "Use json log format.")
   111  	flags.StringVarP(flagSet, &fs.Config.OrderBy, "order-by", "", fs.Config.OrderBy, "Instructions on how to order the transfers, eg 'size,descending'")
   112  }
   113  
   114  // SetFlags converts any flags into config which weren't straight forward
   115  func SetFlags() {
   116  	if verbose >= 2 {
   117  		fs.Config.LogLevel = fs.LogLevelDebug
   118  	} else if verbose >= 1 {
   119  		fs.Config.LogLevel = fs.LogLevelInfo
   120  	}
   121  	if quiet {
   122  		if verbose > 0 {
   123  			log.Fatalf("Can't set -v and -q")
   124  		}
   125  		fs.Config.LogLevel = fs.LogLevelError
   126  	}
   127  	logLevelFlag := pflag.Lookup("log-level")
   128  	if logLevelFlag != nil && logLevelFlag.Changed {
   129  		if verbose > 0 {
   130  			log.Fatalf("Can't set -v and --log-level")
   131  		}
   132  		if quiet {
   133  			log.Fatalf("Can't set -q and --log-level")
   134  		}
   135  	}
   136  	if fs.Config.UseJSONLog {
   137  		logrus.AddHook(fsLog.NewCallerHook())
   138  		logrus.SetFormatter(&logrus.JSONFormatter{
   139  			TimestampFormat: "2006-01-02T15:04:05.999999-07:00",
   140  		})
   141  		logrus.SetLevel(logrus.DebugLevel)
   142  		switch fs.Config.LogLevel {
   143  		case fs.LogLevelEmergency, fs.LogLevelAlert:
   144  			logrus.SetLevel(logrus.PanicLevel)
   145  		case fs.LogLevelCritical:
   146  			logrus.SetLevel(logrus.FatalLevel)
   147  		case fs.LogLevelError:
   148  			logrus.SetLevel(logrus.ErrorLevel)
   149  		case fs.LogLevelWarning, fs.LogLevelNotice:
   150  			logrus.SetLevel(logrus.WarnLevel)
   151  		case fs.LogLevelInfo:
   152  			logrus.SetLevel(logrus.InfoLevel)
   153  		case fs.LogLevelDebug:
   154  			logrus.SetLevel(logrus.DebugLevel)
   155  		}
   156  	}
   157  
   158  	if dumpHeaders {
   159  		fs.Config.Dump |= fs.DumpHeaders
   160  		fs.Logf(nil, "--dump-headers is obsolete - please use --dump headers instead")
   161  	}
   162  	if dumpBodies {
   163  		fs.Config.Dump |= fs.DumpBodies
   164  		fs.Logf(nil, "--dump-bodies is obsolete - please use --dump bodies instead")
   165  	}
   166  
   167  	switch {
   168  	case deleteBefore && (deleteDuring || deleteAfter),
   169  		deleteDuring && deleteAfter:
   170  		log.Fatalf(`Only one of --delete-before, --delete-during or --delete-after can be used.`)
   171  	case deleteBefore:
   172  		fs.Config.DeleteMode = fs.DeleteModeBefore
   173  	case deleteDuring:
   174  		fs.Config.DeleteMode = fs.DeleteModeDuring
   175  	case deleteAfter:
   176  		fs.Config.DeleteMode = fs.DeleteModeAfter
   177  	default:
   178  		fs.Config.DeleteMode = fs.DeleteModeDefault
   179  	}
   180  
   181  	if fs.Config.CompareDest != "" && fs.Config.CopyDest != "" {
   182  		log.Fatalf(`Can't use --compare-dest with --copy-dest.`)
   183  	}
   184  
   185  	switch {
   186  	case len(fs.Config.StatsOneLineDateFormat) > 0:
   187  		fs.Config.StatsOneLineDate = true
   188  		fs.Config.StatsOneLine = true
   189  	case fs.Config.StatsOneLineDate:
   190  		fs.Config.StatsOneLineDateFormat = "2006/01/02 15:04:05 - "
   191  		fs.Config.StatsOneLine = true
   192  	}
   193  
   194  	if bindAddr != "" {
   195  		addrs, err := net.LookupIP(bindAddr)
   196  		if err != nil {
   197  			log.Fatalf("--bind: Failed to parse %q as IP address: %v", bindAddr, err)
   198  		}
   199  		if len(addrs) != 1 {
   200  			log.Fatalf("--bind: Expecting 1 IP address for %q but got %d", bindAddr, len(addrs))
   201  		}
   202  		fs.Config.BindAddr = addrs[0]
   203  	}
   204  
   205  	if disableFeatures != "" {
   206  		if disableFeatures == "help" {
   207  			log.Fatalf("Possible backend features are: %s\n", strings.Join(new(fs.Features).List(), ", "))
   208  		}
   209  		fs.Config.DisableFeatures = strings.Split(disableFeatures, ",")
   210  	}
   211  
   212  	// Make the config file absolute
   213  	configPath, err := filepath.Abs(config.ConfigPath)
   214  	if err == nil {
   215  		config.ConfigPath = configPath
   216  	}
   217  
   218  	// Set whether multi-thread-streams was set
   219  	multiThreadStreamsFlag := pflag.Lookup("multi-thread-streams")
   220  	fs.Config.MultiThreadSet = multiThreadStreamsFlag != nil && multiThreadStreamsFlag.Changed
   221  
   222  }