From 85d09729f2eb90a0edd868daae0ea795c6e09273 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 16 May 2018 16:30:09 +0100 Subject: [PATCH] fs: factor OptionToEnv and ConfigToEnv into fs --- fs/config.go | 14 ++++++++++++++ fs/config/config.go | 13 +++---------- fs/config/flags/flags.go | 9 +-------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/fs/config.go b/fs/config.go index 543923981..c5283e14b 100644 --- a/fs/config.go +++ b/fs/config.go @@ -2,6 +2,7 @@ package fs import ( "net" + "strings" "time" ) @@ -103,3 +104,16 @@ func NewConfig() *ConfigInfo { return c } + +// ConfigToEnv converts an config section and name, eg ("myremote", +// "ignore-size") into an environment name +// "RCLONE_CONFIG_MYREMOTE_IGNORE_SIZE" +func ConfigToEnv(section, name string) string { + return "RCLONE_CONFIG_" + strings.ToUpper(strings.Replace(section+"_"+name, "-", "_", -1)) +} + +// OptionToEnv converts an option name, eg "ignore-size" into an +// environment name "RCLONE_IGNORE_SIZE" +func OptionToEnv(name string) string { + return "RCLONE_" + strings.ToUpper(strings.Replace(name, "-", "_", -1)) +} diff --git a/fs/config/config.go b/fs/config/config.go index ad4d2ea3d..4eca15032 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -1108,19 +1108,12 @@ func Authorize(args []string) { fs.Config(name) } -// configToEnv converts an config section and name, eg ("myremote", -// "ignore-size") into an environment name -// "RCLONE_CONFIG_MYREMOTE_IGNORE_SIZE" -func configToEnv(section, name string) string { - return "RCLONE_CONFIG_" + strings.ToUpper(strings.Replace(section+"_"+name, "-", "_", -1)) -} - // FileGet gets the config key under section returning the // default or empty string if not set. // // It looks up defaults in the environment if they are present func FileGet(section, key string, defaultVal ...string) string { - envKey := configToEnv(section, key) + envKey := fs.ConfigToEnv(section, key) newValue, found := os.LookupEnv(envKey) if found { defaultVal = []string{newValue} @@ -1133,7 +1126,7 @@ func FileGet(section, key string, defaultVal ...string) string { // // It looks up defaults in the environment if they are present func FileGetBool(section, key string, defaultVal ...bool) bool { - envKey := configToEnv(section, key) + envKey := fs.ConfigToEnv(section, key) newValue, found := os.LookupEnv(envKey) if found { newBool, err := strconv.ParseBool(newValue) @@ -1151,7 +1144,7 @@ func FileGetBool(section, key string, defaultVal ...bool) bool { // // It looks up defaults in the environment if they are present func FileGetInt(section, key string, defaultVal ...int) int { - envKey := configToEnv(section, key) + envKey := fs.ConfigToEnv(section, key) newValue, found := os.LookupEnv(envKey) if found { newInt, err := strconv.Atoi(newValue) diff --git a/fs/config/flags/flags.go b/fs/config/flags/flags.go index 1618dd1fd..4297f3c06 100644 --- a/fs/config/flags/flags.go +++ b/fs/config/flags/flags.go @@ -5,23 +5,16 @@ package flags import ( "log" "os" - "strings" "time" "github.com/ncw/rclone/fs" "github.com/spf13/pflag" ) -// optionToEnv converts an option name, eg "ignore-size" into an -// environment name "RCLONE_IGNORE_SIZE" -func optionToEnv(name string) string { - return "RCLONE_" + strings.ToUpper(strings.Replace(name, "-", "_", -1)) -} - // setDefaultFromEnv constructs a name from the flag passed in and // sets the default from the environment if possible. func setDefaultFromEnv(name string) { - key := optionToEnv(name) + key := fs.OptionToEnv(name) newValue, found := os.LookupEnv(key) if found { flag := pflag.Lookup(name)