1
mirror of https://github.com/rclone/rclone synced 2025-01-03 03:46:24 +01:00

fs/config: Add method to reload configfile from disk

Fixes #3268
This commit is contained in:
Maran 2019-06-19 14:51:19 +02:00 committed by Nick Craig-Wood
parent 5935cb0a29
commit ba72e62b41
2 changed files with 29 additions and 0 deletions

View File

@ -1340,6 +1340,16 @@ func FileDeleteKey(section, key string) bool {
var matchEnv = regexp.MustCompile(`^RCLONE_CONFIG_(.*?)_TYPE=.*$`) 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 // FileSections returns the sections in the config file
// including any defined by environment variables. // including any defined by environment variables.
func FileSections() []string { func FileSections() []string {

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -292,3 +293,21 @@ func TestMatchProvider(t *testing.T) {
assert.Equal(t, test.want, got, what) 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())
}