github.com/hashicorp/packer@v1.14.3/helper/wrappedstreams/streams.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  // STOLEN SHAMELESSLY FROM THE TERRAFORM REPO BECAUSE VENDORING OUT
     5  // WRAPPEDREADLINE AND WRAPPEDSTREAMS FELT LIKE TOO MUCH WORK.
     6  //
     7  // "a little copying is better than a lot of dependency"
     8  //
     9  // Package wrappedstreams provides access to the standard OS streams
    10  // (stdin, stdout, stderr) even if wrapped under panicwrap.
    11  package wrappedstreams
    12  
    13  import (
    14  	"os"
    15  
    16  	"github.com/mitchellh/panicwrap"
    17  )
    18  
    19  // Stdin returns the true stdin of the process.
    20  func Stdin() *os.File {
    21  	stdin, _, _ := fds()
    22  	return stdin
    23  }
    24  
    25  // Stdout returns the true stdout of the process.
    26  func Stdout() *os.File {
    27  	_, stdout, _ := fds()
    28  	return stdout
    29  }
    30  
    31  // Stderr returns the true stderr of the process.
    32  func Stderr() *os.File {
    33  	_, _, stderr := fds()
    34  	return stderr
    35  }
    36  
    37  func fds() (stdin, stdout, stderr *os.File) {
    38  	stdin, stdout, stderr = os.Stdin, os.Stdout, os.Stderr
    39  	if panicwrap.Wrapped(nil) {
    40  		initPlatform()
    41  		stdin, stdout, stderr = wrappedStdin, wrappedStdout, wrappedStderr
    42  	}
    43  	return
    44  }
    45  
    46  // These are the wrapped standard streams. These are setup by the
    47  // platform specific code in initPlatform.
    48  var (
    49  	wrappedStdin  *os.File
    50  	wrappedStdout *os.File
    51  	wrappedStderr *os.File
    52  )