From a76c9a1636c6608725f2efc79c95589bdcffea23 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sun, 28 Jun 2020 18:05:03 +0100 Subject: [PATCH] fs: Debug the names of env vars we read values from as part of the config See: https://forum.rclone.org/t/how-can-i-check-if-environment-variables-are-being-used/17490 --- fs/config/config.go | 6 +++--- fs/config/flags/flags.go | 3 +-- fs/fs.go | 22 +++++++++++++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/fs/config/config.go b/fs/config/config.go index f7066ad83..a719ac555 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -172,7 +172,7 @@ func makeConfigPath() string { // Check to see if user supplied a --config variable or environment // variable. We can't use pflag for this because it isn't initialised // yet so we search the command line manually. - _, configSupplied := os.LookupEnv("RCLONE_CONFIG") + _, configSupplied := fs.LookupEnv("RCLONE_CONFIG") if !configSupplied { for _, item := range os.Args { if item == "--config" || strings.HasPrefix(item, "--config=") { @@ -311,7 +311,7 @@ func loadConfigFile() (*goconfig.ConfigFile, error) { } else { usingPasswordCommand = false - envpw := os.Getenv("RCLONE_CONFIG_PASS") + envpw := fs.Getenv("RCLONE_CONFIG_PASS") if envpw != "" { err := setConfigPassword(envpw) @@ -1432,7 +1432,7 @@ func FileGetFlag(section, key string) (string, bool) { // It looks up defaults in the environment if they are present func FileGet(section, key string, defaultVal ...string) string { envKey := fs.ConfigToEnv(section, key) - newValue, found := os.LookupEnv(envKey) + newValue, found := fs.LookupEnv(envKey) if found { defaultVal = []string{newValue} } diff --git a/fs/config/flags/flags.go b/fs/config/flags/flags.go index bd27810fa..b081e6c95 100644 --- a/fs/config/flags/flags.go +++ b/fs/config/flags/flags.go @@ -4,7 +4,6 @@ package flags import ( "log" - "os" "time" "github.com/rclone/rclone/fs" @@ -15,7 +14,7 @@ import ( // sets the default from the environment if possible. func setDefaultFromEnv(flags *pflag.FlagSet, name string) { key := fs.OptionToEnv(name) - newValue, found := os.LookupEnv(key) + newValue, found := fs.LookupEnv(key) if found { flag := flags.Lookup(name) if flag == nil { diff --git a/fs/fs.go b/fs/fs.go index 121898a97..cd7d798a5 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -1193,9 +1193,25 @@ func ParseRemote(path string) (fsInfo *RegInfo, configName, fsPath string, err e // A configmap.Getter to read from the environment RCLONE_CONFIG_backend_option_name type configEnvVars string +// LookupEnv calls os.LookupEnv and Debugfs a message if the env var was found +func LookupEnv(key string) (value string, ok bool) { + value, ok = os.LookupEnv(key) + if ok { + Debugf(nil, "Read env var %s", key) + } + return value, ok +} + +// Getenv calls os.LookupEnv and Debugfs a message if the env var was +// found. If the var wasn't round it returns empty string. +func Getenv(key string) (value string) { + value, _ = LookupEnv(key) + return value +} + // Get a config item from the environment variables if possible func (configName configEnvVars) Get(key string) (value string, ok bool) { - return os.LookupEnv(ConfigToEnv(string(configName), key)) + return LookupEnv(ConfigToEnv(string(configName), key)) } // A configmap.Getter to read from the environment RCLONE_option_name @@ -1211,12 +1227,12 @@ func (oev optionEnvVars) Get(key string) (value string, ok bool) { } // For options with NoPrefix set, check without prefix too if opt.NoPrefix { - value, ok = os.LookupEnv(OptionToEnv(key)) + value, ok = LookupEnv(OptionToEnv(key)) if ok { return value, ok } } - return os.LookupEnv(OptionToEnv(oev.fsInfo.Prefix + "-" + key)) + return LookupEnv(OptionToEnv(oev.fsInfo.Prefix + "-" + key)) } // A configmap.Getter to read either the default value or the set