Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

fs: refactor fs.ConfigMap to take a prefix and Options rather than an fs.RegInfo

Nick Craig-Wood8d72698d5aba
diff --git a/cmd/serve/docker/options.go b/cmd/serve/docker/options.go
index dd94bb119..c62327eaf 100644
--- a/cmd/serve/docker/options.go
+++ b/cmd/serve/docker/options.go
@@ -88,7 +88,7 @@ func (vol *Volume) applyOptions(volOpt VolOpts) error {
fsType = "local"
if fsName != "" {
var ok bool
- fsType, ok = fs.ConfigMap(nil, fsName, nil).Get("type")
+ fsType, ok = fs.ConfigMap("", nil, fsName, nil).Get("type")
if !ok {
return fs.ErrorNotFoundInConfigFile
}
diff --git a/fs/config/authorize.go b/fs/config/authorize.go
index b40f7e2b4..8f3aa86ce 100644
--- a/fs/config/authorize.go
+++ b/fs/config/authorize.go
@@ -60,7 +60,7 @@ func Authorize(ctx context.Context, args []string, noAutoBrowser bool, templateF
// Name used for temporary remote
name := "**temp-fs**"
- m := fs.ConfigMap(ri, name, inM)
+ m := fs.ConfigMap(ri.Prefix, ri.Options, name, inM)
outM := configmap.Simple{}
m.ClearSetters()
m.AddSetter(outM)
diff --git a/fs/config/config.go b/fs/config/config.go
index 4f27031d1..c4a56f096 100644
--- a/fs/config/config.go
+++ b/fs/config/config.go
@@ -470,7 +470,7 @@ func updateRemote(ctx context.Context, name string, keyValues rc.Params, opt Upd
}
choices := configmap.Simple{}
- m := fs.ConfigMap(ri, name, nil)
+ m := fs.ConfigMap(ri.Prefix, ri.Options, name, nil)
// Set the config
for k, v := range keyValues {
diff --git a/fs/config/ui.go b/fs/config/ui.go
index 694548b71..7c99871b2 100644
--- a/fs/config/ui.go
+++ b/fs/config/ui.go
@@ -454,7 +454,7 @@ func PostConfig(ctx context.Context, name string, m configmap.Mapper, ri *fs.Reg
func RemoteConfig(ctx context.Context, name string) error {
fmt.Printf("Remote config\n")
ri := mustFindByName(name)
- m := fs.ConfigMap(ri, name, nil)
+ m := fs.ConfigMap(ri.Prefix, ri.Options, name, nil)
if ri.Config == nil {
return nil
}
diff --git a/fs/configmap.go b/fs/configmap.go
index 31a7d3fa3..9a0610c8e 100644
--- a/fs/configmap.go
+++ b/fs/configmap.go
@@ -23,25 +23,26 @@ func (configName configEnvVars) Get(key string) (value string, ok bool) {
// A configmap.Getter to read from the environment RCLONE_option_name
type optionEnvVars struct {
- fsInfo *RegInfo
+ prefix string
+ options Options
}
// Get a config item from the option environment variables if possible
func (oev optionEnvVars) Get(key string) (value string, ok bool) {
- opt := oev.fsInfo.Options.Get(key)
+ opt := oev.options.Get(key)
if opt == nil {
return "", false
}
- envKey := OptionToEnv(oev.fsInfo.Prefix + "-" + key)
+ envKey := OptionToEnv(oev.prefix + "-" + key)
value, ok = os.LookupEnv(envKey)
if ok {
- Debugf(nil, "Setting %s_%s=%q from environment variable %s", oev.fsInfo.Prefix, key, value, envKey)
+ Debugf(nil, "Setting %s_%s=%q from environment variable %s", oev.prefix, key, value, envKey)
} else if opt.NoPrefix {
// For options with NoPrefix set, check without prefix too
envKey := OptionToEnv(key)
value, ok = os.LookupEnv(envKey)
if ok {
- Debugf(nil, "Setting %s=%q for %s from environment variable %s", key, value, oev.fsInfo.Prefix, envKey)
+ Debugf(nil, "Setting %s=%q for %s from environment variable %s", key, value, oev.prefix, envKey)
}
}
return value, ok
@@ -50,14 +51,14 @@ func (oev optionEnvVars) Get(key string) (value string, ok bool) {
// A configmap.Getter to read either the default value or the set
// value from the RegInfo.Options
type regInfoValues struct {
- fsInfo *RegInfo
+ options Options
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) {
- opt := r.fsInfo.Options.Get(key)
+ opt := r.options.Get(key)
if opt != nil && (r.useDefault || opt.Value != nil) {
return opt.String(), true
}
@@ -89,13 +90,15 @@ func (section getConfigFile) Get(key string) (value string, ok bool) {
return value, ok
}
-// ConfigMap creates a configmap.Map from the *RegInfo and the
+// ConfigMap creates a configmap.Map from the Options, prefix and the
// configName passed in. If connectionStringConfig has any entries (it may be nil),
// then it will be added to the lookup with the highest priority.
//
-// If fsInfo is nil then the returned configmap.Map should only be
+// If options is nil then the returned configmap.Map should only be
// used for reading non backend specific parameters, such as "type".
-func ConfigMap(fsInfo *RegInfo, configName string, connectionStringConfig configmap.Simple) (config *configmap.Map) {
+//
+// 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) {
// Create the config
config = configmap.New()
@@ -107,24 +110,26 @@ func ConfigMap(fsInfo *RegInfo, configName string, connectionStringConfig config
}
// flag values
- if fsInfo != nil {
- config.AddGetter(&regInfoValues{fsInfo, false}, configmap.PriorityNormal)
+ if options != nil {
+ config.AddGetter(&regInfoValues{options, false}, configmap.PriorityNormal)
}
// remote specific environment vars
config.AddGetter(configEnvVars(configName), configmap.PriorityNormal)
// backend specific environment vars
- if fsInfo != nil {
- config.AddGetter(optionEnvVars{fsInfo: fsInfo}, configmap.PriorityNormal)
+ if options != nil && prefix != "" {
+ config.AddGetter(optionEnvVars{prefix: prefix, options: options}, configmap.PriorityNormal)
}
// config file
- config.AddGetter(getConfigFile(configName), configmap.PriorityConfig)
+ if configName != "" {
+ config.AddGetter(getConfigFile(configName), configmap.PriorityConfig)
+ }
// default values
- if fsInfo != nil {
- config.AddGetter(&regInfoValues{fsInfo, true}, configmap.PriorityDefault)
+ if options != nil {
+ config.AddGetter(&regInfoValues{options, true}, configmap.PriorityDefault)
}
// Set Config
diff --git a/fs/fs_test.go b/fs/fs_test.go
index 862ab34c4..a83137072 100644
--- a/fs/fs_test.go
+++ b/fs/fs_test.go
@@ -314,12 +314,6 @@ func TestOptionGetters(t *testing.T) {
}
}()
- fsInfo := &RegInfo{
- Name: "local",
- Prefix: "local",
- Options: testOptions,
- }
-
oldConfigFileGet := ConfigFileGet
ConfigFileGet = func(section, key string) (string, bool) {
if section == "sausage" && key == "key1" {
@@ -337,16 +331,16 @@ func TestOptionGetters(t *testing.T) {
configEnvVarsGetter := configEnvVars("local")
// A configmap.Getter to read from the environment RCLONE_option_name
- optionEnvVarsGetter := optionEnvVars{fsInfo}
+ optionEnvVarsGetter := optionEnvVars{"local", testOptions}
// A configmap.Getter to read either the default value or the set
// value from the RegInfo.Options
regInfoValuesGetterFalse := &regInfoValues{
- fsInfo: fsInfo,
+ options: testOptions,
useDefault: false,
}
regInfoValuesGetterTrue := &regInfoValues{
- fsInfo: fsInfo,
+ options: testOptions,
useDefault: true,
}
diff --git a/fs/newfs.go b/fs/newfs.go
index 6c20814ec..d59a505a6 100644
--- a/fs/newfs.go
+++ b/fs/newfs.go
@@ -83,7 +83,7 @@ func ConfigFs(path string) (fsInfo *RegInfo, configName, fsPath string, config *
if err != nil {
return
}
- config = ConfigMap(fsInfo, configName, connectionStringConfig)
+ config = ConfigMap(fsInfo.Prefix, fsInfo.Options, configName, connectionStringConfig)
return
}
@@ -101,7 +101,7 @@ func ParseRemote(path string) (fsInfo *RegInfo, configName, fsPath string, conne
if strings.HasPrefix(configName, ":") {
fsName = configName[1:]
} else {
- m := ConfigMap(nil, configName, parsed.Config)
+ m := ConfigMap("", nil, configName, parsed.Config)
fsName, ok = m.Get("type")
if !ok {
return nil, "", "", nil, ErrorNotFoundInConfigFile