Skip to content

Commit

Permalink
feat: optimze shell command. Now it will execute and return directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Simon committed Feb 17, 2023
1 parent 379444d commit 0479419
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
3 changes: 0 additions & 3 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 0 additions & 35 deletions command/shell.go

This file was deleted.

17 changes: 15 additions & 2 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0479419

Please sign in to comment.