github.com/sylr/terraform@v0.11.12-beta1/helper/wrappedstreams/streams_windows.go (about)

     1  // +build windows
     2  
     3  package wrappedstreams
     4  
     5  import (
     6  	"log"
     7  	"os"
     8  	"syscall"
     9  )
    10  
    11  func initPlatform() {
    12  	wrappedStdin = openConsole("CONIN$", os.Stdin)
    13  	wrappedStdout = openConsole("CONOUT$", os.Stdout)
    14  	wrappedStderr = wrappedStdout
    15  }
    16  
    17  // openConsole opens a console handle, using a backup if it fails.
    18  // This is used to get the exact console handle instead of the redirected
    19  // handles from panicwrap.
    20  func openConsole(name string, backup *os.File) *os.File {
    21  	// Convert to UTF16
    22  	path, err := syscall.UTF16PtrFromString(name)
    23  	if err != nil {
    24  		log.Printf("[ERROR] wrappedstreams: %s", err)
    25  		return backup
    26  	}
    27  
    28  	// Determine the share mode
    29  	var shareMode uint32
    30  	switch name {
    31  	case "CONIN$":
    32  		shareMode = syscall.FILE_SHARE_READ
    33  	case "CONOUT$":
    34  		shareMode = syscall.FILE_SHARE_WRITE
    35  	}
    36  
    37  	// Get the file
    38  	h, err := syscall.CreateFile(
    39  		path,
    40  		syscall.GENERIC_READ|syscall.GENERIC_WRITE,
    41  		shareMode,
    42  		nil,
    43  		syscall.OPEN_EXISTING,
    44  		0, 0)
    45  	if err != nil {
    46  		log.Printf("[ERROR] wrappedstreams: %s", err)
    47  		return backup
    48  	}
    49  
    50  	// Create the Go file
    51  	return os.NewFile(uintptr(h), name)
    52  }