1
mirror of https://github.com/rclone/rclone synced 2024-12-18 07:45:55 +01:00

config: support hyphen in remote name from environment variable

This commit is contained in:
albertony 2021-09-16 19:39:46 +02:00
parent 2d05b28b0a
commit 59c74ea1b8
2 changed files with 56 additions and 4 deletions

View File

@ -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" {

View File

@ -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