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"
|
|
|
|
"exec"
|
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"
|
2010-09-21 23:18:41 -06:00
|
|
|
"strings"
|
2010-09-12 18:46:17 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// run is a simple wrapper for exec.Run/Close
|
|
|
|
func run(envv []string, dir string, argv ...string) os.Error {
|
2011-03-02 20:41:09 -07:00
|
|
|
if *verbose {
|
|
|
|
log.Println("run", argv)
|
|
|
|
}
|
2011-05-30 04:19:56 -06:00
|
|
|
argv = useBash(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,
|
|
|
|
// even if exit status is not 0.
|
|
|
|
func runLog(envv []string, logfile, dir string, argv ...string) (string, int, os.Error) {
|
2011-03-02 20:41:09 -07:00
|
|
|
if *verbose {
|
|
|
|
log.Println("runLog", argv)
|
|
|
|
}
|
2011-05-30 04:19:56 -06:00
|
|
|
argv = useBash(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
|
|
|
|
|
2011-06-06 06:17:28 -06:00
|
|
|
err := cmd.Run()
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
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
|
|
|
if ws, ok := err.(*os.Waitmsg); ok {
|
2011-06-06 06:17:28 -06:00
|
|
|
return b.String(), ws.ExitStatus(), nil
|
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
|
|
|
}
|
2011-06-06 06:17:28 -06:00
|
|
|
return b.String(), 0, nil
|
2010-09-21 23:18:41 -06:00
|
|
|
}
|
2011-05-29 19:20:46 -06:00
|
|
|
|
2011-05-30 04:19:56 -06:00
|
|
|
// useBash prefixes a list of args with 'bash' if the first argument
|
|
|
|
// is a bash script.
|
|
|
|
func useBash(argv []string) []string {
|
|
|
|
// TODO(brainman): choose a more reliable heuristic here.
|
|
|
|
if strings.HasSuffix(argv[0], ".bash") {
|
|
|
|
argv = append([]string{"bash"}, argv...)
|
|
|
|
}
|
|
|
|
return argv
|
2011-05-29 19:20:46 -06:00
|
|
|
}
|