allow to set default arguments values from env vars

This commit is contained in:
Nicola Murino
2019-08-02 09:47:14 +02:00
parent 73c61cda31
commit ba3f9d891a
3 changed files with 53 additions and 24 deletions

View File

@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"time"
"github.com/drakkan/sftpgo/logger"
@@ -69,3 +70,23 @@ func SetPathPermissions(path string, uid int, gid int) {
}
}
}
// GetEnvVar retrieves the value of the environment variable named
// by the key. If the variable is present in the environment the it
// returns the fallback value
func GetEnvVar(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
// GetEnvVarAsInt retrieves the value of the environment variable named
// by the key and returns its value or fallback
func GetEnvVarAsInt(key string, fallback int) int {
stringValue := GetEnvVar(key, strconv.Itoa(fallback))
if value, err := strconv.Atoi(stringValue); err == nil {
return value
}
return fallback
}