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

This is in preparation for generalising the backend config system
This commit is contained in:
Nick Craig-Wood 2024-07-01 18:06:49 +01:00
parent 6e853c82d8
commit 8d72698d5a
7 changed files with 31 additions and 32 deletions

View file

@ -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
}

View file

@ -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)

View file

@ -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 {

View file

@ -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
}

View file

@ -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

View file

@ -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,
}

View file

@ -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