1
mirror of https://github.com/rclone/rclone synced 2024-11-15 14:57:03 +01:00

rc: add context to flag Reload function #4685

This commit is contained in:
Nick Craig-Wood 2020-11-26 17:15:52 +00:00
parent 0ed1857fa9
commit 354b4f19ec
4 changed files with 11 additions and 8 deletions

View File

@ -373,7 +373,8 @@ func StartStats() func() {
// initConfig is run by cobra after initialising the flags // initConfig is run by cobra after initialising the flags
func initConfig() { func initConfig() {
ci := fs.GetConfig(context.Background()) ctx := context.Background()
ci := fs.GetConfig(ctx)
// Activate logger systemd support if systemd invocation ID is detected // Activate logger systemd support if systemd invocation ID is detected
_, sysdLaunch := systemd.GetInvocationID() _, sysdLaunch := systemd.GetInvocationID()
if sysdLaunch { if sysdLaunch {
@ -387,7 +388,7 @@ func initConfig() {
configflags.SetFlags(ci) configflags.SetFlags(ci)
// Load filters // Load filters
err := filterflags.Reload() err := filterflags.Reload(ctx)
if err != nil { if err != nil {
log.Fatalf("Failed to load filters: %v", err) log.Fatalf("Failed to load filters: %v", err)
} }

View File

@ -2,6 +2,8 @@
package filterflags package filterflags
import ( import (
"context"
"github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/fs/filter" "github.com/rclone/rclone/fs/filter"
"github.com/rclone/rclone/fs/rc" "github.com/rclone/rclone/fs/rc"
@ -14,7 +16,7 @@ var (
) )
// Reload the filters from the flags // Reload the filters from the flags
func Reload() (err error) { func Reload(ctx context.Context) (err error) {
filter.Active, err = filter.NewFilter(&Opt) filter.Active, err = filter.NewFilter(&Opt)
return err return err
} }

View File

@ -12,7 +12,7 @@ import (
var ( var (
optionBlock = map[string]interface{}{} optionBlock = map[string]interface{}{}
optionReload = map[string]func() error{} optionReload = map[string]func(context.Context) error{}
) )
// AddOption adds an option set // 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 // AddOptionReload adds an option set with a reload function to be
// called when options are changed // 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 optionBlock[name] = option
optionReload[name] = reload 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) return nil, errors.Wrapf(err, "failed to write options from block %q", name)
} }
if reload := optionReload[name]; reload != nil { if reload := optionReload[name]; reload != nil {
err = reload() err = reload(ctx)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to reload options from block %q", name) return nil, errors.Wrapf(err, "failed to reload options from block %q", name)
} }

View File

@ -41,7 +41,7 @@ func TestAddOption(t *testing.T) {
func TestAddOptionReload(t *testing.T) { func TestAddOptionReload(t *testing.T) {
defer clearOptionBlock()() defer clearOptionBlock()()
assert.Equal(t, len(optionBlock), 0) assert.Equal(t, len(optionBlock), 0)
reload := func() error { return nil } reload := func(ctx context.Context) error { return nil }
AddOptionReload("potato", &testOptions, reload) AddOptionReload("potato", &testOptions, reload)
assert.Equal(t, len(optionBlock), 1) assert.Equal(t, len(optionBlock), 1)
assert.Equal(t, len(optionReload), 1) assert.Equal(t, len(optionReload), 1)
@ -99,7 +99,7 @@ func TestOptionsGetMarshal(t *testing.T) {
func TestOptionsSet(t *testing.T) { func TestOptionsSet(t *testing.T) {
defer clearOptionBlock()() defer clearOptionBlock()()
var reloaded int var reloaded int
AddOptionReload("potato", &testOptions, func() error { AddOptionReload("potato", &testOptions, func(ctx context.Context) error {
if reloaded > 0 { if reloaded > 0 {
return errors.New("error while reloading") return errors.New("error while reloading")
} }