зеркало из https://github.com/golang/dep.git
gps: DRY
This commit is contained in:
Родитель
2b5ebb1ced
Коммит
a374f1cebd
|
@ -0,0 +1,13 @@
|
|||
// Copyright 2017 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gps
|
||||
|
||||
func (c cmd) Args() []string {
|
||||
return c.Cmd.Args
|
||||
}
|
||||
|
||||
func (c cmd) SetDir(dir string) {
|
||||
c.Cmd.Dir = dir
|
||||
}
|
|
@ -21,41 +21,33 @@ type cmd struct {
|
|||
ctx context.Context
|
||||
// cancel is called when the graceful shutdown timeout expires.
|
||||
cancel context.CancelFunc
|
||||
cmd *exec.Cmd
|
||||
Cmd *exec.Cmd
|
||||
}
|
||||
|
||||
func commandContext(ctx context.Context, name string, arg ...string) cmd {
|
||||
// Grab the caller's context and pass a derived one to CommandContext.
|
||||
c := cmd{ctx: ctx}
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
c.cmd = exec.CommandContext(ctx, name, arg...)
|
||||
c.Cmd = exec.CommandContext(ctx, name, arg...)
|
||||
c.cancel = cancel
|
||||
return c
|
||||
}
|
||||
|
||||
func (c cmd) Args() []string {
|
||||
return c.cmd.Args
|
||||
}
|
||||
|
||||
func (c cmd) SetDir(dir string) {
|
||||
c.cmd.Dir = dir
|
||||
}
|
||||
|
||||
// CombinedOutput is like (*os/exec.Cmd).CombinedOutput except that it
|
||||
// terminates subprocesses gently (via os.Interrupt), but resorts to Kill if
|
||||
// the subprocess fails to exit after 1 minute.
|
||||
func (c cmd) CombinedOutput() ([]byte, error) {
|
||||
// Adapted from (*os/exec.Cmd).CombinedOutput
|
||||
if c.cmd.Stdout != nil {
|
||||
if c.Cmd.Stdout != nil {
|
||||
return nil, errors.New("exec: Stdout already set")
|
||||
}
|
||||
if c.cmd.Stderr != nil {
|
||||
if c.Cmd.Stderr != nil {
|
||||
return nil, errors.New("exec: Stderr already set")
|
||||
}
|
||||
var b bytes.Buffer
|
||||
c.cmd.Stdout = &b
|
||||
c.cmd.Stderr = &b
|
||||
if err := c.cmd.Start(); err != nil {
|
||||
c.Cmd.Stdout = &b
|
||||
c.Cmd.Stderr = &b
|
||||
if err := c.Cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -71,7 +63,7 @@ func (c cmd) CombinedOutput() ([]byte, error) {
|
|||
go func() {
|
||||
select {
|
||||
case <-c.ctx.Done():
|
||||
if err := c.cmd.Process.Signal(os.Interrupt); err != nil {
|
||||
if err := c.Cmd.Process.Signal(os.Interrupt); err != nil {
|
||||
// If an error comes back from attempting to signal, proceed
|
||||
// immediately to hard kill.
|
||||
c.cancel()
|
||||
|
@ -82,7 +74,7 @@ func (c cmd) CombinedOutput() ([]byte, error) {
|
|||
}
|
||||
}()
|
||||
|
||||
if err := c.cmd.Wait(); err != nil {
|
||||
if err := c.Cmd.Wait(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.Bytes(), nil
|
||||
|
|
|
@ -16,11 +16,3 @@ type cmd struct {
|
|||
func commandContext(ctx context.Context, name string, arg ...string) cmd {
|
||||
return cmd{Cmd: exec.CommandContext(ctx, name, arg...)}
|
||||
}
|
||||
|
||||
func (c cmd) Args() []string {
|
||||
return c.Cmd.Args
|
||||
}
|
||||
|
||||
func (c cmd) SetDir(dir string) {
|
||||
c.Cmd.Dir = dir
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче