WIP: unit test done, need further test
This commit is contained in:
+66
-31
@@ -13,7 +13,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
envRef = regexp.MustCompile(`\$([a-zA-Z][a-zA-Z0-9]*)`)
|
||||
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
|
||||
)
|
||||
|
||||
type config interface {
|
||||
@@ -75,7 +78,7 @@ func (ts *Tasks) Crones() []wminit.CronTask {
|
||||
func (ts *Tasks) Get(name string) (wminit.Task, error) {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return nil, nil
|
||||
// return nil, nil
|
||||
}
|
||||
|
||||
type ServiceTask struct {
|
||||
@@ -311,32 +314,7 @@ func (t *ServiceTask) Environ() []string {
|
||||
}
|
||||
|
||||
func (t *ServiceTask) PatchEnv(env []string) []string {
|
||||
tMap := make(map[string]string)
|
||||
for _, e := range env {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
tMap[key] = value
|
||||
}
|
||||
|
||||
for _, e := range t.environ {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.ContainsAny(key, "$") {
|
||||
continue
|
||||
}
|
||||
|
||||
//envRef.FindAllString()
|
||||
_ = value
|
||||
}
|
||||
|
||||
return nil
|
||||
return patchEnv(env, t.environ)
|
||||
}
|
||||
|
||||
func (t *ServiceTask) Setsid() bool {
|
||||
@@ -358,19 +336,19 @@ func (t *ServiceTask) WorkingDir() string {
|
||||
func (t *ServiceTask) Status() wminit.TaskStatus {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return nil
|
||||
// return nil
|
||||
}
|
||||
|
||||
func (t *ServiceTask) AutoStart() bool {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return false
|
||||
// return false
|
||||
}
|
||||
|
||||
func (t *ServiceTask) AutoRestart() bool {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return false
|
||||
// return false
|
||||
}
|
||||
|
||||
func (t *ServiceTask) StartSecs() uint {
|
||||
@@ -407,3 +385,60 @@ 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user