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 (
"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))
}

View File

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

View File

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