From aa97885870aee1bef1d6142fe7fe6bc0af90b21b Mon Sep 17 00:00:00 2001 From: Yves Brissaud Date: Fri, 11 Oct 2024 09:43:27 +0200 Subject: [PATCH] pre-run shell scripts This avoid to run multiple times the same script Signed-off-by: Yves Brissaud --- runkit/run.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/runkit/run.go b/runkit/run.go index 08e7113..315632f 100644 --- a/runkit/run.go +++ b/runkit/run.go @@ -102,6 +102,15 @@ func (r *Runnable) cleanup() func() { } func (r *Runnable) compute() error { + shells := map[string]string{} + for k, v := range r.Action.Shell { + out, err := sh(context.Background(), v) + if err != nil { + return fmt.Errorf("could not run shell script %s: %w", k, err) + } + shells[k] = out + } + tmpl, err := template.New("runx").Funcs(template.FuncMap{ "env": func(envName string) string { return r.data.Env[envName] @@ -110,11 +119,11 @@ func (r *Runnable) compute() error { return r.data.Opts[optName] }, "sh": func(cmdName string) (string, error) { - cmd, ok := r.Action.Shell[cmdName] + v, ok := shells[cmdName] if !ok { return "", fmt.Errorf("shell command %q not found", cmdName) } - return sh(context.Background(), cmd) + return v, nil }, }).Parse(r.Action.Command) if err != nil {