WIP: updated log level on some log entries and added environment variable expansion for command line and its arguments
This commit is contained in:
97
task/env/env.go
vendored
Normal file
97
task/env/env.go
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.suyono.dev/suyono/wingmate"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
envCapture = regexp.MustCompile(`\$+[a-zA-Z_][a-zA-Z0-9_]*|\$+{[a-zA-Z_][a-zA-Z0-9_]*}`)
|
||||
envEsc = regexp.MustCompile(`^\$\$+[^$]+$`) // escaped, starts with two or more $ character
|
||||
envRef = regexp.MustCompile(`^\$([^$]+)$`) // capture the variable name
|
||||
envRefExplicit = regexp.MustCompile(`^\${([^$]+)}$`) // capture the variable name - explicit
|
||||
)
|
||||
|
||||
func expandEnv(envMap map[string]string, input string) string {
|
||||
if envEsc.MatchString(input) {
|
||||
return input
|
||||
}
|
||||
|
||||
if envName := envRefExplicit.FindStringSubmatch(input); envName != nil && envName[1] != "" {
|
||||
exVal, ok := envMap[envName[1]]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return exVal
|
||||
}
|
||||
|
||||
if envName := envRef.FindStringSubmatch(input); envName != nil && envName[1] != "" {
|
||||
exVal, ok := envMap[envName[1]]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return exVal
|
||||
}
|
||||
return input
|
||||
|
||||
}
|
||||
|
||||
func PatchEnv(existing, new []string) []string {
|
||||
tMap := make(map[string]string)
|
||||
for _, e := range existing {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
tMap[key] = value
|
||||
}
|
||||
|
||||
for _, e := range new {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.ContainsAny(key, "$") {
|
||||
wingmate.Log().Error().Err(fmt.Errorf("variable name contains $")).Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
|
||||
value = envCapture.ReplaceAllStringFunc(value, func(rep string) string {
|
||||
return expandEnv(tMap, rep)
|
||||
})
|
||||
|
||||
tMap[key] = value
|
||||
}
|
||||
|
||||
outEnv := make([]string, 0, len(existing))
|
||||
for key, val := range tMap {
|
||||
outEnv = append(outEnv, fmt.Sprintf("%s=%s", key, val))
|
||||
}
|
||||
|
||||
return outEnv
|
||||
}
|
||||
|
||||
func ExpandEnv(env []string, input []string) []string {
|
||||
envMap := make(map[string]string)
|
||||
for _, e := range env {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing bad environment:", e)
|
||||
continue
|
||||
}
|
||||
envMap[key] = value
|
||||
}
|
||||
|
||||
for i, s := range input {
|
||||
s = envCapture.ReplaceAllStringFunc(s, func(rep string) string {
|
||||
return expandEnv(envMap, rep)
|
||||
})
|
||||
input[i] = s
|
||||
}
|
||||
return input
|
||||
}
|
||||
Reference in New Issue
Block a user