kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/helper/wrappedstreams/streams_windows.go (about)

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