Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to exec arbitrary commands from templates. #847

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions helpers/execembed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package helpers

import (
"bytes"
"os/exec"

jww "github.com/spf13/jwalterweatherman"
)

func Exec(args ...string) string {
var out bytes.Buffer
var stderr bytes.Buffer
var arg []string
if len(args) == 0 {
jww.ERROR.Print("Nothing to execute")
return "Nothing to execute"
}
name := args[0]
if len(args) > 1 {
arg = args[1 : len(args)-1]
}
cmd := exec.Command(name, arg...)
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
jww.ERROR.Print("Error executing", err, stderr.String())
return stderr.String()
}
return out.String()
}
5 changes: 5 additions & 0 deletions tpl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,10 @@ func Highlight(in interface{}, lang string) template.HTML {
return template.HTML(helpers.Highlight(html.UnescapeString(str), lang))
}

func Exec(args ...string) template.HTML {
return template.HTML(helpers.Exec(args...))
}

func Markdownify(text string) template.HTML {
return template.HTML(helpers.RenderBytes(helpers.RenderingContext{Content: []byte(text), PageFmt: "markdown"}))
}
Expand Down Expand Up @@ -1275,6 +1279,7 @@ func init() {
"delimit": Delimit,
"sort": Sort,
"highlight": Highlight,
"exec": Exec,
"add": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '+') },
"sub": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '-') },
"div": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '/') },
Expand Down