-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
206 lines (169 loc) · 4.42 KB
/
command.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package shellz
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"syscall"
"github.com/codeskyblue/go-sh"
"github.com/ibrt/golang-errors/errorz"
)
// Logf describes a function that echoes the details of the command being run.
type Logf func(string, ...interface{})
var (
// DefaultLogf is the default implementation of Logf.
DefaultLogf = func(cmd string, params ...interface{}) {
fmt.Println(append([]interface{}{"[shell]", cmd}, params...)...)
}
)
// Command describes a command to be spawned in a shell.
type Command struct {
cmd string
params []string
interfaceParams []interface{}
logf Logf
env map[string]string
dir string
stdin io.Reader
stdout io.Writer
stderr io.Writer
}
// NewCommand creates a new Command.
func NewCommand(cmd string, initialParams ...interface{}) *Command {
c := &Command{
cmd: cmd,
params: make([]string, 0),
logf: DefaultLogf,
env: make(map[string]string),
}
for _, initialParam := range initialParams {
c.addParam(initialParam)
}
return c
}
// AddParams appends the given params to the command.
func (c *Command) AddParams(params ...interface{}) *Command {
for _, param := range params {
c.addParam(param)
}
return c
}
// AddParamsString appends the given params to the command.
func (c *Command) AddParamsString(params ...string) *Command {
for _, param := range params {
c.addParam(param)
}
return c
}
// SetLogf overrides DefaultLogf for this command. Set to nil to skip logging.
func (c *Command) SetLogf(logf Logf) *Command {
c.logf = logf
return c
}
// SetDir sets the command working directory.
func (c *Command) SetDir(dir string) *Command {
c.dir = dir
return c
}
// SetStdin sets the command standard input.
func (c *Command) SetStdin(stdin io.Reader) *Command {
c.stdin = stdin
return c
}
// SetStdout sets the command standard output.
func (c *Command) SetStdout(stdout io.Writer) *Command {
c.stdout = stdout
return c
}
// SetStderr sets the command standard error.
func (c *Command) SetStderr(stderr io.Writer) *Command {
c.stderr = stderr
return c
}
// SetEnv sets an environment variable.
func (c *Command) SetEnv(key, value string) *Command {
c.env[key] = value
return c
}
// SetEnvMap sets a map of environment variable.
func (c *Command) SetEnvMap(m map[string]string) *Command {
for k, v := range m {
c.env[k] = v
}
return c
}
// Run runs the command.
func (c *Command) Run() error {
return errorz.MaybeWrap(c.toSH().Run(), errorz.Skip())
}
// MustRun is like Run but panics on error.
func (c *Command) MustRun() {
errorz.MaybeMustWrap(c.Run(), errorz.Skip())
}
// Output runs the command and returns its combined output as string.
func (c *Command) Output() (string, error) {
rawOutput, err := c.toSH().Output()
if err != nil {
return "", errorz.Wrap(err, errorz.Skip())
}
return strings.TrimSpace(string(rawOutput)), nil
}
// MustOutput is like Output but panics on error.
func (c *Command) MustOutput() string {
output, err := c.Output()
errorz.MaybeMustWrap(err, errorz.Skip())
return output
}
// MustExec replaces the current process using syscall.Exec with the Command. Note that:
// - It never returns.
// - It ignores stdin/stdout/stderr settings.
// - Unlike MustRun/MustOutput, it coerces params to strings using fmt.Sprintf.
func (c *Command) MustExec() {
if c.logf != nil {
c.logf(c.cmd, c.interfaceParams...)
}
binFilePath, err := exec.LookPath(c.cmd)
errorz.MaybeMustWrap(err)
env := os.Environ()
for k, v := range c.env {
env = append(env, fmt.Sprintf("%v=%v", k, v))
}
if c.dir != "" {
errorz.MaybeMustWrap(os.Chdir(c.dir))
}
errorz.MaybeMustWrap(syscall.Exec(binFilePath, append([]string{c.cmd}, c.params...), env))
}
func (c *Command) toSH() *sh.Session {
shl := sh.NewSession()
shl.ShowCMD = false
if c.dir != "" {
shl.SetDir(c.dir)
}
if c.stdin != nil {
shl.Stdin = c.stdin
}
if c.stdout != nil {
shl.Stdout = c.stdout
}
if c.stderr != nil {
shl.Stderr = c.stderr
}
for k, v := range c.env {
shl.SetEnv(k, v)
}
if c.logf != nil {
c.logf(c.cmd, c.interfaceParams...)
}
return shl.Command(c.cmd, c.interfaceParams...)
}
func (c *Command) addParam(param interface{}) {
if s, ok := param.(string); ok {
c.params = append(c.params, s)
c.interfaceParams = append(c.interfaceParams, s)
} else {
s := fmt.Sprintf("%v", param)
c.params = append(c.params, s)
c.interfaceParams = append(c.interfaceParams, s)
}
}