-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
66 lines (55 loc) · 1.17 KB
/
cmd.go
File metadata and controls
66 lines (55 loc) · 1.17 KB
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
package simpleexec
import (
"fmt"
"os"
"os/exec"
"syscall"
"github.com/google/shlex"
)
type Cmd struct {
Parent *Cmd
*exec.Cmd
}
func ParseCmd(command string) *Cmd {
cmdArray, err := shlex.Split(command)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil
}
eCmd := exec.Command(cmdArray[0], cmdArray[1:]...)
eCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd := &Cmd{}
cmd.Cmd = eCmd
return cmd
}
func (cmd *Cmd) Pipe(command string) *Cmd {
pCmd := ParseCmd(command)
// Although cmd.StdoutPipe() returns errors, the documentation does
// not say _when_ said errors occur
cOut, _ := cmd.StdoutPipe()
pCmd.Stdin = cOut
pCmd.Parent = cmd
return pCmd
}
func (cmd *Cmd) Start() (err error) {
err = cmd.Cmd.Start()
if err != nil {
return
}
if cmd.Parent != nil {
err = cmd.Parent.Start()
}
return
}
// The logic in this function is not perfect
// We probably need to handle parent errors and child errors separately
func (cmd *Cmd) Wait() (err error) {
if cmd.Parent != nil {
err = cmd.Parent.Wait()
}
_err := cmd.Cmd.Wait()
if err != nil || _err != nil {
err = fmt.Errorf("Parent returned: %v Child returned: %v", err, _err)
}
return
}