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"
|
2011-05-29 19:20:46 -06:00
|
|
|
"runtime"
|
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-29 19:20:46 -06:00
|
|
|
if runtime.GOOS == "windows" && isBash(argv[0]) {
|
|
|
|
// shell script cannot be executed directly on Windows.
|
|
|
|
argv = append([]string{"bash", "-c"}, argv...)
|
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
bin, err := lookPath(argv[0])
|
2010-09-12 18:46:17 -06:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2010-10-20 22:33:31 -06:00
|
|
|
// runLog runs a process and returns the combined stdout/stderr,
|
|
|
|
// as well as writing it to logfile (if specified).
|
|
|
|
func runLog(envv []string, logfile, dir string, argv ...string) (output string, exitStatus int, err os.Error) {
|
2011-03-02 20:41:09 -07:00
|
|
|
if *verbose {
|
|
|
|
log.Println("runLog", argv)
|
|
|
|
}
|
2011-05-29 19:20:46 -06:00
|
|
|
if runtime.GOOS == "windows" && isBash(argv[0]) {
|
|
|
|
// shell script cannot be executed directly on Windows.
|
|
|
|
argv = append([]string{"bash", "-c"}, argv...)
|
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
bin, err := lookPath(argv[0])
|
2010-09-12 18:46:17 -06:00
|
|
|
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)
|
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 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
w = io.MultiWriter(f, b)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(w, p.Stdout)
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2010-10-20 22:33:31 -06:00
|
|
|
wait, err := p.Wait(0)
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2010-10-20 22:33:31 -06:00
|
|
|
return b.String(), wait.WaitStatus.ExitStatus(), nil
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2010-09-21 23:18:41 -06:00
|
|
|
|
2011-05-12 09:21:34 -06:00
|
|
|
// 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
|
2010-10-22 11:06:33 -06:00
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
return exec.LookPath(cmd)
|
2010-09-21 23:18:41 -06:00
|
|
|
}
|
2011-05-29 19:20:46 -06:00
|
|
|
|
|
|
|
// isBash determines if name refers to a shell script.
|
|
|
|
func isBash(name string) bool {
|
|
|
|
// TODO(brainman): perhaps it is too simple and needs better check.
|
|
|
|
return strings.HasSuffix(name, ".bash")
|
|
|
|
}
|