2011-03-02 20:41:09 -07:00
|
|
|
// Copyright 2011 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.
|
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2012-09-17 11:41:47 -06:00
|
|
|
"fmt"
|
2010-10-20 22:33:31 -06:00
|
|
|
"io"
|
2011-03-02 20:41:09 -07:00
|
|
|
"log"
|
2010-09-12 18:46:17 -06:00
|
|
|
"os"
|
2011-11-08 16:43:02 -07:00
|
|
|
"os/exec"
|
2012-09-17 11:41:47 -06:00
|
|
|
"time"
|
2010-09-12 18:46:17 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// run is a simple wrapper for exec.Run/Close
|
2012-09-17 11:41:47 -06:00
|
|
|
func run(timeout time.Duration, envv []string, dir string, argv ...string) error {
|
2011-03-02 20:41:09 -07:00
|
|
|
if *verbose {
|
|
|
|
log.Println("run", argv)
|
|
|
|
}
|
exec: new API, replace Run with Command
This removes exec.Run and replaces exec.Cmd with a
new implementation. The new exec.Cmd represents
both a currently-running command and also a command
being prepared. It has a good zero value.
You can Start + Wait on a Cmd, or simply Run it.
Start (and Run) deal with copying stdout, stdin,
and stderr between the Cmd's io.Readers and
io.Writers.
There are convenience methods to capture a command's
stdout and/or stderr.
R=r, n13m3y3r, rsc, gustavo, alex.brainman, dsymonds, r, adg, duzy.chan, mike.rosset, kevlar
CC=golang-dev
https://golang.org/cl/4552052
2011-06-01 16:26:53 -06:00
|
|
|
cmd := exec.Command(argv[0], argv[1:]...)
|
|
|
|
cmd.Dir = dir
|
|
|
|
cmd.Env = envv
|
|
|
|
cmd.Stderr = os.Stderr
|
2012-09-17 11:41:47 -06:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return waitWithTimeout(timeout, cmd)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 16:44:29 -07:00
|
|
|
// runLog runs a process and returns the combined stdout/stderr. It returns
|
|
|
|
// process combined stdout and stderr output, exit status and error. The
|
|
|
|
// error returned is nil, if process is started successfully, even if exit
|
|
|
|
// status is not successful.
|
|
|
|
func runLog(timeout time.Duration, envv []string, dir string, argv ...string) (string, bool, error) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
ok, err := runOutput(timeout, envv, &b, dir, argv...)
|
|
|
|
return b.String(), ok, err
|
|
|
|
}
|
exec: new API, replace Run with Command
This removes exec.Run and replaces exec.Cmd with a
new implementation. The new exec.Cmd represents
both a currently-running command and also a command
being prepared. It has a good zero value.
You can Start + Wait on a Cmd, or simply Run it.
Start (and Run) deal with copying stdout, stdin,
and stderr between the Cmd's io.Readers and
io.Writers.
There are convenience methods to capture a command's
stdout and/or stderr.
R=r, n13m3y3r, rsc, gustavo, alex.brainman, dsymonds, r, adg, duzy.chan, mike.rosset, kevlar
CC=golang-dev
https://golang.org/cl/4552052
2011-06-01 16:26:53 -06:00
|
|
|
|
2013-02-14 16:44:29 -07:00
|
|
|
// runOutput runs a process and directs any output to the supplied writer.
|
|
|
|
// It returns exit status and error. The error returned is nil, if process
|
|
|
|
// is started successfully, even if exit status is not successful.
|
|
|
|
func runOutput(timeout time.Duration, envv []string, out io.Writer, dir string, argv ...string) (bool, error) {
|
|
|
|
if *verbose {
|
|
|
|
log.Println("runOutput", argv)
|
2010-10-20 22:33:31 -06:00
|
|
|
}
|
exec: new API, replace Run with Command
This removes exec.Run and replaces exec.Cmd with a
new implementation. The new exec.Cmd represents
both a currently-running command and also a command
being prepared. It has a good zero value.
You can Start + Wait on a Cmd, or simply Run it.
Start (and Run) deal with copying stdout, stdin,
and stderr between the Cmd's io.Readers and
io.Writers.
There are convenience methods to capture a command's
stdout and/or stderr.
R=r, n13m3y3r, rsc, gustavo, alex.brainman, dsymonds, r, adg, duzy.chan, mike.rosset, kevlar
CC=golang-dev
https://golang.org/cl/4552052
2011-06-01 16:26:53 -06:00
|
|
|
|
|
|
|
cmd := exec.Command(argv[0], argv[1:]...)
|
|
|
|
cmd.Dir = dir
|
|
|
|
cmd.Env = envv
|
2013-02-14 16:44:29 -07:00
|
|
|
cmd.Stdout = out
|
|
|
|
cmd.Stderr = out
|
exec: new API, replace Run with Command
This removes exec.Run and replaces exec.Cmd with a
new implementation. The new exec.Cmd represents
both a currently-running command and also a command
being prepared. It has a good zero value.
You can Start + Wait on a Cmd, or simply Run it.
Start (and Run) deal with copying stdout, stdin,
and stderr between the Cmd's io.Readers and
io.Writers.
There are convenience methods to capture a command's
stdout and/or stderr.
R=r, n13m3y3r, rsc, gustavo, alex.brainman, dsymonds, r, adg, duzy.chan, mike.rosset, kevlar
CC=golang-dev
https://golang.org/cl/4552052
2011-06-01 16:26:53 -06:00
|
|
|
|
2012-02-22 12:48:41 -07:00
|
|
|
startErr := cmd.Start()
|
|
|
|
if startErr != nil {
|
2013-02-14 16:44:29 -07:00
|
|
|
return false, startErr
|
2012-02-22 12:48:41 -07:00
|
|
|
}
|
2012-09-17 11:41:47 -06:00
|
|
|
if err := waitWithTimeout(timeout, cmd); err != nil {
|
2013-02-14 16:44:29 -07:00
|
|
|
return false, err
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2013-02-14 16:44:29 -07:00
|
|
|
return true, nil
|
2010-09-21 23:18:41 -06:00
|
|
|
}
|
2012-09-17 11:41:47 -06:00
|
|
|
|
|
|
|
func waitWithTimeout(timeout time.Duration, cmd *exec.Cmd) error {
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
errc <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
var err error
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
|
|
|
cmd.Process.Kill()
|
|
|
|
err = fmt.Errorf("timed out after %v", timeout)
|
|
|
|
case err = <-errc:
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|