feat(env): clean env variable values

- Remove surrounding spaces
- Remove suffix new line characters
This commit is contained in:
Quentin McGaw
2022-05-27 17:22:50 +00:00
parent 7fd45cf17f
commit 364f9de756
18 changed files with 51 additions and 50 deletions

View File

@@ -2,13 +2,24 @@ package secrets
import (
"os"
"strings"
"github.com/qdm12/gluetun/internal/configuration/sources/files"
)
// getCleanedEnv returns an environment variable value with
// surrounding spaces and trailing new line characters removed.
func getCleanedEnv(envKey string) (value string) {
value = os.Getenv(envKey)
value = strings.TrimSpace(value)
value = strings.TrimSuffix(value, "\r\n")
value = strings.TrimSuffix(value, "\n")
return value
}
func readSecretFileAsStringPtr(secretPathEnvKey, defaultSecretPath string) (
stringPtr *string, err error) {
path := os.Getenv(secretPathEnvKey)
path := getCleanedEnv(secretPathEnvKey)
if path == "" {
path = defaultSecretPath
}
@@ -17,7 +28,7 @@ func readSecretFileAsStringPtr(secretPathEnvKey, defaultSecretPath string) (
func readSecretFileAsString(secretPathEnvKey, defaultSecretPath string) (
s string, err error) {
path := os.Getenv(secretPathEnvKey)
path := getCleanedEnv(secretPathEnvKey)
if path == "" {
path = defaultSecretPath
}