mirror of
https://github.com/lancatlin/mdsh.git
synced 2026-11-05 19:30:12 +08:00
chore: Move command execution code to separate file
This commit is contained in:
1 parent
88d6d98282
commit
08d0a094b2
2 files changed
+57
-51
No files matched your search
@@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (ctx TemplateContext) run(command string) (string, error) {
|
||||||
|
cmd := exec.Command("sh", "-c", command)
|
||||||
|
var stdout bytes.Buffer
|
||||||
|
cmd.Stdout = &stdout
|
||||||
|
cmd.Stderr = &stdout
|
||||||
|
for k, v := range ctx {
|
||||||
|
cmd.Env = append(cmd.Env, k+"="+v)
|
||||||
|
}
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.New(stdout.String())
|
||||||
|
}
|
||||||
|
return stdout.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx TemplateContext) raw(command string) string {
|
||||||
|
out, err := ctx.run(command)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return "error"
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx TemplateContext) codeBlock(command string) string {
|
||||||
|
out, err := ctx.run(command)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("> Failed to execute the command:\n> `%s`\n> %s\n", command, err.Error())
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("\n```\n%s\n```\n", out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx TemplateContext) inline(command string) string {
|
||||||
|
out, err := ctx.run(command)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("`Failed to execute the command: '%s'. %s`", command, err.Error())
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("`%s`", strings.Trim(out, "\n "))
|
||||||
|
}
|
||||||
@@ -2,63 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (ctx TemplateContext) run(command string) (string, error) {
|
|
||||||
cmd := exec.Command("sh", "-c", command)
|
|
||||||
var stdout bytes.Buffer
|
|
||||||
cmd.Stdout = &stdout
|
|
||||||
cmd.Stderr = &stdout
|
|
||||||
for k, v := range ctx {
|
|
||||||
cmd.Env = append(cmd.Env, k+"="+v)
|
|
||||||
}
|
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.New(stdout.String())
|
|
||||||
}
|
|
||||||
return stdout.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx TemplateContext) raw(command string) string {
|
|
||||||
out, err := ctx.run(command)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return "error"
|
|
||||||
}
|
|
||||||
return strings.TrimSpace(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx TemplateContext) codeBlock(command string) string {
|
|
||||||
out, err := ctx.run(command)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Sprintf("> Failed to execute the command:\n> `%s`\n> %s\n", command, err.Error())
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("\n```\n%s\n```\n", out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx TemplateContext) inline(command string) string {
|
|
||||||
out, err := ctx.run(command)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Sprintf("`Failed to execute the command: '%s'. %s`", command, err.Error())
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("`%s`", strings.Trim(out, "\n "))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx TemplateContext) funcMap() template.FuncMap {
|
|
||||||
return template.FuncMap{
|
|
||||||
"sh": ctx.inline,
|
|
||||||
"shell": ctx.codeBlock,
|
|
||||||
"raw": ctx.raw,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ctx TemplateContext) render(tmpl string, output io.Writer) error {
|
func (ctx TemplateContext) render(tmpl string, output io.Writer) error {
|
||||||
t, err := template.New("foo").Funcs(ctx.funcMap()).Parse(tmpl)
|
t, err := template.New("foo").Funcs(ctx.funcMap()).Parse(tmpl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -71,6 +20,14 @@ func (ctx TemplateContext) render(tmpl string, output io.Writer) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx TemplateContext) funcMap() template.FuncMap {
|
||||||
|
return template.FuncMap{
|
||||||
|
"sh": ctx.inline,
|
||||||
|
"shell": ctx.codeBlock,
|
||||||
|
"raw": ctx.raw,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (ctx TemplateContext) OutputFile(meta TemplateMeta) (output io.WriteCloser, err error) {
|
func (ctx TemplateContext) OutputFile(meta TemplateMeta) (output io.WriteCloser, err error) {
|
||||||
if meta.Output == "" {
|
if meta.Output == "" {
|
||||||
return os.Stdout, nil
|
return os.Stdout, nil
|
||||||
|
|||||||
Reference in new issue
Block a user