2018-01-12 17:30:54 +01:00
|
|
|
// Package configflags defines the flags used by rclone. It is
|
|
|
|
// decoupled into a separate package so it can be replaced.
|
|
|
|
package configflags
|
|
|
|
|
|
|
|
// Options set by command line flags
|
|
|
|
import (
|
2024-07-20 13:10:42 +02:00
|
|
|
"context"
|
2018-01-12 17:30:54 +01:00
|
|
|
"net"
|
2021-09-10 14:44:13 +02:00
|
|
|
"os"
|
2021-01-29 04:41:50 +01:00
|
|
|
"strconv"
|
2018-01-12 17:30:54 +01:00
|
|
|
"strings"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// these will get interpreted into fs.Config via SetFlags() below
|
2020-02-25 18:03:21 +01:00
|
|
|
verbose int
|
|
|
|
quiet bool
|
2021-04-08 17:49:47 +02:00
|
|
|
configPath string
|
2021-09-10 15:35:53 +02:00
|
|
|
cacheDir string
|
2021-09-10 14:44:13 +02:00
|
|
|
tempDir string
|
2020-02-25 18:03:21 +01:00
|
|
|
dumpHeaders bool
|
|
|
|
dumpBodies bool
|
|
|
|
deleteBefore bool
|
|
|
|
deleteDuring bool
|
|
|
|
deleteAfter bool
|
|
|
|
bindAddr string
|
|
|
|
disableFeatures string
|
2021-01-29 04:41:50 +01:00
|
|
|
dscp string
|
2020-02-10 08:50:08 +01:00
|
|
|
uploadHeaders []string
|
|
|
|
downloadHeaders []string
|
2020-04-23 16:24:21 +02:00
|
|
|
headers []string
|
2022-05-24 16:46:07 +02:00
|
|
|
metadataSet []string
|
2018-01-12 17:30:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddFlags adds the non filing system specific flags to the command
|
2020-11-05 12:33:32 +01:00
|
|
|
func AddFlags(ci *fs.ConfigInfo, flagSet *pflag.FlagSet) {
|
2024-07-09 19:42:01 +02:00
|
|
|
flags.AddFlagsFromOptions(flagSet, "", fs.ConfigOptionsInfo)
|
|
|
|
|
|
|
|
// Add flags we haven't converted into options yet
|
2023-07-10 19:34:10 +02:00
|
|
|
flags.CountVarP(flagSet, &verbose, "verbose", "v", "Print lots more stuff (repeat for more)", "Logging,Important")
|
|
|
|
flags.BoolVarP(flagSet, &quiet, "quiet", "q", false, "Print as little stuff as possible", "Logging")
|
|
|
|
flags.StringVarP(flagSet, &configPath, "config", "", config.GetConfigPath(), "Config file", "Config")
|
|
|
|
flags.StringVarP(flagSet, &cacheDir, "cache-dir", "", config.GetCacheDir(), "Directory rclone will use for caching", "Config")
|
|
|
|
flags.StringVarP(flagSet, &tempDir, "temp-dir", "", os.TempDir(), "Directory rclone will use for temporary files", "Config")
|
|
|
|
flags.BoolVarP(flagSet, &dumpHeaders, "dump-headers", "", false, "Dump HTTP headers - may contain sensitive info", "Debugging")
|
|
|
|
flags.BoolVarP(flagSet, &dumpBodies, "dump-bodies", "", false, "Dump HTTP headers and bodies - may contain sensitive info", "Debugging")
|
|
|
|
flags.BoolVarP(flagSet, &deleteBefore, "delete-before", "", false, "When synchronizing, delete files on destination before transferring", "Sync")
|
|
|
|
flags.BoolVarP(flagSet, &deleteDuring, "delete-during", "", false, "When synchronizing, delete files during transfer", "Sync")
|
|
|
|
flags.BoolVarP(flagSet, &deleteAfter, "delete-after", "", false, "When synchronizing, delete files on destination after transferring (default)", "Sync")
|
|
|
|
flags.StringVarP(flagSet, &bindAddr, "bind", "", "", "Local address to bind to for outgoing connections, IPv4, IPv6 or name", "Networking")
|
|
|
|
flags.StringVarP(flagSet, &disableFeatures, "disable", "", "", "Disable a comma separated list of features (use --disable help to see a list)", "Config")
|
|
|
|
flags.StringArrayVarP(flagSet, &uploadHeaders, "header-upload", "", nil, "Set HTTP header for upload transactions", "Networking")
|
|
|
|
flags.StringArrayVarP(flagSet, &downloadHeaders, "header-download", "", nil, "Set HTTP header for download transactions", "Networking")
|
|
|
|
flags.StringArrayVarP(flagSet, &headers, "header", "", nil, "Set HTTP header for all transactions", "Networking")
|
|
|
|
flags.StringArrayVarP(flagSet, &metadataSet, "metadata-set", "", nil, "Add metadata key=value when uploading", "Metadata")
|
|
|
|
flags.StringVarP(flagSet, &dscp, "dscp", "", "", "Set DSCP value to connections, value or name, e.g. CS1, LE, DF, AF21", "Networking")
|
2020-02-10 08:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseHeaders converts the strings passed in via the header flags into HTTPOptions
|
|
|
|
func ParseHeaders(headers []string) []*fs.HTTPOption {
|
|
|
|
opts := []*fs.HTTPOption{}
|
|
|
|
for _, header := range headers {
|
|
|
|
parts := strings.SplitN(header, ":", 2)
|
|
|
|
if len(parts) == 1 {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "Failed to parse '%s' as an HTTP header. Expecting a string like: 'Content-Encoding: gzip'", header)
|
2020-02-10 08:50:08 +01:00
|
|
|
}
|
|
|
|
option := &fs.HTTPOption{
|
|
|
|
Key: strings.TrimSpace(parts[0]),
|
|
|
|
Value: strings.TrimSpace(parts[1]),
|
|
|
|
}
|
|
|
|
opts = append(opts, option)
|
|
|
|
}
|
|
|
|
return opts
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// SetFlags sets flags which aren't part of the config system
|
2020-11-05 12:33:32 +01:00
|
|
|
func SetFlags(ci *fs.ConfigInfo) {
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process obsolete --dump-headers and --dump-bodies flags
|
2021-06-22 18:32:26 +02:00
|
|
|
if dumpHeaders {
|
|
|
|
ci.Dump |= fs.DumpHeaders
|
|
|
|
fs.Logf(nil, "--dump-headers is obsolete - please use --dump headers instead")
|
|
|
|
}
|
|
|
|
if dumpBodies {
|
|
|
|
ci.Dump |= fs.DumpBodies
|
|
|
|
fs.Logf(nil, "--dump-bodies is obsolete - please use --dump bodies instead")
|
|
|
|
}
|
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process -v flag
|
2018-01-12 17:30:54 +01:00
|
|
|
if verbose >= 2 {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.LogLevel = fs.LogLevelDebug
|
2018-01-12 17:30:54 +01:00
|
|
|
} else if verbose >= 1 {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.LogLevel = fs.LogLevelInfo
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2024-07-09 19:42:01 +02:00
|
|
|
|
|
|
|
// Process -q flag
|
2018-01-12 17:30:54 +01:00
|
|
|
if quiet {
|
|
|
|
if verbose > 0 {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "Can't set -v and -q")
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.LogLevel = fs.LogLevelError
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2024-07-09 19:42:01 +02:00
|
|
|
|
|
|
|
// Can't set log level, -v, -q
|
2018-01-12 17:30:54 +01:00
|
|
|
logLevelFlag := pflag.Lookup("log-level")
|
|
|
|
if logLevelFlag != nil && logLevelFlag.Changed {
|
|
|
|
if verbose > 0 {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "Can't set -v and --log-level")
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
if quiet {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "Can't set -q and --log-level")
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process --delete-before, --delete-during and --delete-after
|
2018-01-12 17:30:54 +01:00
|
|
|
switch {
|
|
|
|
case deleteBefore && (deleteDuring || deleteAfter),
|
|
|
|
deleteDuring && deleteAfter:
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, `Only one of --delete-before, --delete-during or --delete-after can be used.`)
|
2018-01-12 17:30:54 +01:00
|
|
|
case deleteBefore:
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.DeleteMode = fs.DeleteModeBefore
|
2018-01-12 17:30:54 +01:00
|
|
|
case deleteDuring:
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.DeleteMode = fs.DeleteModeDuring
|
2018-01-12 17:30:54 +01:00
|
|
|
case deleteAfter:
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.DeleteMode = fs.DeleteModeAfter
|
2018-01-12 17:30:54 +01:00
|
|
|
default:
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.DeleteMode = fs.DeleteModeDefault
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process --bind into IP address
|
2018-01-12 17:30:54 +01:00
|
|
|
if bindAddr != "" {
|
|
|
|
addrs, err := net.LookupIP(bindAddr)
|
|
|
|
if err != nil {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "--bind: Failed to parse %q as IP address: %v", bindAddr, err)
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
if len(addrs) != 1 {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "--bind: Expecting 1 IP address for %q but got %d", bindAddr, len(addrs))
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.BindAddr = addrs[0]
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process --disable
|
2018-01-12 17:30:54 +01:00
|
|
|
if disableFeatures != "" {
|
|
|
|
if disableFeatures == "help" {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "Possible backend features are: %s\n", strings.Join(new(fs.Features).List(), ", "))
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.DisableFeatures = strings.Split(disableFeatures, ",")
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process --headers-upload, --headers-download, --headers
|
2020-02-10 08:50:08 +01:00
|
|
|
if len(uploadHeaders) != 0 {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.UploadHeaders = ParseHeaders(uploadHeaders)
|
2020-02-10 08:50:08 +01:00
|
|
|
}
|
|
|
|
if len(downloadHeaders) != 0 {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.DownloadHeaders = ParseHeaders(downloadHeaders)
|
2020-02-10 08:50:08 +01:00
|
|
|
}
|
2020-04-23 16:24:21 +02:00
|
|
|
if len(headers) != 0 {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.Headers = ParseHeaders(headers)
|
2020-04-23 16:24:21 +02:00
|
|
|
}
|
2024-07-09 19:42:01 +02:00
|
|
|
|
|
|
|
// Process --metadata-set
|
2022-05-24 16:46:07 +02:00
|
|
|
if len(metadataSet) != 0 {
|
|
|
|
ci.MetadataSet = make(fs.Metadata, len(metadataSet))
|
|
|
|
for _, kv := range metadataSet {
|
|
|
|
equal := strings.IndexRune(kv, '=')
|
|
|
|
if equal < 0 {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "Failed to parse '%s' as metadata key=value.", kv)
|
2022-05-24 16:46:07 +02:00
|
|
|
}
|
|
|
|
ci.MetadataSet[strings.ToLower(kv[:equal])] = kv[equal+1:]
|
|
|
|
}
|
|
|
|
fs.Debugf(nil, "MetadataUpload %v", ci.MetadataSet)
|
|
|
|
}
|
2024-07-09 19:42:01 +02:00
|
|
|
|
|
|
|
// Process --dscp
|
2021-01-29 04:41:50 +01:00
|
|
|
if len(dscp) != 0 {
|
|
|
|
if value, ok := parseDSCP(dscp); ok {
|
|
|
|
ci.TrafficClass = value << 2
|
|
|
|
} else {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "--dscp: Invalid DSCP name: %v", dscp)
|
2021-01-29 04:41:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-10 08:50:08 +01:00
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process --config path
|
2021-04-08 17:49:47 +02:00
|
|
|
if err := config.SetConfigPath(configPath); err != nil {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "--config: Failed to set %q as config path: %v", configPath, err)
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2019-08-12 23:09:40 +02:00
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process --cache-dir path
|
2021-09-10 15:35:53 +02:00
|
|
|
if err := config.SetCacheDir(cacheDir); err != nil {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "--cache-dir: Failed to set %q as cache dir: %v", cacheDir, err)
|
2021-09-10 15:35:53 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process --temp-dir path
|
2021-09-10 14:44:13 +02:00
|
|
|
if err := config.SetTempDir(tempDir); err != nil {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "--temp-dir: Failed to set %q as temp dir: %v", tempDir, err)
|
2021-09-10 14:44:13 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 19:42:01 +02:00
|
|
|
// Process --multi-thread-streams - set whether multi-thread-streams was set
|
2019-08-12 23:09:40 +02:00
|
|
|
multiThreadStreamsFlag := pflag.Lookup("multi-thread-streams")
|
2020-11-05 12:33:32 +01:00
|
|
|
ci.MultiThreadSet = multiThreadStreamsFlag != nil && multiThreadStreamsFlag.Changed
|
2024-07-20 13:10:42 +02:00
|
|
|
|
|
|
|
// Reload any changes
|
|
|
|
if err := ci.Reload(context.Background()); err != nil {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatalf(nil, "Failed to reload config changes: %v", err)
|
2024-07-20 13:10:42 +02:00
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2021-01-29 04:41:50 +01:00
|
|
|
|
2024-09-02 18:20:08 +02:00
|
|
|
// parseDSCP converts DSCP names to value
|
2021-01-29 04:41:50 +01:00
|
|
|
func parseDSCP(dscp string) (uint8, bool) {
|
|
|
|
if s, err := strconv.ParseUint(dscp, 10, 6); err == nil {
|
|
|
|
return uint8(s), true
|
|
|
|
}
|
|
|
|
dscp = strings.ToUpper(dscp)
|
|
|
|
switch dscp {
|
|
|
|
case "BE":
|
|
|
|
fallthrough
|
|
|
|
case "DF":
|
|
|
|
fallthrough
|
|
|
|
case "CS0":
|
|
|
|
return 0x00, true
|
|
|
|
case "CS1":
|
|
|
|
return 0x08, true
|
|
|
|
case "AF11":
|
|
|
|
return 0x0A, true
|
|
|
|
case "AF12":
|
|
|
|
return 0x0C, true
|
|
|
|
case "AF13":
|
|
|
|
return 0x0E, true
|
|
|
|
case "CS2":
|
|
|
|
return 0x10, true
|
|
|
|
case "AF21":
|
|
|
|
return 0x12, true
|
|
|
|
case "AF22":
|
|
|
|
return 0x14, true
|
|
|
|
case "AF23":
|
|
|
|
return 0x16, true
|
|
|
|
case "CS3":
|
|
|
|
return 0x18, true
|
|
|
|
case "AF31":
|
|
|
|
return 0x1A, true
|
|
|
|
case "AF32":
|
|
|
|
return 0x1C, true
|
|
|
|
case "AF33":
|
|
|
|
return 0x1E, true
|
|
|
|
case "CS4":
|
|
|
|
return 0x20, true
|
|
|
|
case "AF41":
|
|
|
|
return 0x22, true
|
|
|
|
case "AF42":
|
|
|
|
return 0x24, true
|
|
|
|
case "AF43":
|
|
|
|
return 0x26, true
|
|
|
|
case "CS5":
|
|
|
|
return 0x28, true
|
|
|
|
case "EF":
|
|
|
|
return 0x2E, true
|
|
|
|
case "CS6":
|
|
|
|
return 0x30, true
|
|
|
|
case "LE":
|
|
|
|
return 0x01, true
|
|
|
|
default:
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
}
|