github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/configmap.go (about) 1 // Getters and Setters for ConfigMap 2 3 package fs 4 5 import ( 6 "os" 7 8 "github.com/rclone/rclone/fs/config/configmap" 9 ) 10 11 // A configmap.Getter to read from the environment RCLONE_CONFIG_backend_option_name 12 type configEnvVars string 13 14 // Get a config item from the environment variables if possible 15 func (configName configEnvVars) Get(key string) (value string, ok bool) { 16 envKey := ConfigToEnv(string(configName), key) 17 value, ok = os.LookupEnv(envKey) 18 if ok { 19 Debugf(nil, "Setting %s=%q for %q from environment variable %s", key, value, configName, envKey) 20 } 21 return value, ok 22 } 23 24 // A configmap.Getter to read from the environment RCLONE_option_name 25 type optionEnvVars struct { 26 fsInfo *RegInfo 27 } 28 29 // Get a config item from the option environment variables if possible 30 func (oev optionEnvVars) Get(key string) (value string, ok bool) { 31 opt := oev.fsInfo.Options.Get(key) 32 if opt == nil { 33 return "", false 34 } 35 envKey := OptionToEnv(oev.fsInfo.Prefix + "-" + key) 36 value, ok = os.LookupEnv(envKey) 37 if ok { 38 Debugf(nil, "Setting %s_%s=%q from environment variable %s", oev.fsInfo.Prefix, key, value, envKey) 39 } else if opt.NoPrefix { 40 // For options with NoPrefix set, check without prefix too 41 envKey := OptionToEnv(key) 42 value, ok = os.LookupEnv(envKey) 43 if ok { 44 Debugf(nil, "Setting %s=%q for %s from environment variable %s", key, value, oev.fsInfo.Prefix, envKey) 45 } 46 } 47 return value, ok 48 } 49 50 // A configmap.Getter to read either the default value or the set 51 // value from the RegInfo.Options 52 type regInfoValues struct { 53 fsInfo *RegInfo 54 useDefault bool 55 } 56 57 // override the values in configMap with the either the flag values or 58 // the default values 59 func (r *regInfoValues) Get(key string) (value string, ok bool) { 60 opt := r.fsInfo.Options.Get(key) 61 if opt != nil && (r.useDefault || opt.Value != nil) { 62 return opt.String(), true 63 } 64 return "", false 65 } 66 67 // A configmap.Setter to read from the config file 68 type setConfigFile string 69 70 // Set a config item into the config file 71 func (section setConfigFile) Set(key, value string) { 72 Debugf(nil, "Saving config %q in section %q of the config file", key, section) 73 err := ConfigFileSet(string(section), key, value) 74 if err != nil { 75 Errorf(nil, "Failed saving config %q in section %q of the config file: %v", key, section, err) 76 } 77 } 78 79 // A configmap.Getter to read from the config file 80 type getConfigFile string 81 82 // Get a config item from the config file 83 func (section getConfigFile) Get(key string) (value string, ok bool) { 84 value, ok = ConfigFileGet(string(section), key) 85 // Ignore empty lines in the config file 86 if value == "" { 87 ok = false 88 } 89 return value, ok 90 } 91 92 // ConfigMap creates a configmap.Map from the *RegInfo and the 93 // configName passed in. If connectionStringConfig has any entries (it may be nil), 94 // then it will be added to the lookup with the highest priority. 95 // 96 // If fsInfo is nil then the returned configmap.Map should only be 97 // used for reading non backend specific parameters, such as "type". 98 func ConfigMap(fsInfo *RegInfo, configName string, connectionStringConfig configmap.Simple) (config *configmap.Map) { 99 // Create the config 100 config = configmap.New() 101 102 // Read the config, more specific to least specific 103 104 // Config from connection string 105 if len(connectionStringConfig) > 0 { 106 config.AddGetter(connectionStringConfig, configmap.PriorityNormal) 107 } 108 109 // flag values 110 if fsInfo != nil { 111 config.AddGetter(®InfoValues{fsInfo, false}, configmap.PriorityNormal) 112 } 113 114 // remote specific environment vars 115 config.AddGetter(configEnvVars(configName), configmap.PriorityNormal) 116 117 // backend specific environment vars 118 if fsInfo != nil { 119 config.AddGetter(optionEnvVars{fsInfo: fsInfo}, configmap.PriorityNormal) 120 } 121 122 // config file 123 config.AddGetter(getConfigFile(configName), configmap.PriorityConfig) 124 125 // default values 126 if fsInfo != nil { 127 config.AddGetter(®InfoValues{fsInfo, true}, configmap.PriorityDefault) 128 } 129 130 // Set Config 131 config.AddSetter(setConfigFile(configName)) 132 return config 133 }