-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
113 additions
and
71 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package shell | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
) | ||
|
||
// Command like exec.Cmd, but with support: | ||
// - io.Closer interface | ||
// - Wait from multiple places | ||
// - Done channel | ||
type Command struct { | ||
*exec.Cmd | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
err error | ||
} | ||
|
||
func NewCommand(s string) *Command { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
args := QuoteSplit(s) | ||
cmd := exec.CommandContext(ctx, args[0], args[1:]...) | ||
cmd.SysProcAttr = procAttr | ||
return &Command{cmd, ctx, cancel, nil} | ||
} | ||
|
||
func (c *Command) Start() error { | ||
if err := c.Cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
c.err = c.Cmd.Wait() | ||
c.cancel() // release context resources | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func (c *Command) Wait() error { | ||
<-c.ctx.Done() | ||
return c.err | ||
} | ||
|
||
func (c *Command) Run() error { | ||
if err := c.Start(); err != nil { | ||
return err | ||
} | ||
return c.Wait() | ||
} | ||
|
||
func (c *Command) Done() <-chan struct{} { | ||
return c.ctx.Done() | ||
} | ||
|
||
func (c *Command) Close() error { | ||
c.cancel() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build !linux | ||
|
||
package shell | ||
|
||
import "syscall" | ||
|
||
var procAttr *syscall.SysProcAttr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package shell | ||
|
||
import "syscall" | ||
|
||
// will stop child if parent died (even with SIGKILL) | ||
var procAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGTERM} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters