-
Notifications
You must be signed in to change notification settings - Fork 15
/
shot.go
39 lines (35 loc) · 782 Bytes
/
shot.go
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
36
37
38
39
package main
import (
"github.com/yuin/gopher-lua"
"github.com/hymkor/expect/internal/go-console/output"
)
func shot(n int) ([]string, error) {
if !useStderrOnGetRecentOutput {
result, err := consoleoutput.GetRecentOutputByStdout(n)
if err == nil {
return result, nil
}
useStderrOnGetRecentOutput = true
}
return consoleoutput.GetRecentOutputByStderr(n)
}
func Shot(L *lua.LState) int {
n, ok := L.Get(-1).(lua.LNumber)
if !ok {
L.Push(lua.LNil)
L.Push(lua.LString("Expected a number"))
return 2
}
result, err := shot(int(n))
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
table := L.NewTable()
for i, line := range result {
L.SetTable(table, lua.LNumber(i+1), lua.LString(line))
}
L.Push(table)
return 1
}