1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-06 00:03:32 +00:00

fs: add NonDefaultRC for discovering options in use

This enables us to send rc messages with the config in use.
This commit is contained in:
Nick Craig-Wood
2025-07-28 12:14:13 +01:00
parent 86edb26fd5
commit f16b39165b
2 changed files with 58 additions and 0 deletions

View File

@@ -154,6 +154,37 @@ func (os Options) NonDefault(m configmap.Getter) configmap.Simple {
return nonDefault
}
// NonDefaultRC discovers which config values aren't at their default
//
// It expects a pointer to the current config struct in opts.
//
// It returns the overridden config in rc config format.
func (os Options) NonDefaultRC(opts any) (map[string]any, error) {
items, err := configstruct.Items(opts)
if err != nil {
return nil, err
}
itemsByName := map[string]*configstruct.Item{}
for i := range items {
item := &items[i]
itemsByName[item.Name] = item
}
var nonDefault = map[string]any{}
for i := range os {
opt := &os[i]
item, found := itemsByName[opt.Name]
if !found {
return nil, fmt.Errorf("key %q in OptionsInfo not found in Options struct", opt.Name)
}
value := fmt.Sprint(item.Value)
defaultValue := fmt.Sprint(opt.Default)
if value != defaultValue {
nonDefault[item.Field] = item.Value
}
}
return nonDefault, nil
}
// HasAdvanced discovers if any options have an Advanced setting
func (os Options) HasAdvanced() bool {
for i := range os {

View File

@@ -279,3 +279,30 @@ func TestOptionGetters(t *testing.T) {
}
}
func TestOptionsNonDefaultRC(t *testing.T) {
type cfg struct {
X string `config:"x"`
Y int `config:"y"`
}
c := &cfg{X: "a", Y: 6}
opts := Options{
{Name: "x", Default: "a"}, // at default, should be omitted
{Name: "y", Default: 5}, // non-default, should be included
}
got, err := opts.NonDefaultRC(c)
require.NoError(t, err)
require.Equal(t, map[string]any{"Y": 6}, got)
}
func TestOptionsNonDefaultRCMissingKey(t *testing.T) {
type cfg struct {
X string `config:"x"`
}
c := &cfg{X: "a"}
// Options refers to a key not present in the struct -> expect error
opts := Options{{Name: "missing", Default: ""}}
_, err := opts.NonDefaultRC(c)
assert.ErrorContains(t, err, "not found")
}