From 59c74ea1b839cdf09cc0667bd1ae9127b5242f34 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Thu, 16 Sep 2021 19:39:46 +0200 Subject: [PATCH] config: support hyphen in remote name from environment variable --- cmdtest/environment_test.go | 54 ++++++++++++++++++++++++++++++++++++- fs/config.go | 6 ++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/cmdtest/environment_test.go b/cmdtest/environment_test.go index 14538522e..2124dc31c 100644 --- a/cmdtest/environment_test.go +++ b/cmdtest/environment_test.go @@ -78,6 +78,59 @@ func TestEnvironmentVariables(t *testing.T) { assert.Contains(t, out, "RCLONE_STATS=") } + // Backend flags and remote name + // - The listremotes command includes names from environment variables, + // the part between "RCLONE_CONFIG_" and "_TYPE", converted to lowercase. + // - When using using a remote created from env, e.g. with lsd command, + // the name is case insensitive in contrast to remotes in config file + // (fs.ConfigToEnv converts to uppercase before checking environment). + // - Previously using a remote created from env, e.g. with lsd command, + // would not be possible for remotes with '-' in names, and remote names + // with '_' could be referred to with both '-' and '_', because any '-' + // were replaced with '_' before lookup. + // =================================== + + env = "RCLONE_CONFIG_MY-LOCAL_TYPE=local" + out, err = rcloneEnv(env, "listremotes") + if assert.NoError(t, err) { + assert.Contains(t, out, "my-local:") + } + out, err = rcloneEnv(env, "lsl", "my-local:"+testFolder) + if assert.NoError(t, err) { + assert.Contains(t, out, "rclone.config") + assert.Contains(t, out, "file1.txt") + assert.Contains(t, out, "fileA1.txt") + assert.Contains(t, out, "fileAA1.txt") + } + out, err = rcloneEnv(env, "lsl", "mY-LoCaL:"+testFolder) + if assert.NoError(t, err) { + assert.Contains(t, out, "rclone.config") + assert.Contains(t, out, "file1.txt") + assert.Contains(t, out, "fileA1.txt") + assert.Contains(t, out, "fileAA1.txt") + } + out, err = rcloneEnv(env, "lsl", "my_local:"+testFolder) + if assert.Error(t, err) { + assert.Contains(t, out, "Failed to create file system") + } + + env = "RCLONE_CONFIG_MY_LOCAL_TYPE=local" + out, err = rcloneEnv(env, "listremotes") + if assert.NoError(t, err) { + assert.Contains(t, out, "my_local:") + } + out, err = rcloneEnv(env, "lsl", "my_local:"+testFolder) + if assert.NoError(t, err) { + assert.Contains(t, out, "rclone.config") + assert.Contains(t, out, "file1.txt") + assert.Contains(t, out, "fileA1.txt") + assert.Contains(t, out, "fileAA1.txt") + } + out, err = rcloneEnv(env, "lsl", "my-local:"+testFolder) + if assert.Error(t, err) { + assert.Contains(t, out, "Failed to create file system") + } + // Backend flags and option precedence // =================================== @@ -86,7 +139,6 @@ func TestEnvironmentVariables(t *testing.T) { // and skip_links=false on all levels with lower precedence // // Reference: https://rclone.org/docs/#precedence - // Create a symlink in test data err = os.Symlink(testdataPath+"/folderA", testdataPath+"/symlinkA") if runtime.GOOS == "windows" { diff --git a/fs/config.go b/fs/config.go index e5fabb6ad..3a4de7717 100644 --- a/fs/config.go +++ b/fs/config.go @@ -237,11 +237,11 @@ func AddConfig(ctx context.Context) (context.Context, *ConfigInfo) { return newCtx, cCopy } -// ConfigToEnv converts a config section and name, e.g. ("myremote", +// ConfigToEnv converts a config section and name, e.g. ("my-remote", // "ignore-size") into an environment name -// "RCLONE_CONFIG_MYREMOTE_IGNORE_SIZE" +// "RCLONE_CONFIG_MY-REMOTE_IGNORE_SIZE" func ConfigToEnv(section, name string) string { - return "RCLONE_CONFIG_" + strings.ToUpper(strings.Replace(section+"_"+name, "-", "_", -1)) + return "RCLONE_CONFIG_" + strings.ToUpper(section+"_"+strings.Replace(name, "-", "_", -1)) } // OptionToEnv converts an option name, e.g. "ignore-size" into an