-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd.go
150 lines (135 loc) · 2.94 KB
/
cmd.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cmd
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
)
func Output(cmd string, args ...string) (string, error) {
return cmdExec(cmd, args, "", false)
}
func Run(cmd string, args ...string) error {
_, err := cmdExec(cmd, args, "", true)
return err
}
type CmdBuilder struct {
env []string
dir string
debug bool
ignoreError bool
stdout io.Writer
stderr io.Writer
}
func Env(env []string) *CmdBuilder {
return &CmdBuilder{
env: env,
}
}
func Dir(dir string) *CmdBuilder {
return &CmdBuilder{
dir: dir,
}
}
func Debug() *CmdBuilder {
return &CmdBuilder{
debug: true,
}
}
func IgnoreError(b ...bool) *CmdBuilder {
c := &CmdBuilder{}
return c.IgnoreError(b...)
}
func New() *CmdBuilder {
return &CmdBuilder{}
}
func (c *CmdBuilder) Env(env []string) *CmdBuilder {
c.env = env
return c
}
func (c *CmdBuilder) Dir(dir string) *CmdBuilder {
c.dir = dir
return c
}
func (c *CmdBuilder) Stdout(stdout io.Writer) *CmdBuilder {
c.stdout = stdout
return c
}
func (c *CmdBuilder) Stderr(stderr io.Writer) *CmdBuilder {
c.stderr = stderr
return c
}
func (c *CmdBuilder) Debug() *CmdBuilder {
c.debug = true
return c
}
func (c *CmdBuilder) IgnoreError(b ...bool) *CmdBuilder {
ignore := true
if len(b) > 0 {
ignore = b[0]
}
c.ignoreError = ignore
return c
}
func (c *CmdBuilder) Output(cmd string, args ...string) (string, error) {
return cmdExecEnv(cmd, args, c.env, c.dir, false, c)
}
func (c *CmdBuilder) Run(cmd string, args ...string) error {
_, err := cmdExecEnv(cmd, args, c.env, c.dir, true, c)
return err
}
func cmdExec(cmd string, args []string, dir string, pipeStdout bool) (string, error) {
return cmdExecEnv(cmd, args, nil, dir, pipeStdout, nil)
}
func cmdExecEnv(cmd string, args []string, env []string, dir string, useStdout bool, c *CmdBuilder) (string, error) {
var stderr io.Writer
if c != nil && c.stderr != nil {
stderr = c.stderr
} else {
stderr = os.Stderr
}
if c != nil && c.debug {
var lines []string
if len(env) > 0 {
for _, e := range env {
lines = append(lines, "# "+e)
}
}
if dir != "" {
lines = append(lines, "# cd "+Quote(dir))
}
cmdQuotes := make([]string, 0, 1+len(args))
cmdQuotes = append(cmdQuotes, cmd)
for _, arg := range args {
cmdQuotes = append(cmdQuotes, Quote(arg))
}
cmdStr := strings.Join(cmdQuotes, " ")
lines = append(lines, cmdStr)
for _, line := range lines {
fmt.Fprintln(stderr, line)
}
}
execCmd := exec.Command(cmd, args...)
execCmd.Stderr = stderr
if len(env) > 0 {
execCmd.Env = os.Environ()
execCmd.Env = append(execCmd.Env, env...)
}
execCmd.Dir = dir
if c != nil && c.stdout != nil {
execCmd.Stdout = c.stdout
return "", execCmd.Run()
} else if useStdout {
execCmd.Stdout = os.Stdout
return "", execCmd.Run()
}
out, err := execCmd.Output()
outStr := strings.TrimSuffix(string(out), "\n")
if err != nil {
if !c.ignoreError {
return outStr, err
}
err = nil
}
return outStr, nil
}