/
executor.go
128 lines (102 loc) · 2.43 KB
/
executor.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package watcher
import (
"bufio"
"io"
"io/ioutil"
"os/exec"
"strings"
"sync"
)
// Executor provides a minimal workflow to run commands.
type Executor interface {
// Running must return true when the command is still running.
Running() bool
// Exec takes command program with first param, then arguments.
Exec(params ...string) error
}
// NewPrintExec only prints to stdout the full file path that triggered an
// event so you can pipe the output and do whatever you want with it.
func NewPrintExec(output io.Writer) Executor {
return &printExec{output: output}
}
type printExec struct {
output io.Writer
}
func (e *printExec) Running() bool {
return false
}
func (e *printExec) Exec(params ...string) error {
_, err := e.output.Write([]byte(strings.Join(params, " ") + "\n"))
return err
}
// NewUnixShellExec returns an executor that will run your command through
// /bin/sh -c "<command>". Your command will be quoted before to avoid any
// problems.
func NewUnixShellExec(output io.Writer) Executor {
return &unixShellExec{
rawExec: NewRawExec(output),
}
}
type unixShellExec struct {
rawExec Executor
}
func (e *unixShellExec) Exec(params ...string) error {
return e.rawExec.Exec(append([]string{"/bin/sh", "-c"}, params...)...)
}
func (e *unixShellExec) Running() bool {
return e.rawExec.Running()
}
// NewRawExec will run your command without shell.
// FIXME: commands with arguments are NOT supported for now.
func NewRawExec(output io.Writer) Executor {
return &rawExec{output: output}
}
type rawExec struct {
lock sync.RWMutex
executing bool
output io.Writer
}
func (e *rawExec) setExecuting(on bool) {
e.lock.Lock()
defer e.lock.Unlock()
e.executing = on
}
func (e *rawExec) Running() bool {
e.lock.Lock()
defer e.lock.Unlock()
return e.executing
}
func (e *rawExec) Exec(params ...string) error {
e.setExecuting(true)
defer e.setExecuting(false)
rp, wp := io.Pipe()
var cmd *exec.Cmd
var execError error
cmd = exec.Command(params[0], params[1:]...)
cmd.Stdout = wp
cmd.Stderr = wp
execFinished := make(chan bool, 1)
go func() {
if err := cmd.Run(); err != nil {
execError = err
}
wp.Close()
execFinished <- true
}()
reader := bufio.NewReader(rp)
for {
b, err := reader.ReadBytes('\n')
if len(b) > 0 {
e.output.Write(b)
}
if err != nil {
break
}
}
if reader.Buffered() > 0 {
b, _ := ioutil.ReadAll(reader)
e.output.Write(b)
}
<-execFinished
return execError
}