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