From 0479419d5829c97226cf5c63571ae15982d3baff Mon Sep 17 00:00:00 2001 From: Fabian Simon Date: Fri, 17 Feb 2023 07:49:23 +0100 Subject: [PATCH] feat: optimze shell command. Now it will execute and return directly --- command/commands.go | 3 --- command/shell.go | 35 ----------------------------------- interpreter/interpreter.go | 17 +++++++++++++++-- 3 files changed, 15 insertions(+), 40 deletions(-) delete mode 100644 command/shell.go diff --git a/command/commands.go b/command/commands.go index ce12b14..0f734b7 100644 --- a/command/commands.go +++ b/command/commands.go @@ -53,9 +53,6 @@ func (c *CommandHandler) registerStandardHandler() { c.RegisterHandler(&IncludeCommand{ handler: *c, }) - c.RegisterHandler(&ShellCommand{ - handler: *c, - }) } func (c *CommandHandler) ExecuteVariablesCommands(variabels map[string]any) (map[string]any, error) { diff --git a/command/shell.go b/command/shell.go deleted file mode 100644 index c29af8b..0000000 --- a/command/shell.go +++ /dev/null @@ -1,35 +0,0 @@ -package command - -import ( - "bytes" - "fmt" - "log" - "os/exec" - "text/template" -) - -type ShellCommand struct { - handler CommandHandler -} - -func (i *ShellCommand) Name() string { - return "shell" -} - -func (i *ShellCommand) GetFuncMap() template.FuncMap { - return template.FuncMap{ - i.Name(): func(name string) string { - return fmt.Sprintf("__%s_%s=%s", i.handler.appName, i.Name(), name) - }, - } -} - -func (i *ShellCommand) Execute(cmd string, makefile MakeStruct, listType CommandListType) ([]string, error) { - cmdRunner := exec.Command("/bin/sh", "-c", cmd) - buf := new(bytes.Buffer) - cmdRunner.Stdout = buf - cmdRunner.Stderr = log.Writer() - - err := cmdRunner.Run() - return []string{buf.String()}, err -} diff --git a/interpreter/interpreter.go b/interpreter/interpreter.go index e2a442d..f8846ca 100644 --- a/interpreter/interpreter.go +++ b/interpreter/interpreter.go @@ -108,6 +108,7 @@ func (r *Interpreter) GetExecuteTemplate(file string, extraVariables map[string] varStr, err := r.getParsedTemplate("gomake_vars", varCommandArr[0], TemplateData{Env: env, Vars: tempVar, Colors: getColorKeyMap()}) + log.Println(string(varStr)) if err != nil { return nil, nil, err } @@ -304,8 +305,20 @@ func (r *Interpreter) getParsedTemplate(templateName, tmpl string, data Template var buf bytes.Buffer funcMap := r.cmdHandler.GetFuncMap() + funcMap["shell"] = func(cmd string) string { + cmdRunner := exec.Command("/bin/sh", "-c", cmd) + buf := new(bytes.Buffer) + cmdRunner.Stdout = buf + cmdRunner.Stderr = log.Writer() + + err := cmdRunner.Run() + if err != nil { + log.Panic(err) + } + return buf.String() + } funcMap["includeFile"] = func(name string) string { - files, err := GetContent(name) + files, err := GetContents(name) if err != nil { log.Panic(err) } @@ -337,7 +350,7 @@ func (r *Interpreter) getParsedTemplate(templateName, tmpl string, data Template return buf.Bytes(), nil } -func GetContent(path string) ([]string, error) { +func GetContents(path string) ([]string, error) { var contents []string if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") { resp, err := http.Get(path)