fs: add Features.Enabled to return map of enabled features by name

This commit is contained in:
Nick Craig-Wood 2019-06-08 08:46:53 +01:00
parent bd10344d65
commit e1cf551ded
2 changed files with 45 additions and 0 deletions

View File

@ -618,6 +618,26 @@ func (ft *Features) List() (out []string) {
return out
}
// Enabled returns a map of features with keys showing whether they
// are enabled or not
func (ft *Features) Enabled() (features map[string]bool) {
v := reflect.ValueOf(ft).Elem()
vType := v.Type()
features = make(map[string]bool, v.NumField())
for i := 0; i < v.NumField(); i++ {
vName := vType.Field(i).Name
field := v.Field(i)
if field.Kind() == reflect.Func {
// Can't compare functions
features[vName] = !field.IsNil()
} else {
zero := reflect.Zero(field.Type())
features[vName] = field.Interface() != zero.Interface()
}
}
return features
}
// DisableList nil's out the comma separated list of named features.
// If it isn't found then it will log a message.
func (ft *Features) DisableList(list []string) *Features {

View File

@ -41,6 +41,31 @@ func TestFeaturesList(t *testing.T) {
assert.True(t, strings.Contains(names, ",Copy,"))
}
func TestFeaturesEnabled(t *testing.T) {
ft := new(Features)
ft.CaseInsensitive = true
ft.Purge = func() error { return nil }
enabled := ft.Enabled()
flag, ok := enabled["CaseInsensitive"]
assert.Equal(t, true, ok)
assert.Equal(t, true, flag, enabled)
flag, ok = enabled["Purge"]
assert.Equal(t, true, ok)
assert.Equal(t, true, flag, enabled)
flag, ok = enabled["DuplicateFiles"]
assert.Equal(t, true, ok)
assert.Equal(t, false, flag, enabled)
flag, ok = enabled["Copy"]
assert.Equal(t, true, ok)
assert.Equal(t, false, flag, enabled)
assert.Equal(t, len(ft.List()), len(enabled))
}
func TestFeaturesDisableList(t *testing.T) {
ft := new(Features)
ft.Copy = func(src Object, remote string) (Object, error) {