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"
|
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"
|
2010-09-12 18:46:17 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// run is a simple wrapper for exec.Run/Close
|
2011-11-01 20:06:05 -06:00
|
|
|
func run(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
|
|
|
|
return cmd.Run()
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
2010-10-20 22:33:31 -06:00
|
|
|
// runLog runs a process and returns the combined stdout/stderr,
|
2011-06-06 06:17:28 -06:00
|
|
|
// as well as writing it to logfile (if specified). It returns
|
|
|
|
// process combined stdout and stderr output, exit status and error.
|
|
|
|
// The error returned is nil, if process is started successfully,
|
2012-02-22 12:48:41 -07:00
|
|
|
// even if exit status is not successful.
|
2011-11-01 20:06:05 -06:00
|
|
|
func runLog(envv []string, logfile, dir string, argv ...string) (string, int, error) {
|
2011-03-02 20:41:09 -07:00
|
|
|
if *verbose {
|
|
|
|
log.Println("runLog", 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
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
b := new(bytes.Buffer)
|
2010-10-20 22:33:31 -06:00
|
|
|
var w io.Writer = b
|
|
|
|
if logfile != "" {
|
2011-04-05 09:12:02 -06:00
|
|
|
f, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
2010-10-20 22:33:31 -06:00
|
|
|
if err != nil {
|
2011-06-06 06:17:28 -06:00
|
|
|
return "", 0, err
|
2010-10-20 22:33:31 -06:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
w = io.MultiWriter(f, b)
|
|
|
|
}
|
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.Stdout = w
|
|
|
|
cmd.Stderr = w
|
|
|
|
|
2012-02-22 12:48:41 -07:00
|
|
|
startErr := cmd.Start()
|
|
|
|
if startErr != nil {
|
|
|
|
return "", 1, startErr
|
|
|
|
}
|
|
|
|
exitStatus := 0
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
exitStatus = 1 // TODO(bradfitz): this is fake. no callers care, so just return a bool instead.
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2012-02-22 12:48:41 -07:00
|
|
|
return b.String(), exitStatus, nil
|
2010-09-21 23:18:41 -06:00
|
|
|
}
|