2021-06-14 19:42:49 +08:00
|
|
|
// Getters and Setters for ConfigMap
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A configmap.Getter to read from the environment RCLONE_CONFIG_backend_option_name
|
|
|
|
type configEnvVars string
|
|
|
|
|
|
|
|
// Get a config item from the environment variables if possible
|
|
|
|
func (configName configEnvVars) Get(key string) (value string, ok bool) {
|
2021-05-20 20:08:01 +08:00
|
|
|
envKey := ConfigToEnv(string(configName), key)
|
|
|
|
value, ok = os.LookupEnv(envKey)
|
|
|
|
if ok {
|
|
|
|
Debugf(nil, "Setting %s=%q for %q from environment variable %s", key, value, configName, envKey)
|
|
|
|
}
|
|
|
|
return value, ok
|
2021-06-14 19:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// A configmap.Getter to read from the environment RCLONE_option_name
|
|
|
|
type optionEnvVars struct {
|
2024-07-02 01:06:49 +08:00
|
|
|
prefix string
|
|
|
|
options Options
|
2021-06-14 19:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a config item from the option environment variables if possible
|
|
|
|
func (oev optionEnvVars) Get(key string) (value string, ok bool) {
|
2024-07-02 01:06:49 +08:00
|
|
|
opt := oev.options.Get(key)
|
2021-06-14 19:42:49 +08:00
|
|
|
if opt == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
2024-07-04 00:22:47 +08:00
|
|
|
var envKey string
|
|
|
|
if oev.prefix == "" {
|
|
|
|
envKey = OptionToEnv(key)
|
|
|
|
} else {
|
|
|
|
envKey = OptionToEnv(oev.prefix + "-" + key)
|
|
|
|
}
|
2021-05-20 20:08:01 +08:00
|
|
|
value, ok = os.LookupEnv(envKey)
|
|
|
|
if ok {
|
2024-07-04 00:22:47 +08:00
|
|
|
Debugf(nil, "Setting %s %s=%q from environment variable %s", oev.prefix, key, value, envKey)
|
2021-05-20 20:08:01 +08:00
|
|
|
} else if opt.NoPrefix {
|
|
|
|
// For options with NoPrefix set, check without prefix too
|
|
|
|
envKey := OptionToEnv(key)
|
|
|
|
value, ok = os.LookupEnv(envKey)
|
2021-06-14 19:42:49 +08:00
|
|
|
if ok {
|
2024-07-02 01:06:49 +08:00
|
|
|
Debugf(nil, "Setting %s=%q for %s from environment variable %s", key, value, oev.prefix, envKey)
|
2021-06-14 19:42:49 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 20:08:01 +08:00
|
|
|
return value, ok
|
2021-06-14 19:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// A configmap.Getter to read either the default value or the set
|
|
|
|
// value from the RegInfo.Options
|
|
|
|
type regInfoValues struct {
|
2024-07-02 01:06:49 +08:00
|
|
|
options Options
|
2021-06-14 19:42:49 +08:00
|
|
|
useDefault bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// override the values in configMap with the either the flag values or
|
|
|
|
// the default values
|
|
|
|
func (r *regInfoValues) Get(key string) (value string, ok bool) {
|
2024-07-02 01:06:49 +08:00
|
|
|
opt := r.options.Get(key)
|
2024-07-12 19:40:12 +08:00
|
|
|
if opt != nil && (r.useDefault || !opt.IsDefault()) {
|
2021-06-14 19:42:49 +08:00
|
|
|
return opt.String(), true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// A configmap.Setter to read from the config file
|
|
|
|
type setConfigFile string
|
|
|
|
|
|
|
|
// Set a config item into the config file
|
|
|
|
func (section setConfigFile) Set(key, value string) {
|
2021-07-14 00:41:28 +08:00
|
|
|
Debugf(nil, "Saving config %q in section %q of the config file", key, section)
|
2021-06-14 19:42:49 +08:00
|
|
|
err := ConfigFileSet(string(section), key, value)
|
|
|
|
if err != nil {
|
2021-07-14 00:41:28 +08:00
|
|
|
Errorf(nil, "Failed saving config %q in section %q of the config file: %v", key, section, err)
|
2021-06-14 19:42:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A configmap.Getter to read from the config file
|
|
|
|
type getConfigFile string
|
|
|
|
|
|
|
|
// Get a config item from the config file
|
|
|
|
func (section getConfigFile) Get(key string) (value string, ok bool) {
|
|
|
|
value, ok = ConfigFileGet(string(section), key)
|
|
|
|
// Ignore empty lines in the config file
|
|
|
|
if value == "" {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
return value, ok
|
|
|
|
}
|
|
|
|
|
2024-07-02 01:06:49 +08:00
|
|
|
// ConfigMap creates a configmap.Map from the Options, prefix and the
|
2021-06-14 19:42:49 +08:00
|
|
|
// configName passed in. If connectionStringConfig has any entries (it may be nil),
|
|
|
|
// then it will be added to the lookup with the highest priority.
|
|
|
|
//
|
2024-07-02 01:06:49 +08:00
|
|
|
// If options is nil then the returned configmap.Map should only be
|
2021-06-14 19:42:49 +08:00
|
|
|
// used for reading non backend specific parameters, such as "type".
|
2024-07-02 01:06:49 +08:00
|
|
|
//
|
|
|
|
// This can be used for global settings if prefix is "" and configName is ""
|
|
|
|
func ConfigMap(prefix string, options Options, configName string, connectionStringConfig configmap.Simple) (config *configmap.Map) {
|
2021-06-14 19:42:49 +08:00
|
|
|
// Create the config
|
|
|
|
config = configmap.New()
|
|
|
|
|
|
|
|
// Read the config, more specific to least specific
|
|
|
|
|
|
|
|
// Config from connection string
|
|
|
|
if len(connectionStringConfig) > 0 {
|
|
|
|
config.AddGetter(connectionStringConfig, configmap.PriorityNormal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flag values
|
2024-07-02 01:06:49 +08:00
|
|
|
if options != nil {
|
|
|
|
config.AddGetter(®InfoValues{options, false}, configmap.PriorityNormal)
|
2021-06-14 19:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// remote specific environment vars
|
2024-07-04 00:22:47 +08:00
|
|
|
if configName != "" {
|
|
|
|
config.AddGetter(configEnvVars(configName), configmap.PriorityNormal)
|
|
|
|
}
|
2021-06-14 19:42:49 +08:00
|
|
|
|
|
|
|
// backend specific environment vars
|
2024-07-04 00:22:47 +08:00
|
|
|
if options != nil {
|
2024-07-02 01:06:49 +08:00
|
|
|
config.AddGetter(optionEnvVars{prefix: prefix, options: options}, configmap.PriorityNormal)
|
2021-06-14 19:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// config file
|
2024-07-02 01:06:49 +08:00
|
|
|
if configName != "" {
|
|
|
|
config.AddGetter(getConfigFile(configName), configmap.PriorityConfig)
|
|
|
|
}
|
2021-06-14 19:42:49 +08:00
|
|
|
|
|
|
|
// default values
|
2024-07-02 01:06:49 +08:00
|
|
|
if options != nil {
|
|
|
|
config.AddGetter(®InfoValues{options, true}, configmap.PriorityDefault)
|
2021-06-14 19:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set Config
|
|
|
|
config.AddSetter(setConfigFile(configName))
|
|
|
|
return config
|
|
|
|
}
|