Skip to content

Commit 36d7189

Browse files
Rename SetCmd to BeforeExec
1 parent bb29a47 commit 36d7189

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## v1.4
44

5-
### v1.4.0
5+
### v1.4.0 (2022-01-01)
66

7-
* Added `Options.SetCmd` based on PR #53 #54 by @wenerme
7+
* Added `Options.BeforeExec` based on PR #53 #54 by @wenerme (issue #53)
88

99
## v1.3
1010

cmd.go

+26-26
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ type Cmd struct {
8585
Stderr chan string
8686

8787
*sync.Mutex
88-
started bool // cmd.Start called, no error
89-
stopped bool // Stop called
90-
done bool // run() done
91-
final bool // status finalized in Status
92-
startTime time.Time // if started true
93-
stdoutBuf *OutputBuffer
94-
stderrBuf *OutputBuffer
95-
stdoutStream *OutputStream
96-
stderrStream *OutputStream
97-
status Status
98-
statusChan chan Status // nil until Start() called
99-
doneChan chan struct{} // closed when done running
100-
setCmdFuncs []func(cmd *exec.Cmd)
88+
started bool // cmd.Start called, no error
89+
stopped bool // Stop called
90+
done bool // run() done
91+
final bool // status finalized in Status
92+
startTime time.Time // if started true
93+
stdoutBuf *OutputBuffer
94+
stderrBuf *OutputBuffer
95+
stdoutStream *OutputStream
96+
stderrStream *OutputStream
97+
status Status
98+
statusChan chan Status // nil until Start() called
99+
doneChan chan struct{} // closed when done running
100+
beforeExecFuncs []func(cmd *exec.Cmd)
101101
}
102102

103103
var (
@@ -153,10 +153,10 @@ type Options struct {
153153
// streaming channels, else lines are dropped silently.
154154
Streaming bool
155155

156-
// SetCmd is a list of callbacks to customize the underlying os/exec.Cmd.
157-
// For example, use it to set SysProcAttr. All callbacks are called once,
158-
// just before running the command.
159-
SetCmd []func(cmd *exec.Cmd)
156+
// BeforeExec is a list of functions called immediately before starting
157+
// the real command. These functions can be used to customize the underlying
158+
// os/exec.Cmd. For example, to set SysProcAttr.
159+
BeforeExec []func(cmd *exec.Cmd)
160160
}
161161

162162
// NewCmdOptions creates a new Cmd with options. The command is not started
@@ -191,13 +191,13 @@ func NewCmdOptions(options Options, name string, args ...string) *Cmd {
191191
c.stderrStream = NewOutputStream(c.Stderr)
192192
}
193193

194-
if len(options.SetCmd) > 0 {
195-
c.setCmdFuncs = []func(cmd *exec.Cmd){}
196-
for _, f := range options.SetCmd {
194+
if len(options.BeforeExec) > 0 {
195+
c.beforeExecFuncs = []func(cmd *exec.Cmd){}
196+
for _, f := range options.BeforeExec {
197197
if f == nil {
198198
continue
199199
}
200-
c.setCmdFuncs = append(c.setCmdFuncs, f)
200+
c.beforeExecFuncs = append(c.beforeExecFuncs, f)
201201
}
202202
}
203203

@@ -220,10 +220,10 @@ func (c *Cmd) Clone() *Cmd {
220220
clone.Dir = c.Dir
221221
clone.Env = c.Env
222222

223-
if len(c.setCmdFuncs) > 0 {
224-
clone.setCmdFuncs = make([]func(cmd *exec.Cmd), len(c.setCmdFuncs))
225-
for i := range c.setCmdFuncs {
226-
clone.setCmdFuncs[i] = c.setCmdFuncs[i]
223+
if len(c.beforeExecFuncs) > 0 {
224+
clone.beforeExecFuncs = make([]func(cmd *exec.Cmd), len(c.beforeExecFuncs))
225+
for i := range c.beforeExecFuncs {
226+
clone.beforeExecFuncs[i] = c.beforeExecFuncs[i]
227227
}
228228
}
229229

@@ -417,7 +417,7 @@ func (c *Cmd) run(in io.Reader) {
417417
cmd.Dir = c.Dir
418418

419419
// Run all optional commands to customize underlying os/exe.Cmd.
420-
for _, f := range c.setCmdFuncs {
420+
for _, f := range c.beforeExecFuncs {
421421
f(cmd)
422422
}
423423

cmd_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !windows
12
// +build !windows
23

34
package cmd_test
@@ -1128,11 +1129,11 @@ func TestStdinOk(t *testing.T) {
11281129
}
11291130
}
11301131

1131-
func TestOptionsSetCmd(t *testing.T) {
1132+
func TestOptionsBeforeExec(t *testing.T) {
11321133
handled := false
11331134
p := cmd.NewCmdOptions(
11341135
cmd.Options{
1135-
SetCmd: []func(cmd *exec.Cmd){
1136+
BeforeExec: []func(cmd *exec.Cmd){
11361137
func(cmd *exec.Cmd) { handled = true },
11371138
},
11381139
},
@@ -1147,7 +1148,7 @@ func TestOptionsSetCmd(t *testing.T) {
11471148
handled = false
11481149
p = cmd.NewCmdOptions(
11491150
cmd.Options{
1150-
SetCmd: []func(cmd *exec.Cmd){
1151+
BeforeExec: []func(cmd *exec.Cmd){
11511152
nil,
11521153
func(cmd *exec.Cmd) { handled = true },
11531154
},

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/go-cmd/cmd
22

3-
go 1.16
3+
go 1.17
44

55
require github.com/go-test/deep v1.0.7

0 commit comments

Comments
 (0)