From d70a2fe63d360d00ffb0ca2fb6e13191d3855eb0 Mon Sep 17 00:00:00 2001 From: 10sr <8slashes+git@gmail.com> Date: Wed, 16 Nov 2016 14:06:12 +0900 Subject: [PATCH 1/2] Add plugin function JobSpawn --- cmd/micro/job.go | 11 ++++++++--- cmd/micro/micro.go | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/micro/job.go b/cmd/micro/job.go index 316aa636..829954b2 100644 --- a/cmd/micro/job.go +++ b/cmd/micro/job.go @@ -40,15 +40,20 @@ func (f *CallbackFile) Write(data []byte) (int, error) { return f.Writer.Write(data) } -// JobStart starts a process in the background with the given callbacks +// JobStart starts a shell command in the background with the given callbacks // It returns an *exec.Cmd as the job id func JobStart(cmd string, onStdout, onStderr, onExit string, userargs ...string) *exec.Cmd { split := strings.Split(cmd, " ") - args := split[1:] + cmdArgs := split[1:] cmdName := split[0] + return JobSpawn(cmdName, cmdArgs, onStdout, onStderr, onExit, userargs...) +} +// JobSpawn starts a process with args in the background with the given callbacks +// It returns an *exec.Cmd as the job id +func JobSpawn(cmdName string, cmdArgs []string, onStdout, onStderr, onExit string, userargs ...string) *exec.Cmd { // Set up everything correctly if the functions have been provided - proc := exec.Command(cmdName, args...) + proc := exec.Command(cmdName, cmdArgs...) var outbuf bytes.Buffer if onStdout != "" { proc.Stdout = &CallbackFile{&outbuf, LuaFunctionJob(onStdout), userargs} diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index f3c1fbd7..289f3783 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -340,6 +340,7 @@ func main() { // Used for asynchronous jobs L.SetGlobal("JobStart", luar.New(L, JobStart)) + L.SetGlobal("JobSpawn", luar.New(L, JobSpawn)) L.SetGlobal("JobSend", luar.New(L, JobSend)) L.SetGlobal("JobStop", luar.New(L, JobStop)) From 856acf4a518e6f34da5a6c5040eebf956038734d Mon Sep 17 00:00:00 2001 From: 10sr <8slashes+git@gmail.com> Date: Wed, 16 Nov 2016 14:13:17 +0900 Subject: [PATCH 2/2] Update plugins.md --- runtime/help/plugins.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index 5a35a56a..eb616366 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -90,12 +90,16 @@ as Go's GOOS variable, so `darwin`, `windows`, `linux`, `freebsd`...) * `ByteOffset(loc Loc, buf *Buffer) int`: exactly like `ToCharPos` except it it counts bytes instead of runes. -* `JobStart(cmd string, onStdout, onStderr, onExit string, userargs ...string)`: - Starts running the given shell command in the background. `onStdout` `onStderr` and `onExit` +* `JobSpawn(cmdName string, cmdArgs []string, onStdout, onStderr, onExit string, userargs ...string)`: + Starts running the given process in the background. `onStdout` `onStderr` and `onExit` are callbacks to lua functions which will be called when the given actions happen to the background process. `userargs` are the arguments which will get passed to the callback functions +* `JobStart(cmd string, onStdout, onStderr, onExit string, userargs ...string)`: + Starts running the given shell command in the background. + This function is a shorthand for `JobSpawn`. + * `JobSend(cmd *exec.Cmd, data string)`: send a string into the stdin of the job process * `JobStop(cmd *exec.Cmd)`: kill a job