aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/exec.go
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2016-05-20 20:42:21 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2016-05-20 21:19:32 +0000
commit4cad610401edc11fe921205438a7b3ab4faa3982 (patch)
treedf08822a3384d7a65c92e500bf8cb9ec85d8cdcd /src/os/exec/exec.go
parent85e39f838722a1521e09288cddfe378843d662fb (diff)
downloadgo-4cad610401edc11fe921205438a7b3ab4faa3982.tar.gz
go-4cad610401edc11fe921205438a7b3ab4faa3982.zip
os/exec: remove Cmd.RunContext and Cmd.WaitContext, add CommandContext
Fixes #15775 Change-Id: I0a6c2ca09d3850c3538494711f7a9801b9500411 Reviewed-on: https://go-review.googlesource.com/23300 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/os/exec/exec.go')
-rw-r--r--src/os/exec/exec.go38
1 files changed, 19 insertions, 19 deletions
diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go
index 5121b9b2cc..10300ce234 100644
--- a/src/os/exec/exec.go
+++ b/src/os/exec/exec.go
@@ -103,8 +103,9 @@ type Cmd struct {
// available after a call to Wait or Run.
ProcessState *os.ProcessState
- lookPathErr error // LookPath error, if any.
- finished bool // when Wait was called
+ ctx context.Context // nil means none
+ lookPathErr error // LookPath error, if any.
+ finished bool // when Wait was called
childFiles []*os.File
closeAfterStart []io.Closer
closeAfterWait []io.Closer
@@ -139,6 +140,20 @@ func Command(name string, arg ...string) *Cmd {
return cmd
}
+// CommandContext is like Command but includes a context.
+//
+// The provided context is used to kill the process (by calling
+// os.Process.Kill) if the context becomes done before the command
+// completes on its own.
+func CommandContext(ctx context.Context, name string, arg ...string) *Cmd {
+ if ctx == nil {
+ panic("nil Context")
+ }
+ cmd := Command(name, arg...)
+ cmd.ctx = ctx
+ return cmd
+}
+
// interfaceEqual protects against panics from doing equality tests on
// two interfaces with non-comparable underlying types.
func interfaceEqual(a, b interface{}) bool {
@@ -263,15 +278,6 @@ func (c *Cmd) Run() error {
return c.Wait()
}
-// RunContext is like Run, but kills the process (by calling os.Process.Kill)
-// if ctx is done before the process ends on its own.
-func (c *Cmd) RunContext(ctx context.Context) error {
- if err := c.Start(); err != nil {
- return err
- }
- return c.WaitContext(ctx)
-}
-
// lookExtensions finds windows executable by its dir and path.
// It uses LookPath to try appropriate extensions.
// lookExtensions does not search PATH, instead it converts `prog` into `.\prog`.
@@ -396,12 +402,6 @@ func (e *ExitError) Error() string {
//
// Wait releases any resources associated with the Cmd.
func (c *Cmd) Wait() error {
- return c.WaitContext(nil)
-}
-
-// WaitContext is like Wait, but kills the process (by calling os.Process.Kill)
-// if ctx is done before the process ends on its own.
-func (c *Cmd) WaitContext(ctx context.Context) error {
if c.Process == nil {
return errors.New("exec: not started")
}
@@ -411,11 +411,11 @@ func (c *Cmd) WaitContext(ctx context.Context) error {
c.finished = true
var waitDone chan struct{}
- if ctx != nil {
+ if c.ctx != nil {
waitDone = make(chan struct{})
go func() {
select {
- case <-ctx.Done():
+ case <-c.ctx.Done():
c.Process.Kill()
case <-waitDone:
}