mk/recipe.go

60 lines
960 B
Go
Raw Normal View History

2013-02-25 23:52:08 -08:00
package main
import (
2013-02-26 11:33:07 -08:00
"io"
"log"
"os"
"os/exec"
2013-02-25 23:52:08 -08:00
)
// A monolithic function for executing recipes.
func executeRecipe(program string,
2013-02-26 11:33:07 -08:00
args []string,
input string,
echo_out bool,
echo_err bool,
capture_out bool) string {
cmd := exec.Command(program, args...)
if echo_out {
cmdout, err := cmd.StdoutPipe()
if err != nil {
go io.Copy(os.Stdout, cmdout)
}
}
if echo_err {
cmderr, err := cmd.StdoutPipe()
if err != nil {
go io.Copy(os.Stderr, cmderr)
}
}
if len(input) > 0 {
cmdin, err := cmd.StdinPipe()
2013-02-26 22:41:25 -08:00
if err == nil {
go func() { cmdin.Write([]byte(input)); cmdin.Close() }()
2013-02-26 11:33:07 -08:00
}
}
output := ""
var err error
if capture_out {
var outbytes []byte
outbytes, err = cmd.Output()
output = string(outbytes)
2013-02-26 22:41:25 -08:00
if output[len(output)-1] == '\n' {
output = output[:len(output)-1]
}
2013-02-26 11:33:07 -08:00
} else {
err = cmd.Run()
}
if err != nil {
// TODO: better error output
log.Fatal("Recipe failed")
}
return output
2013-02-25 23:52:08 -08:00
}