mirror of
https://github.com/rclone/rclone.git
synced 2024-11-25 00:12:22 +08:00
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:
parent
6e853c82d8
commit
8d72698d5a
|
@ -88,7 +88,7 @@ func (vol *Volume) applyOptions(volOpt VolOpts) error {
|
||||||
fsType = "local"
|
fsType = "local"
|
||||||
if fsName != "" {
|
if fsName != "" {
|
||||||
var ok bool
|
var ok bool
|
||||||
fsType, ok = fs.ConfigMap(nil, fsName, nil).Get("type")
|
fsType, ok = fs.ConfigMap("", nil, fsName, nil).Get("type")
|
||||||
if !ok {
|
if !ok {
|
||||||
return fs.ErrorNotFoundInConfigFile
|
return fs.ErrorNotFoundInConfigFile
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ func Authorize(ctx context.Context, args []string, noAutoBrowser bool, templateF
|
||||||
// Name used for temporary remote
|
// Name used for temporary remote
|
||||||
name := "**temp-fs**"
|
name := "**temp-fs**"
|
||||||
|
|
||||||
m := fs.ConfigMap(ri, name, inM)
|
m := fs.ConfigMap(ri.Prefix, ri.Options, name, inM)
|
||||||
outM := configmap.Simple{}
|
outM := configmap.Simple{}
|
||||||
m.ClearSetters()
|
m.ClearSetters()
|
||||||
m.AddSetter(outM)
|
m.AddSetter(outM)
|
||||||
|
|
|
@ -470,7 +470,7 @@ func updateRemote(ctx context.Context, name string, keyValues rc.Params, opt Upd
|
||||||
}
|
}
|
||||||
|
|
||||||
choices := configmap.Simple{}
|
choices := configmap.Simple{}
|
||||||
m := fs.ConfigMap(ri, name, nil)
|
m := fs.ConfigMap(ri.Prefix, ri.Options, name, nil)
|
||||||
|
|
||||||
// Set the config
|
// Set the config
|
||||||
for k, v := range keyValues {
|
for k, v := range keyValues {
|
||||||
|
|
|
@ -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 {
|
func RemoteConfig(ctx context.Context, name string) error {
|
||||||
fmt.Printf("Remote config\n")
|
fmt.Printf("Remote config\n")
|
||||||
ri := mustFindByName(name)
|
ri := mustFindByName(name)
|
||||||
m := fs.ConfigMap(ri, name, nil)
|
m := fs.ConfigMap(ri.Prefix, ri.Options, name, nil)
|
||||||
if ri.Config == nil {
|
if ri.Config == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
// A configmap.Getter to read from the environment RCLONE_option_name
|
||||||
type optionEnvVars struct {
|
type optionEnvVars struct {
|
||||||
fsInfo *RegInfo
|
prefix string
|
||||||
|
options Options
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a config item from the option environment variables if possible
|
// Get a config item from the option environment variables if possible
|
||||||
func (oev optionEnvVars) Get(key string) (value string, ok bool) {
|
func (oev optionEnvVars) Get(key string) (value string, ok bool) {
|
||||||
opt := oev.fsInfo.Options.Get(key)
|
opt := oev.options.Get(key)
|
||||||
if opt == nil {
|
if opt == nil {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
envKey := OptionToEnv(oev.fsInfo.Prefix + "-" + key)
|
envKey := OptionToEnv(oev.prefix + "-" + key)
|
||||||
value, ok = os.LookupEnv(envKey)
|
value, ok = os.LookupEnv(envKey)
|
||||||
if ok {
|
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 {
|
} else if opt.NoPrefix {
|
||||||
// For options with NoPrefix set, check without prefix too
|
// For options with NoPrefix set, check without prefix too
|
||||||
envKey := OptionToEnv(key)
|
envKey := OptionToEnv(key)
|
||||||
value, ok = os.LookupEnv(envKey)
|
value, ok = os.LookupEnv(envKey)
|
||||||
if ok {
|
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
|
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
|
// A configmap.Getter to read either the default value or the set
|
||||||
// value from the RegInfo.Options
|
// value from the RegInfo.Options
|
||||||
type regInfoValues struct {
|
type regInfoValues struct {
|
||||||
fsInfo *RegInfo
|
options Options
|
||||||
useDefault bool
|
useDefault bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// override the values in configMap with the either the flag values or
|
// override the values in configMap with the either the flag values or
|
||||||
// the default values
|
// the default values
|
||||||
func (r *regInfoValues) Get(key string) (value string, ok bool) {
|
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) {
|
if opt != nil && (r.useDefault || opt.Value != nil) {
|
||||||
return opt.String(), true
|
return opt.String(), true
|
||||||
}
|
}
|
||||||
|
@ -89,13 +90,15 @@ func (section getConfigFile) Get(key string) (value string, ok bool) {
|
||||||
return value, ok
|
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),
|
// configName passed in. If connectionStringConfig has any entries (it may be nil),
|
||||||
// then it will be added to the lookup with the highest priority.
|
// 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".
|
// 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
|
// Create the config
|
||||||
config = configmap.New()
|
config = configmap.New()
|
||||||
|
|
||||||
|
@ -107,24 +110,26 @@ func ConfigMap(fsInfo *RegInfo, configName string, connectionStringConfig config
|
||||||
}
|
}
|
||||||
|
|
||||||
// flag values
|
// flag values
|
||||||
if fsInfo != nil {
|
if options != nil {
|
||||||
config.AddGetter(®InfoValues{fsInfo, false}, configmap.PriorityNormal)
|
config.AddGetter(®InfoValues{options, false}, configmap.PriorityNormal)
|
||||||
}
|
}
|
||||||
|
|
||||||
// remote specific environment vars
|
// remote specific environment vars
|
||||||
config.AddGetter(configEnvVars(configName), configmap.PriorityNormal)
|
config.AddGetter(configEnvVars(configName), configmap.PriorityNormal)
|
||||||
|
|
||||||
// backend specific environment vars
|
// backend specific environment vars
|
||||||
if fsInfo != nil {
|
if options != nil && prefix != "" {
|
||||||
config.AddGetter(optionEnvVars{fsInfo: fsInfo}, configmap.PriorityNormal)
|
config.AddGetter(optionEnvVars{prefix: prefix, options: options}, configmap.PriorityNormal)
|
||||||
}
|
}
|
||||||
|
|
||||||
// config file
|
// config file
|
||||||
config.AddGetter(getConfigFile(configName), configmap.PriorityConfig)
|
if configName != "" {
|
||||||
|
config.AddGetter(getConfigFile(configName), configmap.PriorityConfig)
|
||||||
|
}
|
||||||
|
|
||||||
// default values
|
// default values
|
||||||
if fsInfo != nil {
|
if options != nil {
|
||||||
config.AddGetter(®InfoValues{fsInfo, true}, configmap.PriorityDefault)
|
config.AddGetter(®InfoValues{options, true}, configmap.PriorityDefault)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set Config
|
// Set Config
|
||||||
|
|
|
@ -314,12 +314,6 @@ func TestOptionGetters(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
fsInfo := &RegInfo{
|
|
||||||
Name: "local",
|
|
||||||
Prefix: "local",
|
|
||||||
Options: testOptions,
|
|
||||||
}
|
|
||||||
|
|
||||||
oldConfigFileGet := ConfigFileGet
|
oldConfigFileGet := ConfigFileGet
|
||||||
ConfigFileGet = func(section, key string) (string, bool) {
|
ConfigFileGet = func(section, key string) (string, bool) {
|
||||||
if section == "sausage" && key == "key1" {
|
if section == "sausage" && key == "key1" {
|
||||||
|
@ -337,16 +331,16 @@ func TestOptionGetters(t *testing.T) {
|
||||||
configEnvVarsGetter := configEnvVars("local")
|
configEnvVarsGetter := configEnvVars("local")
|
||||||
|
|
||||||
// A configmap.Getter to read from the environment RCLONE_option_name
|
// 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
|
// A configmap.Getter to read either the default value or the set
|
||||||
// value from the RegInfo.Options
|
// value from the RegInfo.Options
|
||||||
regInfoValuesGetterFalse := ®InfoValues{
|
regInfoValuesGetterFalse := ®InfoValues{
|
||||||
fsInfo: fsInfo,
|
options: testOptions,
|
||||||
useDefault: false,
|
useDefault: false,
|
||||||
}
|
}
|
||||||
regInfoValuesGetterTrue := ®InfoValues{
|
regInfoValuesGetterTrue := ®InfoValues{
|
||||||
fsInfo: fsInfo,
|
options: testOptions,
|
||||||
useDefault: true,
|
useDefault: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ func ConfigFs(path string) (fsInfo *RegInfo, configName, fsPath string, config *
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
config = ConfigMap(fsInfo, configName, connectionStringConfig)
|
config = ConfigMap(fsInfo.Prefix, fsInfo.Options, configName, connectionStringConfig)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ func ParseRemote(path string) (fsInfo *RegInfo, configName, fsPath string, conne
|
||||||
if strings.HasPrefix(configName, ":") {
|
if strings.HasPrefix(configName, ":") {
|
||||||
fsName = configName[1:]
|
fsName = configName[1:]
|
||||||
} else {
|
} else {
|
||||||
m := ConfigMap(nil, configName, parsed.Config)
|
m := ConfigMap("", nil, configName, parsed.Config)
|
||||||
fsName, ok = m.Get("type")
|
fsName, ok = m.Get("type")
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, "", "", nil, ErrorNotFoundInConfigFile
|
return nil, "", "", nil, ErrorNotFoundInConfigFile
|
||||||
|
|
Loading…
Reference in New Issue
Block a user