mirror of
https://github.com/rclone/rclone
synced 2024-09-06 00:08:57 +02:00
fs: enable configmap to be able to tell values set vs config file values #4996
This adds AddOverrideGetter and GetOverride methods to config map and uses them in fs.ConfigMap. This enables us to tell which values have been set and which are just read from the config file or at their defaults. This also deletes the unused AddGetters method in configmap.
This commit is contained in:
parent
96207f342c
commit
f111e0eaf8
@ -29,8 +29,9 @@ type Mapper interface {
|
||||
// Map provides a wrapper around multiple Setter and
|
||||
// Getter interfaces.
|
||||
type Map struct {
|
||||
setters []Setter
|
||||
getters []Getter
|
||||
setters []Setter
|
||||
getters []Getter
|
||||
override []Getter
|
||||
}
|
||||
|
||||
// New returns an empty Map
|
||||
@ -44,9 +45,12 @@ func (c *Map) AddGetter(getter Getter) *Map {
|
||||
return c
|
||||
}
|
||||
|
||||
// AddGetters appends multiple getters onto the end of the getters
|
||||
func (c *Map) AddGetters(getters ...Getter) *Map {
|
||||
c.getters = append(c.getters, getters...)
|
||||
// AddOverrideGetter appends a getter onto the end of the getters
|
||||
//
|
||||
// It also appends it onto the override getters for GetOverride
|
||||
func (c *Map) AddOverrideGetter(getter Getter) *Map {
|
||||
c.getters = append(c.getters, getter)
|
||||
c.override = append(c.override, getter)
|
||||
return c
|
||||
}
|
||||
|
||||
@ -56,11 +60,11 @@ func (c *Map) AddSetter(setter Setter) *Map {
|
||||
return c
|
||||
}
|
||||
|
||||
// Get gets an item with the key passed in and return the value from
|
||||
// get gets an item with the key passed in and return the value from
|
||||
// the first getter. If the item is found then it returns true,
|
||||
// otherwise false.
|
||||
func (c *Map) Get(key string) (value string, ok bool) {
|
||||
for _, do := range c.getters {
|
||||
func (c *Map) get(key string, getters []Getter) (value string, ok bool) {
|
||||
for _, do := range getters {
|
||||
value, ok = do.Get(key)
|
||||
if ok {
|
||||
return value, ok
|
||||
@ -69,6 +73,20 @@ func (c *Map) Get(key string) (value string, ok bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Get gets an item with the key passed in and return the value from
|
||||
// the first getter. If the item is found then it returns true,
|
||||
// otherwise false.
|
||||
func (c *Map) Get(key string) (value string, ok bool) {
|
||||
return c.get(key, c.getters)
|
||||
}
|
||||
|
||||
// GetOverride gets an item with the key passed in and return the
|
||||
// value from the first override getter. If the item is found then it
|
||||
// returns true, otherwise false.
|
||||
func (c *Map) GetOverride(key string) (value string, ok bool) {
|
||||
return c.get(key, c.override)
|
||||
}
|
||||
|
||||
// Set sets an item into all the stored setters.
|
||||
func (c *Map) Set(key, value string) {
|
||||
for _, do := range c.setters {
|
||||
|
@ -90,6 +90,56 @@ func TestConfigMapSet(t *testing.T) {
|
||||
}, m2)
|
||||
}
|
||||
|
||||
func TestConfigMapGetOverride(t *testing.T) {
|
||||
m := New()
|
||||
|
||||
value, found := m.GetOverride("config1")
|
||||
assert.Equal(t, "", value)
|
||||
assert.Equal(t, false, found)
|
||||
|
||||
value, found = m.GetOverride("config2")
|
||||
assert.Equal(t, "", value)
|
||||
assert.Equal(t, false, found)
|
||||
|
||||
m1 := Simple{
|
||||
"config1": "one",
|
||||
}
|
||||
|
||||
m.AddOverrideGetter(m1)
|
||||
|
||||
value, found = m.GetOverride("config1")
|
||||
assert.Equal(t, "one", value)
|
||||
assert.Equal(t, true, found)
|
||||
|
||||
value, found = m.GetOverride("config2")
|
||||
assert.Equal(t, "", value)
|
||||
assert.Equal(t, false, found)
|
||||
|
||||
m2 := Simple{
|
||||
"config1": "one2",
|
||||
"config2": "two2",
|
||||
}
|
||||
|
||||
m.AddGetter(m2)
|
||||
|
||||
value, found = m.GetOverride("config1")
|
||||
assert.Equal(t, "one", value)
|
||||
assert.Equal(t, true, found)
|
||||
|
||||
value, found = m.GetOverride("config2")
|
||||
assert.Equal(t, "", value)
|
||||
assert.Equal(t, false, found)
|
||||
|
||||
value, found = m.Get("config1")
|
||||
assert.Equal(t, "one", value)
|
||||
assert.Equal(t, true, found)
|
||||
|
||||
value, found = m.Get("config2")
|
||||
assert.Equal(t, "two2", value)
|
||||
assert.Equal(t, true, found)
|
||||
|
||||
}
|
||||
|
||||
func TestSimpleString(t *testing.T) {
|
||||
// Basic
|
||||
assert.Equal(t, "", Simple(nil).String())
|
||||
|
8
fs/fs.go
8
fs/fs.go
@ -1316,20 +1316,20 @@ func ConfigMap(fsInfo *RegInfo, configName string, connectionStringConfig config
|
||||
|
||||
// Config from connection string
|
||||
if len(connectionStringConfig) > 0 {
|
||||
config.AddGetter(connectionStringConfig)
|
||||
config.AddOverrideGetter(connectionStringConfig)
|
||||
}
|
||||
|
||||
// flag values
|
||||
if fsInfo != nil {
|
||||
config.AddGetter(®InfoValues{fsInfo, false})
|
||||
config.AddOverrideGetter(®InfoValues{fsInfo, false})
|
||||
}
|
||||
|
||||
// remote specific environment vars
|
||||
config.AddGetter(configEnvVars(configName))
|
||||
config.AddOverrideGetter(configEnvVars(configName))
|
||||
|
||||
// backend specific environment vars
|
||||
if fsInfo != nil {
|
||||
config.AddGetter(optionEnvVars{fsInfo: fsInfo})
|
||||
config.AddOverrideGetter(optionEnvVars{fsInfo: fsInfo})
|
||||
}
|
||||
|
||||
// config file
|
||||
|
Loading…
Reference in New Issue
Block a user