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:
parent
0ed1857fa9
commit
354b4f19ec
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user