fs: factor OptionToEnv and ConfigToEnv into fs

This commit is contained in:
Nick Craig-Wood 2018-05-16 16:30:09 +01:00
parent b3bd2d1c9e
commit 85d09729f2
3 changed files with 18 additions and 18 deletions

View file

@ -2,6 +2,7 @@ package fs
import ( import (
"net" "net"
"strings"
"time" "time"
) )
@ -103,3 +104,16 @@ func NewConfig() *ConfigInfo {
return c 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))
}

View file

@ -1108,19 +1108,12 @@ func Authorize(args []string) {
fs.Config(name) 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 // FileGet gets the config key under section returning the
// default or empty string if not set. // default or empty string if not set.
// //
// It looks up defaults in the environment if they are present // It looks up defaults in the environment if they are present
func FileGet(section, key string, defaultVal ...string) string { func FileGet(section, key string, defaultVal ...string) string {
envKey := configToEnv(section, key) envKey := fs.ConfigToEnv(section, key)
newValue, found := os.LookupEnv(envKey) newValue, found := os.LookupEnv(envKey)
if found { if found {
defaultVal = []string{newValue} 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 // It looks up defaults in the environment if they are present
func FileGetBool(section, key string, defaultVal ...bool) bool { func FileGetBool(section, key string, defaultVal ...bool) bool {
envKey := configToEnv(section, key) envKey := fs.ConfigToEnv(section, key)
newValue, found := os.LookupEnv(envKey) newValue, found := os.LookupEnv(envKey)
if found { if found {
newBool, err := strconv.ParseBool(newValue) 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 // It looks up defaults in the environment if they are present
func FileGetInt(section, key string, defaultVal ...int) int { func FileGetInt(section, key string, defaultVal ...int) int {
envKey := configToEnv(section, key) envKey := fs.ConfigToEnv(section, key)
newValue, found := os.LookupEnv(envKey) newValue, found := os.LookupEnv(envKey)
if found { if found {
newInt, err := strconv.Atoi(newValue) newInt, err := strconv.Atoi(newValue)

View file

@ -5,23 +5,16 @@ package flags
import ( import (
"log" "log"
"os" "os"
"strings"
"time" "time"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/spf13/pflag" "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 // setDefaultFromEnv constructs a name from the flag passed in and
// sets the default from the environment if possible. // sets the default from the environment if possible.
func setDefaultFromEnv(name string) { func setDefaultFromEnv(name string) {
key := optionToEnv(name) key := fs.OptionToEnv(name)
newValue, found := os.LookupEnv(key) newValue, found := os.LookupEnv(key)
if found { if found {
flag := pflag.Lookup(name) flag := pflag.Lookup(name)