From ba72e62b41cbfd6f36ec089a1f58c26d31a57734 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 19 Jun 2019 14:51:19 +0200 Subject: [PATCH] fs/config: Add method to reload configfile from disk Fixes #3268 --- fs/config/config.go | 10 ++++++++++ fs/config/config_test.go | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/fs/config/config.go b/fs/config/config.go index be9d3b5cb..df18f66a5 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -1340,6 +1340,16 @@ func FileDeleteKey(section, key string) bool { var matchEnv = regexp.MustCompile(`^RCLONE_CONFIG_(.*?)_TYPE=.*$`) +// FileRefresh ensures the latest configFile is loaded from disk +func FileRefresh() error { + reloadedConfigFile, err := loadConfigFile() + if err != nil { + return err + } + configFile = reloadedConfigFile + return nil +} + // FileSections returns the sections in the config file // including any defined by environment variables. func FileSections() []string { diff --git a/fs/config/config_test.go b/fs/config/config_test.go index efd654bba..cde4da35c 100644 --- a/fs/config/config_test.go +++ b/fs/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "bytes" "fmt" "io/ioutil" "os" @@ -292,3 +293,21 @@ func TestMatchProvider(t *testing.T) { assert.Equal(t, test.want, got, what) } } + +func TestFileRefresh(t *testing.T) { + defer testConfigFile(t, "refresh.conf")() + require.NoError(t, CreateRemote("refresh_test", "config_test_remote", rc.Params{ + "bool": true, + })) + b, err := ioutil.ReadFile(ConfigPath) + assert.NoError(t, err) + + b = bytes.Replace(b, []byte("refresh_test"), []byte("refreshed_test"), 1) + err = ioutil.WriteFile(ConfigPath, b, 0644) + assert.NoError(t, err) + + assert.NotEqual(t, []string{"refreshed_test"}, configFile.GetSectionList()) + err = FileRefresh() + assert.NoError(t, err) + assert.Equal(t, []string{"refreshed_test"}, configFile.GetSectionList()) +}