From 354b4f19ec51daea1af0c7946b0ecd6bdadc9693 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 26 Nov 2020 17:15:52 +0000 Subject: [PATCH] rc: add context to flag Reload function #4685 --- cmd/cmd.go | 5 +++-- fs/filter/filterflags/filterflags.go | 4 +++- fs/rc/config.go | 6 +++--- fs/rc/config_test.go | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index d9b29fa43..3d5224ff2 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -373,7 +373,8 @@ func StartStats() func() { // initConfig is run by cobra after initialising the flags func initConfig() { - ci := fs.GetConfig(context.Background()) + ctx := context.Background() + ci := fs.GetConfig(ctx) // Activate logger systemd support if systemd invocation ID is detected _, sysdLaunch := systemd.GetInvocationID() if sysdLaunch { @@ -387,7 +388,7 @@ func initConfig() { configflags.SetFlags(ci) // Load filters - err := filterflags.Reload() + err := filterflags.Reload(ctx) if err != nil { log.Fatalf("Failed to load filters: %v", err) } diff --git a/fs/filter/filterflags/filterflags.go b/fs/filter/filterflags/filterflags.go index e2e48ca27..f37574cb2 100644 --- a/fs/filter/filterflags/filterflags.go +++ b/fs/filter/filterflags/filterflags.go @@ -2,6 +2,8 @@ package filterflags import ( + "context" + "github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/filter" "github.com/rclone/rclone/fs/rc" @@ -14,7 +16,7 @@ var ( ) // Reload the filters from the flags -func Reload() (err error) { +func Reload(ctx context.Context) (err error) { filter.Active, err = filter.NewFilter(&Opt) return err } diff --git a/fs/rc/config.go b/fs/rc/config.go index eb4667444..5add5b212 100644 --- a/fs/rc/config.go +++ b/fs/rc/config.go @@ -12,7 +12,7 @@ import ( var ( optionBlock = map[string]interface{}{} - optionReload = map[string]func() error{} + optionReload = map[string]func(context.Context) error{} ) // AddOption adds an option set @@ -22,7 +22,7 @@ func AddOption(name string, option interface{}) { // AddOptionReload adds an option set with a reload function to be // called when options are changed -func AddOptionReload(name string, option interface{}, reload func() error) { +func AddOptionReload(name string, option interface{}, reload func(context.Context) error) { optionBlock[name] = option optionReload[name] = reload } @@ -116,7 +116,7 @@ func rcOptionsSet(ctx context.Context, in Params) (out Params, err error) { return nil, errors.Wrapf(err, "failed to write options from block %q", name) } if reload := optionReload[name]; reload != nil { - err = reload() + err = reload(ctx) if err != nil { return nil, errors.Wrapf(err, "failed to reload options from block %q", name) } diff --git a/fs/rc/config_test.go b/fs/rc/config_test.go index eb46698e8..144edc352 100644 --- a/fs/rc/config_test.go +++ b/fs/rc/config_test.go @@ -41,7 +41,7 @@ func TestAddOption(t *testing.T) { func TestAddOptionReload(t *testing.T) { defer clearOptionBlock()() assert.Equal(t, len(optionBlock), 0) - reload := func() error { return nil } + reload := func(ctx context.Context) error { return nil } AddOptionReload("potato", &testOptions, reload) assert.Equal(t, len(optionBlock), 1) assert.Equal(t, len(optionReload), 1) @@ -99,7 +99,7 @@ func TestOptionsGetMarshal(t *testing.T) { func TestOptionsSet(t *testing.T) { defer clearOptionBlock()() var reloaded int - AddOptionReload("potato", &testOptions, func() error { + AddOptionReload("potato", &testOptions, func(ctx context.Context) error { if reloaded > 0 { return errors.New("error while reloading") }