| 1 |
package shell |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"os/exec" |
| 7 |
) |
| 8 |
|
| 9 |
var Jobs chan JobFunction |
| 10 |
|
| 11 |
func init() { |
| 12 |
Jobs = make(chan JobFunction, 100) |
| 13 |
} |
| 14 |
|
| 15 |
// Jobs are the way plugins can run processes in the background |
| 16 |
// A job is simply a process that gets executed asynchronously |
| 17 |
// There are callbacks for when the job exits, when the job creates stdout |
| 18 |
// and when the job creates stderr |
| 19 |
|
| 20 |
// These jobs run in a separate goroutine but the lua callbacks need to be |
| 21 |
// executed in the main thread (where the Lua VM is running) so they are |
| 22 |
// put into the jobs channel which gets read by the main loop |
| 23 |
|
| 24 |
// JobFunction is a representation of a job (this data structure is what is loaded |
| 25 |
// into the jobs channel) |
| 26 |
type JobFunction struct { |
| 27 |
Function func(string, []any) |
| 28 |
Output string |
| 29 |
Args []any |
| 30 |
} |
| 31 |
|
| 32 |
// A CallbackFile is the data structure that makes it possible to catch stderr and stdout write events |
| 33 |
type CallbackFile struct { |
| 34 |
io.Writer |
| 35 |
|
| 36 |
callback func(string, []any) |
| 37 |
args []any |
| 38 |
} |
| 39 |
|
| 40 |
// Job stores the executing command for the job, and the stdin pipe |
| 41 |
type Job struct { |
| 42 |
*exec.Cmd |
| 43 |
Stdin io.WriteCloser |
| 44 |
} |
| 45 |
|
| 46 |
func (f *CallbackFile) Write(data []byte) (int, error) { |
| 47 |
// This is either stderr or stdout |
| 48 |
// In either case we create a new job function callback and put it in the jobs channel |
| 49 |
jobFunc := JobFunction{f.callback, string(data), f.args} |
| 50 |
Jobs <- jobFunc |
| 51 |
return f.Writer.Write(data) |
| 52 |
} |
| 53 |
|
| 54 |
// JobStart starts a shell command in the background with the given callbacks |
| 55 |
// It returns an *exec.Cmd as the job id |
| 56 |
func JobStart(cmd string, onStdout, onStderr, onExit func(string, []any), userargs ...any) *Job { |
| 57 |
return JobSpawn("sh", []string{"-c", cmd}, onStdout, onStderr, onExit, userargs...) |
| 58 |
} |
| 59 |
|
| 60 |
// JobSpawn starts a process with args in the background with the given callbacks |
| 61 |
// It returns an *exec.Cmd as the job id |
| 62 |
func JobSpawn(cmdName string, cmdArgs []string, onStdout, onStderr, onExit func(string, []any), userargs ...any) *Job { |
| 63 |
// Set up everything correctly if the functions have been provided |
| 64 |
proc := exec.Command(cmdName, cmdArgs...) |
| 65 |
var outbuf bytes.Buffer |
| 66 |
if onStdout != nil { |
| 67 |
proc.Stdout = &CallbackFile{&outbuf, onStdout, userargs} |
| 68 |
} else { |
| 69 |
proc.Stdout = &outbuf |
| 70 |
} |
| 71 |
if onStderr != nil { |
| 72 |
proc.Stderr = &CallbackFile{&outbuf, onStderr, userargs} |
| 73 |
} else { |
| 74 |
proc.Stderr = &outbuf |
| 75 |
} |
| 76 |
stdin, _ := proc.StdinPipe() |
| 77 |
|
| 78 |
go func() { |
| 79 |
// Run the process in the background and create the onExit callback |
| 80 |
proc.Run() |
| 81 |
if onExit != nil { |
| 82 |
jobFunc := JobFunction{onExit, outbuf.String(), userargs} |
| 83 |
Jobs <- jobFunc |
| 84 |
} |
| 85 |
}() |
| 86 |
|
| 87 |
return &Job{proc, stdin} |
| 88 |
} |
| 89 |
|
| 90 |
// JobStop kills a job |
| 91 |
func JobStop(j *Job) { |
| 92 |
j.Process.Kill() |
| 93 |
} |
| 94 |
|
| 95 |
// JobSend sends the given data into the job's stdin stream |
| 96 |
func JobSend(j *Job, data string) { |
| 97 |
j.Stdin.Write([]byte(data)) |
| 98 |
} |