argument 수식 처리
This commit is contained in:
@ -14,11 +14,14 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/Knetic/govaluate"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/metric"
|
||||
"repositories.action2quare.com/ayo/houston/shared"
|
||||
@ -237,6 +240,68 @@ func makeLogFilePrefix(meta *procmeta, index int) string {
|
||||
return path.Join(meta.verpath, "logs", fmt.Sprintf("%s_%d_%s", nameonly, index, ts))
|
||||
}
|
||||
|
||||
func evaluateExpression(expression string, params map[string]any) (any, error) {
|
||||
expression = strings.TrimSpace(expression)
|
||||
expr, err := govaluate.NewEvaluableExpression(expression)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return expr.Evaluate(params)
|
||||
}
|
||||
|
||||
func evaluateArgs(args []string, params map[string]any) ([]string, error) {
|
||||
re := regexp.MustCompile(`\$\(\((.*?)\)\)`)
|
||||
|
||||
for i, input := range args {
|
||||
matches := re.FindAllStringSubmatch(input, -1)
|
||||
if len(matches) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, match := range matches {
|
||||
if len(match) > 1 {
|
||||
expression := strings.TrimSpace(match[1])
|
||||
expr, err := govaluate.NewEvaluableExpression(expression)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := expr.Evaluate(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 원래 표현식을 결과로 대체
|
||||
input = strings.Replace(input, match[0], fmt.Sprintf("%v", result), -1)
|
||||
}
|
||||
}
|
||||
|
||||
args[i] = input
|
||||
}
|
||||
|
||||
return args, nil
|
||||
}
|
||||
|
||||
func parseEnv(input []string) map[string]any {
|
||||
output := make(map[string]any, len(input))
|
||||
for _, envkv := range input {
|
||||
kv := strings.SplitN(envkv, "=", 2)
|
||||
parsed, err := strconv.ParseInt(kv[1], 10, 0)
|
||||
if err == nil {
|
||||
output[kv[0]] = parsed
|
||||
} else {
|
||||
parsed, err := strconv.ParseFloat(kv[1], 32)
|
||||
if err == nil {
|
||||
output[kv[0]] = parsed
|
||||
} else {
|
||||
output[kv[0]] = kv[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
stdout, err := meta.cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
@ -423,8 +488,14 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
|
||||
go stdReader(meta.name, stdout, index)
|
||||
|
||||
logger.Println("startChildProcess :", meta.cmd.Args)
|
||||
meta.cmd.Env = append(os.Environ(), fmt.Sprintf("HOUSTON_SIBLIING_INDEX=%d", index))
|
||||
meta.cmd.Args, err = evaluateArgs(meta.cmd.Args, parseEnv(meta.cmd.Env))
|
||||
if err != nil {
|
||||
logger.Println("evaluateArgs failed :", err)
|
||||
return err
|
||||
}
|
||||
logger.Println("startChildProcess :", meta.cmd.Args)
|
||||
|
||||
err = meta.cmd.Start()
|
||||
if err == nil {
|
||||
logger.Println("process index, pid =", index, meta.cmd.Process.Pid)
|
||||
|
||||
Reference in New Issue
Block a user