Skip to content

Commit

Permalink
Update exec() to send event objects, not output string
Browse files Browse the repository at this point in the history
  • Loading branch information
thesephist committed Dec 23, 2019
1 parent 6f18152 commit 822c2ed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
20 changes: 13 additions & 7 deletions pkg/ink/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,10 @@ func inkExec(ctx *Context, in []Value) (Value, error) {
return
}

_, err := evalInkFunction(stdoutFn, false, StringValue(""))
_, err := evalInkFunction(stdoutFn, false, CompositeValue{
"type": StringValue("data"),
"data": StringValue(""),
})
if err != nil {
ctx.LogErr(Err{
ErrRuntime,
Expand Down Expand Up @@ -1256,9 +1259,9 @@ func inkExec(ctx *Context, in []Value) (Value, error) {
cmd.Stdin = strings.NewReader(string(stdin))
cmd.Stdout = &stdout

sendErr := func() {
sendErr := func(msg string) {
ctx.ExecListener(func() {
_, err := evalInkFunction(stdoutFn, false, BooleanValue(false))
_, err := evalInkFunction(stdoutFn, false, errMsg(msg))
if err != nil {
ctx.LogErr(Err{
ErrRuntime,
Expand All @@ -1274,24 +1277,27 @@ func inkExec(ctx *Context, in []Value) (Value, error) {
cmdMutex.Unlock()

if err != nil {
sendErr()
sendErr(fmt.Sprintf("error starting command in exec(), %s", err.Error()))
return
}

err = cmd.Wait()
if err != nil {
sendErr()
sendErr(fmt.Sprintf("error waiting for command to exit in exec(), %s", err.Error()))
return
}

output, err := ioutil.ReadAll(&stdout)
if err != nil {
sendErr()
sendErr(fmt.Sprintf("error reading command output in exec(), %s", err.Error()))
return
}

ctx.ExecListener(func() {
_, err := evalInkFunction(stdoutFn, false, StringValue(output))
_, err := evalInkFunction(stdoutFn, false, CompositeValue{
"type": StringValue("data"),
"data": StringValue(output),
})
if err != nil {
ctx.LogErr(Err{
ErrRuntime,
Expand Down
24 changes: 16 additions & 8 deletions samples/exec.ink
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,38 @@ std := load('std')

log := std.log

handleExec := evt => evt.type :: {
'error' -> log(evt.message)
_ -> out(evt.data)
}

` runs without problems `
log('See: Hello, World!')
exec('echo', ['Hello, World!'], '', s => out(s))
exec('echo', ['Hello, World!'], '', handleExec)

` swallows stdout correctly `
log('See: nothing')
exec('echo', ['Hello, World!'], '', s => ())
exec('echo', ['Hello, World!'], '', evt => evt.type :: {
'error' -> log(evt.message)
_ -> ()
})

` sets args correctly `
log('See: Goodbye, World!')
exec('echo', ['Goodbye,', 'World!'], '', s => out(s))
exec('echo', ['Goodbye,', 'World!'], '', handleExec)

` runs commands at full paths `
log('See: Hello, Echo!')
exec('/bin/echo', ['Hello, Echo!'], '', s => out(s))
exec('/bin/echo', ['Hello, Echo!'], '', handleExec)

` interprets stdin correctly `
log('See: lovin-pasta')
exec('cat', [], 'lovin-pasta', s => out(s))
exec('cat', [], 'lovin-pasta', handleExec)

` closes immediately after exec `
(
log('Should close immediately after exec safely (may not run):')
close := exec('sleep', ['10'], '', s => log('Closed immediately after exec safely!'))
close := exec('sleep', ['10'], '', _ => log('Closed immediately after exec safely!'))
close()

` multiple closes do not fail `
Expand All @@ -39,7 +47,7 @@ exec('cat', [], 'lovin-pasta', s => out(s))
` closes during execution `
(
log('Should close during execution safely:')
close := exec('sleep', ['5'], '', s => log('Closed during execution safely!'))
close := exec('sleep', ['5'], '', _ => log('Closed during execution safely!'))
wait(1, () => close())

` multiple closes do not fail `
Expand All @@ -49,7 +57,7 @@ exec('cat', [], 'lovin-pasta', s => out(s))
` closes after execution `
(
log('Should exit safely, then close:')
close := exec('sleep', ['1'], '', s => log('Exited safely!'))
close := exec('sleep', ['1'], '', _ => log('Exited safely!'))
wait(2, () => (
close()
log('Closed!')
Expand Down

0 comments on commit 822c2ed

Please sign in to comment.