diff options
author | Brad Fitzpatrick <bradfitz@golang.org> | 2011-06-01 15:26:53 -0700 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2011-06-01 15:26:53 -0700 |
commit | 40940c426d956f7450beff31945b4a6844a4a0a1 (patch) | |
tree | c0c7ab835e353d9e9978200da00e874ef62419d0 /misc | |
parent | 843e1ce33478cfb32d2e66fd1fee7b8c891b8518 (diff) | |
download | go-40940c426d956f7450beff31945b4a6844a4a0a1.tar.gz |
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
http://codereview.appspot.com/4552052
Diffstat (limited to 'misc')
-rw-r--r-- | misc/dashboard/builder/exec.go | 53 | ||||
-rw-r--r-- | misc/goplay/goplay.go | 28 |
2 files changed, 20 insertions, 61 deletions
diff --git a/misc/dashboard/builder/exec.go b/misc/dashboard/builder/exec.go index a7ef93308..0db509136 100644 --- a/misc/dashboard/builder/exec.go +++ b/misc/dashboard/builder/exec.go @@ -19,16 +19,11 @@ func run(envv []string, dir string, argv ...string) os.Error { log.Println("run", argv) } argv = useBash(argv) - bin, err := lookPath(argv[0]) - if err != nil { - return err - } - p, err := exec.Run(bin, argv, envv, dir, - exec.DevNull, exec.DevNull, exec.PassThrough) - if err != nil { - return err - } - return p.Close() + cmd := exec.Command(argv[0], argv[1:]...) + cmd.Dir = dir + cmd.Env = envv + cmd.Stderr = os.Stderr + return cmd.Run() } // runLog runs a process and returns the combined stdout/stderr, @@ -38,16 +33,7 @@ func runLog(envv []string, logfile, dir string, argv ...string) (output string, log.Println("runLog", argv) } argv = useBash(argv) - bin, err := lookPath(argv[0]) - if err != nil { - return - } - p, err := exec.Run(bin, argv, envv, dir, - exec.DevNull, exec.Pipe, exec.MergeWithStdout) - if err != nil { - return - } - defer p.Close() + b := new(bytes.Buffer) var w io.Writer = b if logfile != "" { @@ -58,23 +44,22 @@ func runLog(envv []string, logfile, dir string, argv ...string) (output string, defer f.Close() w = io.MultiWriter(f, b) } - _, err = io.Copy(w, p.Stdout) - if err != nil { - return - } - wait, err := p.Wait(0) + + cmd := exec.Command(argv[0], argv[1:]...) + cmd.Dir = dir + cmd.Env = envv + cmd.Stdout = w + cmd.Stderr = w + + err = cmd.Run() + output = b.String() if err != nil { + if ws, ok := err.(*os.Waitmsg); ok { + exitStatus = ws.ExitStatus() + } return } - return b.String(), wait.WaitStatus.ExitStatus(), nil -} - -// lookPath looks for cmd in $PATH if cmd does not begin with / or ./ or ../. -func lookPath(cmd string) (string, os.Error) { - if strings.HasPrefix(cmd, "/") || strings.HasPrefix(cmd, "./") || strings.HasPrefix(cmd, "../") { - return cmd, nil - } - return exec.LookPath(cmd) + return } // useBash prefixes a list of args with 'bash' if the first argument diff --git a/misc/goplay/goplay.go b/misc/goplay/goplay.go index f3e2ff565..f1dc1bca5 100644 --- a/misc/goplay/goplay.go +++ b/misc/goplay/goplay.go @@ -5,7 +5,6 @@ package main import ( - "bytes" "exec" "flag" "http" @@ -140,32 +139,7 @@ func error(w http.ResponseWriter, out []byte, err os.Error) { // run executes the specified command and returns its output and an error. func run(cmd ...string) ([]byte, os.Error) { - // find the specified binary - bin, err := exec.LookPath(cmd[0]) - if err != nil { - // report binary as well as the error - return nil, os.NewError(cmd[0] + ": " + err.String()) - } - - // run the binary and read its combined stdout and stderr into a buffer - p, err := exec.Run(bin, cmd, os.Environ(), "", exec.DevNull, exec.Pipe, exec.MergeWithStdout) - if err != nil { - return nil, err - } - var buf bytes.Buffer - io.Copy(&buf, p.Stdout) - w, err := p.Wait(0) - p.Close() - if err != nil { - return nil, err - } - - // set the error return value if the program had a non-zero exit status - if !w.Exited() || w.ExitStatus() != 0 { - err = os.ErrorString("running " + cmd[0] + ": " + w.String()) - } - - return buf.Bytes(), err + return exec.Command(cmd[0], cmd[1:]...).CombinedOutput() } var frontPage, output *template.Template // HTML templates |