forked from toolshed/abra
proper env modifiers support
This implements proper modifier support in the env file using this new fork of the godotenv library. The modifier implementation is quite basic for but can be improved later if needed. See this commit for the actual implementation. Because we are now using proper modifer parsing, it does not affect the parsing of value, so this is possible again: ``` MY_VAR="#foo" ``` Closes coop-cloud/organising#535
This commit is contained in:
@ -12,7 +12,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/Autonomic-Cooperative/godotenv"
|
||||
"git.coopcloud.tech/coop-cloud/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@ -55,45 +55,34 @@ func GetServers() ([]string, error) {
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
// ReadEnvOptions modifies the ReadEnv processing of env vars.
|
||||
type ReadEnvOptions struct {
|
||||
IncludeModifiers bool
|
||||
}
|
||||
|
||||
// ContainsEnvVarModifier determines if an env var contains a modifier.
|
||||
func ContainsEnvVarModifier(envVar string) bool {
|
||||
for _, mod := range envVarModifiers {
|
||||
if strings.Contains(envVar, fmt.Sprintf("%s=", mod)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ReadEnv loads an app envivornment into a map.
|
||||
func ReadEnv(filePath string, opts ReadEnvOptions) (AppEnv, error) {
|
||||
func ReadEnv(filePath string) (AppEnv, error) {
|
||||
var envVars AppEnv
|
||||
|
||||
envVars, err := godotenv.Read(filePath)
|
||||
envVars, _, err := godotenv.Read(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// for idx, envVar := range envVars {
|
||||
// if strings.Contains(envVar, "#") {
|
||||
// if opts.IncludeModifiers && ContainsEnvVarModifier(envVar) {
|
||||
// continue
|
||||
// }
|
||||
// vals := strings.Split(envVar, "#")
|
||||
// envVars[idx] = strings.TrimSpace(vals[0])
|
||||
// }
|
||||
// }
|
||||
|
||||
logrus.Debugf("read %s from %s", envVars, filePath)
|
||||
|
||||
return envVars, nil
|
||||
}
|
||||
|
||||
// ReadEnv loads an app envivornment and their modifiers in two different maps.
|
||||
func ReadEnvWithModifiers(filePath string) (AppEnv, AppModifiers, error) {
|
||||
var envVars AppEnv
|
||||
|
||||
envVars, mods, err := godotenv.Read(filePath)
|
||||
if err != nil {
|
||||
return nil, mods, err
|
||||
}
|
||||
|
||||
logrus.Debugf("read %s from %s", envVars, filePath)
|
||||
|
||||
return envVars, mods, nil
|
||||
}
|
||||
|
||||
// ReadServerNames retrieves all server names.
|
||||
func ReadServerNames() ([]string, error) {
|
||||
serverNames, err := GetAllFoldersInDirectory(SERVERS_DIR)
|
||||
@ -227,7 +216,7 @@ func CheckEnv(app App) ([]EnvVar, error) {
|
||||
return envVars, err
|
||||
}
|
||||
|
||||
envSample, err := ReadEnv(envSamplePath, ReadEnvOptions{})
|
||||
envSample, err := ReadEnv(envSamplePath)
|
||||
if err != nil {
|
||||
return envVars, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user