WIP: updated log level on some log entries and added environment variable expansion for command line and its arguments

This commit is contained in:
2025-05-26 17:17:20 +10:00
parent 2474d3cddf
commit 1827cf2e3e
14 changed files with 242 additions and 116 deletions
+14 -74
View File
@@ -4,19 +4,9 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"regexp"
"strings"
"gitea.suyono.dev/suyono/wingmate"
wminit "gitea.suyono.dev/suyono/wingmate/init"
)
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
wmenv "gitea.suyono.dev/suyono/wingmate/task/env"
)
type config interface {
@@ -227,6 +217,7 @@ func (t *ServiceTask) Name() string {
}
func (t *ServiceTask) prepareCommandLine() []string {
//TODO: is this method used somewhere? if not, can I remove this?
if len(t.cmdLine) > 0 {
return t.cmdLine
}
@@ -288,19 +279,25 @@ func (t *ServiceTask) UtilDepCheck() error {
return nil
}
func (t *ServiceTask) Command() string {
func (t *ServiceTask) Command(env ...string) string {
if len(env) > 0 {
return wmenv.ExpandEnv(env, []string{t.cmdLine[0]})[0]
}
return t.cmdLine[0]
}
func (t *ServiceTask) Arguments() []string {
func (t *ServiceTask) Arguments(env ...string) []string {
if len(t.cmdLine) == 1 {
return nil
}
retval := make([]string, len(t.cmdLine)-1)
copy(retval, t.cmdLine[1:])
argCopy := make([]string, len(t.cmdLine)-1)
copy(argCopy, t.cmdLine[1:])
if len(env) > 0 {
argCopy = wmenv.ExpandEnv(env, argCopy)
}
return retval
return argCopy
}
func (t *ServiceTask) EnvLen() int {
@@ -314,7 +311,7 @@ func (t *ServiceTask) Environ() []string {
}
func (t *ServiceTask) PatchEnv(env []string) []string {
return patchEnv(env, t.environ)
return wmenv.PatchEnv(env, t.environ)
}
func (t *ServiceTask) Setsid() bool {
@@ -385,60 +382,3 @@ func validate(validators ...func() error) error {
}
return nil
}
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 {
if envEsc.MatchString(rep) {
return rep
}
if envName := envRefExplicit.FindStringSubmatch(rep); envName != nil && envName[1] != "" {
exVal, ok := tMap[envName[1]]
if !ok {
return ""
}
return exVal
}
if envName := envRef.FindStringSubmatch(rep); envName != nil && envName[1] != "" {
exVal, ok := tMap[envName[1]]
if !ok {
return ""
}
return exVal
}
return 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
}