Prefixed IO to make sure stdout/stderr match up

This commit is contained in:
Mitchell Hashimoto 2014-06-10 10:28:47 -07:00
parent 582229969e
commit f9e830705d
2 changed files with 44 additions and 30 deletions

View File

@ -10,8 +10,17 @@ import (
// Commands is the mapping of all the available Terraform commands. // Commands is the mapping of all the available Terraform commands.
var Commands map[string]cli.CommandFactory var Commands map[string]cli.CommandFactory
const ErrorPrefix = "e:"
const OutputPrefix = "o:"
func init() { func init() {
ui := &cli.BasicUi{Writer: os.Stdout} ui := &cli.PrefixedUi{
AskPrefix: OutputPrefix,
OutputPrefix: OutputPrefix,
InfoPrefix: OutputPrefix,
ErrorPrefix: ErrorPrefix,
Ui: &cli.BasicUi{Writer: os.Stdout},
}
Commands = map[string]cli.CommandFactory{ Commands = map[string]cli.CommandFactory{
"apply": func() (cli.Command, error) { "apply": func() (cli.Command, error) {

63
main.go
View File

@ -2,14 +2,15 @@ package main
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"github.com/ActiveState/tail"
"github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/plugin"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/mitchellh/panicwrap" "github.com/mitchellh/panicwrap"
"github.com/mitchellh/prefixedio"
) )
func main() { func main() {
@ -26,6 +27,9 @@ func realMain() int {
fmt.Fprintf(os.Stderr, "Couldn't setup log output: %s", err) fmt.Fprintf(os.Stderr, "Couldn't setup log output: %s", err)
return 1 return 1
} }
if logWriter == nil {
logWriter = ioutil.Discard
}
// We always send logs to a temporary file that we use in case // We always send logs to a temporary file that we use in case
// there is a panic. Otherwise, we delete it. // there is a panic. Otherwise, we delete it.
@ -34,33 +38,22 @@ func realMain() int {
fmt.Fprintf(os.Stderr, "Couldn't setup logging tempfile: %s", err) fmt.Fprintf(os.Stderr, "Couldn't setup logging tempfile: %s", err)
return 1 return 1
} }
logTempFile.Close()
defer os.Remove(logTempFile.Name()) defer os.Remove(logTempFile.Name())
defer logTempFile.Close()
// Tell the logger to log to this file // Tell the logger to log to this file
os.Setenv(EnvLog, "1") os.Setenv(EnvLog, "")
os.Setenv(EnvLogFile, logTempFile.Name()) os.Setenv(EnvLogFile, "")
if logWriter != nil { // Setup the prefixed readers that send data properly to
// Start tailing the file beforehand to get the data // stdout/stderr.
t, err := tail.TailFile(logTempFile.Name(), tail.Config{ outR, outW := io.Pipe()
Follow: true, go copyOutput(outR)
Logger: tail.DiscardingLogger,
MustExist: true,
})
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't setup logging tempfile: %s", err)
return 1
}
go func() {
for line := range t.Lines {
logWriter.Write([]byte(line.Text + "\n"))
}
}()
}
// Create the configuration for panicwrap and wrap our executable // Create the configuration for panicwrap and wrap our executable
wrapConfig.Handler = panicHandler(logTempFile) wrapConfig.Handler = panicHandler(logTempFile)
wrapConfig.Writer = io.MultiWriter(logTempFile, logWriter)
wrapConfig.Stdout = outW
exitStatus, err := panicwrap.Wrap(&wrapConfig) exitStatus, err := panicwrap.Wrap(&wrapConfig)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't start Terraform: %s", err) fmt.Fprintf(os.Stderr, "Couldn't start Terraform: %s", err)
@ -82,14 +75,7 @@ func realMain() int {
} }
func wrappedMain() int { func wrappedMain() int {
logOutput, err := logOutput() log.SetOutput(os.Stderr)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting Terraform: %s", err)
return 1
}
if logOutput != nil {
log.SetOutput(logOutput)
}
// Load the configuration // Load the configuration
config := BuiltinConfig config := BuiltinConfig
@ -127,3 +113,22 @@ func wrappedMain() int {
return exitCode return exitCode
} }
func copyOutput(r io.Reader) {
pr, err := prefixedio.NewReader(r)
if err != nil {
panic(err)
}
stderrR, err := pr.Prefix(ErrorPrefix)
if err != nil {
panic(err)
}
stdoutR, err := pr.Prefix(OutputPrefix)
if err != nil {
panic(err)
}
go io.Copy(os.Stderr, stderrR)
go io.Copy(os.Stdout, stdoutR)
}