feat: allow hilbish.run to take a table of files to use for command output

pull/291/head
sammyette 2024-04-27 12:41:23 -04:00
parent cee5129e80
commit 960badb5df
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 69 additions and 24 deletions

62
api.go
View File

@ -27,6 +27,7 @@ import (
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib" "github.com/arnodel/golua/lib/packagelib"
"github.com/arnodel/golua/lib/iolib"
"github.com/maxlandon/readline" "github.com/maxlandon/readline"
"mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/interp"
) )
@ -152,12 +153,13 @@ func unsetVimMode() {
util.SetField(l, hshMod, "vimMode", rt.NilValue) util.SetField(l, hshMod, "vimMode", rt.NilValue)
} }
// run(cmd, returnOut) -> exitCode (number), stdout (string), stderr (string) // run(cmd, streams) -> exitCode (number), stdout (string), stderr (string)
// Runs `cmd` in Hilbish's shell script interpreter. // Runs `cmd` in Hilbish's shell script interpreter.
// #param cmd string // #param cmd string
// #param returnOut boolean If this is true, the function will return the standard output and error of the command instead of printing it. // #param returnOut boolean If this is true, the function will return the standard output and error of the command instead of printing it.
// #returns number, string, string // #returns number, string, string
func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// TODO: ON BREAKING RELEASE, DO NOT ACCEPT `streams` AS A BOOLEAN.
if err := c.Check1Arg(); err != nil { if err := c.Check1Arg(); err != nil {
return nil, err return nil, err
} }
@ -166,20 +168,57 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return nil, err return nil, err
} }
var strms *streams
var terminalOut bool var terminalOut bool
if len(c.Etc()) != 0 { if len(c.Etc()) != 0 {
tout := c.Etc()[0] tout := c.Etc()[0]
termOut, ok := tout.TryBool()
terminalOut = termOut var ok bool
terminalOut, ok = tout.TryBool()
if !ok { if !ok {
return nil, errors.New("bad argument to run (expected boolean, got " + tout.TypeName() + ")") luastreams, ok := tout.TryTable()
if !ok {
return nil, errors.New("bad argument to run (expected boolean or table, got " + tout.TypeName() + ")")
}
var stdoutStream, stderrStream *iolib.File
stdoutstrm := luastreams.Get(rt.StringValue("stdout"))
if !stdoutstrm.IsNil() {
f, ok := iolib.ValueToFile(stdoutstrm)
if !ok {
return nil, errors.New("bad argument to run streams table (expected file, got " + stdoutstrm.TypeName() + ")")
}
stdoutStream = f
}
stderrstrm := luastreams.Get(rt.StringValue("stderr"))
if !stderrstrm.IsNil() {
f, ok := iolib.ValueToFile(stderrstrm)
if !ok {
return nil, errors.New("bad argument to run streams table (expected file, got " + stderrstrm.TypeName() + ")")
}
stderrStream = f
}
strms = &streams{
stdout: stdoutStream.Handle(),
stderr: stderrStream.Handle(),
} }
} else { } else {
terminalOut = true if !terminalOut {
strms = &streams{
stdout: new(bytes.Buffer),
stderr: new(bytes.Buffer),
}
}
}
} }
var exitcode uint8 var exitcode uint8
stdout, stderr, err := execCommand(cmd, terminalOut) stdout, stderr, err := execCommand(cmd, strms)
if code, ok := interp.IsExitStatus(err); ok { if code, ok := interp.IsExitStatus(err); ok {
exitcode = code exitcode = code
@ -187,11 +226,12 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
exitcode = 1 exitcode = 1
} }
stdoutStr := "" var stdoutStr, stderrStr string
stderrStr := "" if stdoutBuf, ok := stdout.(*bytes.Buffer); ok {
if !terminalOut { stdoutStr = stdoutBuf.String()
stdoutStr = stdout.(*bytes.Buffer).String() }
stderrStr = stderr.(*bytes.Buffer).String() if stderrBuf, ok := stderr.(*bytes.Buffer); ok {
stderrStr = stderrBuf.String()
} }
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitcode)), rt.StringValue(stdoutStr), rt.StringValue(stderrStr)), nil return c.PushingNext(t.Runtime, rt.IntValue(int64(exitcode)), rt.StringValue(stdoutStr), rt.StringValue(stderrStr)), nil

29
exec.go
View File

@ -28,6 +28,12 @@ var errNotExec = errors.New("not executable")
var errNotFound = errors.New("not found") var errNotFound = errors.New("not found")
var runnerMode rt.Value = rt.StringValue("hybrid") var runnerMode rt.Value = rt.StringValue("hybrid")
type streams struct {
stdout io.Writer
stderr io.Writer
stdin io.Reader
}
type execError struct{ type execError struct{
typ string typ string
cmd string cmd string
@ -236,7 +242,7 @@ func handleSh(cmdString string) (input string, exitCode uint8, cont bool, runErr
} }
func execSh(cmdString string) (string, uint8, bool, error) { func execSh(cmdString string) (string, uint8, bool, error) {
_, _, err := execCommand(cmdString, true) _, _, err := execCommand(cmdString, nil)
if err != nil { if err != nil {
// If input is incomplete, start multiline prompting // If input is incomplete, start multiline prompting
if syntax.IsIncomplete(err) { if syntax.IsIncomplete(err) {
@ -257,7 +263,7 @@ func execSh(cmdString string) (string, uint8, bool, error) {
} }
// Run command in sh interpreter // Run command in sh interpreter
func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) { func execCommand(cmd string, strms *streams) (io.Writer, io.Writer, error) {
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "") file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -265,15 +271,14 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
runner, _ := interp.New() runner, _ := interp.New()
var stdout io.Writer if strms == nil {
var stderr io.Writer strms = &streams{
if terminalOut { stdout: os.Stdout,
interp.StdIO(os.Stdin, os.Stdout, os.Stderr)(runner) stderr: os.Stderr,
} else {
stdout = new(bytes.Buffer)
stderr = new(bytes.Buffer)
interp.StdIO(os.Stdin, stdout, stderr)(runner)
} }
}
interp.StdIO(os.Stdin, strms.stdout, strms.stderr)(runner)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
printer := syntax.NewPrinter() printer := syntax.NewPrinter()
@ -292,11 +297,11 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
interp.ExecHandler(execHandle(bg))(runner) interp.ExecHandler(execHandle(bg))(runner)
err = runner.Run(context.TODO(), stmt) err = runner.Run(context.TODO(), stmt)
if err != nil { if err != nil {
return stdout, stderr, err return strms.stdout, strms.stderr, err
} }
} }
return stdout, stderr, nil return strms.stdout, strms.stderr, nil
} }
func execHandle(bg bool) interp.ExecHandlerFunc { func execHandle(bg bool) interp.ExecHandlerFunc {