@@ -3,15 +3,13 @@ package main
33import (
44 "encoding/json"
55 "fmt"
6- "io"
76 "io/ioutil"
87 "log"
98 "net"
109 "os"
1110 "path/filepath"
1211 "runtime"
1312 "strings"
14- "sync"
1513
1614 "github.com/hashicorp/go-plugin"
1715 "github.com/hashicorp/terraform-svchost/disco"
@@ -21,12 +19,10 @@ import (
2119 "github.com/hashicorp/terraform/httpclient"
2220 "github.com/hashicorp/terraform/internal/logging"
2321 "github.com/hashicorp/terraform/version"
24- "github.com/mattn/go-colorable"
2522 "github.com/mattn/go-shellwords"
2623 "github.com/mitchellh/cli"
2724 "github.com/mitchellh/colorstring"
2825 "github.com/mitchellh/panicwrap"
29- "github.com/mitchellh/prefixedio"
3026
3127 backendInit "github.com/hashicorp/terraform/backend/init"
3228)
@@ -59,15 +55,8 @@ func realMain() int {
5955 defer os .Remove (logTempFile .Name ())
6056 defer logTempFile .Close ()
6157
62- // Setup the prefixed readers that send data properly to
63- // stdout/stderr.
64- doneCh := make (chan struct {})
65- outR , outW := io .Pipe ()
66- go copyOutput (outR , doneCh )
67-
6858 // Create the configuration for panicwrap and wrap our executable
6959 wrapConfig .Handler = logging .PanicHandler (logTempFile )
70- wrapConfig .Stdout = outW
7160 wrapConfig .IgnoreSignals = ignoreSignals
7261 wrapConfig .ForwardSignals = forwardSignals
7362 exitStatus , err := panicwrap .Wrap (& wrapConfig )
@@ -76,36 +65,18 @@ func realMain() int {
7665 return 1
7766 }
7867
79- // If >= 0, we're the parent, so just exit
80- if exitStatus >= 0 {
81- // Close the stdout writer so that our copy process can finish
82- outW .Close ()
83-
84- // Wait for the output copying to finish
85- <- doneCh
86-
87- return exitStatus
88- }
89-
90- // We're the child, so just close the tempfile we made in order to
91- // save file handles since the tempfile is only used by the parent.
92- logTempFile .Close ()
68+ return exitStatus
9369 }
9470
9571 // Call the real main
9672 return wrappedMain ()
9773}
9874
9975func init () {
100- Ui = & cli.PrefixedUi {
101- AskPrefix : OutputPrefix ,
102- OutputPrefix : OutputPrefix ,
103- InfoPrefix : OutputPrefix ,
104- ErrorPrefix : ErrorPrefix ,
105- Ui : & cli.BasicUi {
106- Writer : os .Stdout ,
107- Reader : os .Stdin ,
108- },
76+ Ui = & cli.BasicUi {
77+ Writer : os .Stdout ,
78+ ErrorWriter : os .Stderr ,
79+ Reader : os .Stdin ,
10980 }
11081}
11182
@@ -299,65 +270,6 @@ func wrappedMain() int {
299270 return exitCode
300271}
301272
302- // copyOutput uses output prefixes to determine whether data on stdout
303- // should go to stdout or stderr. This is due to panicwrap using stderr
304- // as the log and error channel.
305- func copyOutput (r io.Reader , doneCh chan <- struct {}) {
306- defer close (doneCh )
307-
308- pr , err := prefixedio .NewReader (r )
309- if err != nil {
310- panic (err )
311- }
312-
313- stderrR , err := pr .Prefix (ErrorPrefix )
314- if err != nil {
315- panic (err )
316- }
317- stdoutR , err := pr .Prefix (OutputPrefix )
318- if err != nil {
319- panic (err )
320- }
321- defaultR , err := pr .Prefix ("" )
322- if err != nil {
323- panic (err )
324- }
325-
326- var stdout io.Writer = os .Stdout
327- var stderr io.Writer = os .Stderr
328-
329- if runtime .GOOS == "windows" {
330- stdout = colorable .NewColorableStdout ()
331- stderr = colorable .NewColorableStderr ()
332-
333- // colorable is not concurrency-safe when stdout and stderr are the
334- // same console, so we need to add some synchronization to ensure that
335- // we can't be concurrently writing to both stderr and stdout at
336- // once, or else we get intermingled writes that create gibberish
337- // in the console.
338- wrapped := synchronizedWriters (stdout , stderr )
339- stdout = wrapped [0 ]
340- stderr = wrapped [1 ]
341- }
342-
343- var wg sync.WaitGroup
344- wg .Add (3 )
345- go func () {
346- defer wg .Done ()
347- io .Copy (stderr , stderrR )
348- }()
349- go func () {
350- defer wg .Done ()
351- io .Copy (stdout , stdoutR )
352- }()
353- go func () {
354- defer wg .Done ()
355- io .Copy (stdout , defaultR )
356- }()
357-
358- wg .Wait ()
359- }
360-
361273func mergeEnvArgs (envName string , cmd string , args []string ) ([]string , error ) {
362274 v := os .Getenv (envName )
363275 if v == "" {
0 commit comments