2019-04-30 14:06:24 +02:00
|
|
|
// Package flags contains enhanced versions of spf13/pflag flag
|
2018-01-12 17:30:54 +01:00
|
|
|
// routines which will read from the environment also.
|
|
|
|
package flags
|
2016-12-20 16:50:46 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2016-12-20 16:50:46 +01:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// setValueFromEnv constructs a name from the flag passed in and
|
|
|
|
// sets the value and default from the environment if possible
|
|
|
|
// the value may be overridden when the command line is parsed
|
|
|
|
//
|
|
|
|
// Used to create non-backend flags like --stats
|
|
|
|
func setValueFromEnv(flags *pflag.FlagSet, name string) {
|
|
|
|
envKey := fs.OptionToEnv(name)
|
|
|
|
envValue, found := os.LookupEnv(envKey)
|
2016-12-20 16:50:46 +01:00
|
|
|
if found {
|
2019-10-11 17:37:46 +02:00
|
|
|
flag := flags.Lookup(name)
|
2016-12-20 16:50:46 +01:00
|
|
|
if flag == nil {
|
2020-12-28 13:26:23 +01:00
|
|
|
log.Fatalf("Couldn't find flag --%q", name)
|
2016-12-20 16:50:46 +01:00
|
|
|
}
|
2021-05-20 14:08:01 +02:00
|
|
|
err := flags.Set(name, envValue)
|
2016-12-20 16:50:46 +01:00
|
|
|
if err != nil {
|
2021-05-20 14:08:01 +02:00
|
|
|
log.Fatalf("Invalid value when setting --%s from environment variable %s=%q: %v", name, envKey, envValue, err)
|
2016-12-20 16:50:46 +01:00
|
|
|
}
|
2021-05-20 14:08:01 +02:00
|
|
|
fs.Debugf(nil, "Setting --%s %q from environment variable %s=%q", name, flag.Value, envKey, envValue)
|
|
|
|
flag.DefValue = envValue
|
2016-12-20 16:50:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// SetDefaultFromEnv constructs a name from the flag passed in and
|
|
|
|
// sets the default from the environment if possible
|
|
|
|
//
|
|
|
|
// Used to create backend flags like --skip-links
|
|
|
|
func SetDefaultFromEnv(flags *pflag.FlagSet, name string) {
|
|
|
|
envKey := fs.OptionToEnv(name)
|
|
|
|
envValue, found := os.LookupEnv(envKey)
|
|
|
|
if found {
|
|
|
|
flag := flags.Lookup(name)
|
|
|
|
if flag == nil {
|
|
|
|
log.Fatalf("Couldn't find flag --%q", name)
|
|
|
|
}
|
|
|
|
fs.Debugf(nil, "Setting default for %s=%q from environment variable %s", name, envValue, envKey)
|
|
|
|
//err = tempValue.Set()
|
|
|
|
flag.DefValue = envValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.StringP
|
|
|
|
func StringP(name, shorthand string, value string, usage string) (out *string) {
|
|
|
|
out = pflag.StringP(name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// StringVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.StringVarP
|
|
|
|
func StringVarP(flags *pflag.FlagSet, p *string, name, shorthand string, value string, usage string) {
|
|
|
|
flags.StringVarP(p, name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// BoolP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.BoolP
|
|
|
|
func BoolP(name, shorthand string, value bool, usage string) (out *bool) {
|
|
|
|
out = pflag.BoolP(name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// BoolVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.BoolVarP
|
|
|
|
func BoolVarP(flags *pflag.FlagSet, p *bool, name, shorthand string, value bool, usage string) {
|
|
|
|
flags.BoolVarP(p, name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// IntP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.IntP
|
|
|
|
func IntP(name, shorthand string, value int, usage string) (out *int) {
|
|
|
|
out = pflag.IntP(name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Int64P defines a flag which can be set by an environment variable
|
2018-01-12 17:30:54 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.IntP
|
|
|
|
func Int64P(name, shorthand string, value int64, usage string) (out *int64) {
|
|
|
|
out = pflag.Int64P(name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2018-01-12 17:30:54 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Int64VarP defines a flag which can be set by an environment variable
|
2018-01-22 19:53:18 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.Int64VarP
|
2019-10-11 17:55:04 +02:00
|
|
|
func Int64VarP(flags *pflag.FlagSet, p *int64, name, shorthand string, value int64, usage string) {
|
2018-01-22 19:53:18 +01:00
|
|
|
flags.Int64VarP(p, name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2018-01-22 19:53:18 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// IntVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.IntVarP
|
|
|
|
func IntVarP(flags *pflag.FlagSet, p *int, name, shorthand string, value int, usage string) {
|
|
|
|
flags.IntVarP(p, name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Uint32VarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.Uint32VarP
|
|
|
|
func Uint32VarP(flags *pflag.FlagSet, p *uint32, name, shorthand string, value uint32, usage string) {
|
|
|
|
flags.Uint32VarP(p, name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Float64P defines a flag which can be set by an environment variable
|
2017-07-13 06:11:24 +02:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.Float64P
|
|
|
|
func Float64P(name, shorthand string, value float64, usage string) (out *float64) {
|
|
|
|
out = pflag.Float64P(name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2017-07-13 06:11:24 +02:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Float64VarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.Float64VarP
|
|
|
|
func Float64VarP(flags *pflag.FlagSet, p *float64, name, shorthand string, value float64, usage string) {
|
|
|
|
flags.Float64VarP(p, name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// DurationP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
2022-11-10 18:28:12 +01:00
|
|
|
// It wraps the duration in an fs.Duration for extra suffixes and
|
|
|
|
// passes it to pflag.VarP
|
2016-12-20 16:50:46 +01:00
|
|
|
func DurationP(name, shorthand string, value time.Duration, usage string) (out *time.Duration) {
|
2022-11-10 18:28:12 +01:00
|
|
|
out = new(time.Duration)
|
|
|
|
*out = value
|
|
|
|
pflag.VarP((*fs.Duration)(out), name, shorthand, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// DurationVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
2022-11-10 18:28:12 +01:00
|
|
|
// It wraps the duration in an fs.Duration for extra suffixes and
|
|
|
|
// passes it to pflag.VarP
|
2017-11-07 18:09:08 +01:00
|
|
|
func DurationVarP(flags *pflag.FlagSet, p *time.Duration, name, shorthand string, value time.Duration, usage string) {
|
2022-11-10 18:28:12 +01:00
|
|
|
flags.VarP((*fs.Duration)(p), name, shorthand, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// VarP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.VarP
|
|
|
|
func VarP(value pflag.Value, name, shorthand, usage string) {
|
|
|
|
pflag.VarP(value, name, shorthand, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2016-12-20 16:50:46 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// FVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.VarP
|
2018-01-12 17:30:54 +01:00
|
|
|
func FVarP(flags *pflag.FlagSet, value pflag.Value, name, shorthand, usage string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.VarP(value, name, shorthand, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2020-10-07 13:29:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// StringArrayP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It sets one value only - command line flags can be used to set more.
|
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.StringArrayP
|
|
|
|
func StringArrayP(name, shorthand string, value []string, usage string) (out *[]string) {
|
|
|
|
out = pflag.StringArrayP(name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
2017-02-09 22:22:46 +01:00
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// StringArrayVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It sets one value only - command line flags can be used to set more.
|
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.StringArrayVarP
|
|
|
|
func StringArrayVarP(flags *pflag.FlagSet, p *[]string, name, shorthand string, value []string, usage string) {
|
|
|
|
flags.StringArrayVarP(p, name, shorthand, value, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// CountP defines a flag which can be set by an environment variable
|
2017-02-09 22:22:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.CountP
|
|
|
|
func CountP(name, shorthand string, usage string) (out *int) {
|
|
|
|
out = pflag.CountP(name, shorthand, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(pflag.CommandLine, name)
|
2017-02-09 22:22:46 +01:00
|
|
|
return out
|
|
|
|
}
|
2017-11-07 18:09:08 +01:00
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// CountVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.CountVarP
|
|
|
|
func CountVarP(flags *pflag.FlagSet, p *int, name, shorthand string, usage string) {
|
|
|
|
flags.CountVarP(p, name, shorthand, usage)
|
2021-05-20 14:08:01 +02:00
|
|
|
setValueFromEnv(flags, name)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|