aboutsummaryrefslogtreecommitdiff
path: root/internal/gluaexpect/expect.go
blob: b7e57d46fcde989313c978f01607aac14d742723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package gluaexpect

import (
	"github.com/ThomasRooney/gexpect"
	luar "github.com/layeh/gopher-luar"
	lua "github.com/yuin/gopher-lua"
)

func Preload(L *lua.LState) {
	L.PreloadModule("expect", Loader)
}

// Loader is the module loader function.
func Loader(L *lua.LState) int {
	mod := L.SetFuncs(L.NewTable(), api)
	L.Push(mod)
	return 1
}

var api = map[string]lua.LGFunction{
	"spawn": spawn,
}

func spawn(L *lua.LState) int {
	cmd := L.CheckString(1)
	child, err := gexpect.Spawn(cmd)
	if err != nil {
		L.Push(lua.LNil)
		L.Push(lua.LString(err.Error()))
		return 2
	}

	L.Push(luar.New(L, child))
	return 1
}