github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/helper/wrappedstreams/streams.go (about) 1 // Package wrappedstreams provides access to the standard OS streams 2 // (stdin, stdout, stderr) even if wrapped under panicwrap. 3 package wrappedstreams 4 5 import ( 6 "os" 7 8 "github.com/mitchellh/panicwrap" 9 ) 10 11 // Stdin returns the true stdin of the process. 12 func Stdin() *os.File { 13 stdin, _, _ := fds() 14 return stdin 15 } 16 17 // Stdout returns the true stdout of the process. 18 func Stdout() *os.File { 19 _, stdout, _ := fds() 20 return stdout 21 } 22 23 // Stderr returns the true stderr of the process. 24 func Stderr() *os.File { 25 _, _, stderr := fds() 26 return stderr 27 } 28 29 func fds() (stdin, stdout, stderr *os.File) { 30 stdin, stdout, stderr = os.Stdin, os.Stdout, os.Stderr 31 if panicwrap.Wrapped(nil) { 32 initPlatform() 33 stdin, stdout, stderr = wrappedStdin, wrappedStdout, wrappedStderr 34 } 35 return 36 } 37 38 // These are the wrapped standard streams. These are set up by the 39 // platform specific code in initPlatform. 40 var ( 41 wrappedStdin *os.File 42 wrappedStdout *os.File 43 wrappedStderr *os.File 44 )