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