mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 09:21:14 -06:00
fd36b548c5
Fixes #10266 panicwrap was using Extrafiles to get the original standard streams for `terraform console`. This doesn't work on Windows. Instead, we must use the Win32 APIs to get the exact handles.
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
// +build windows
|
|
|
|
package wrappedstreams
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"syscall"
|
|
)
|
|
|
|
func initPlatform() {
|
|
wrappedStdin = openConsole("CONIN$", os.Stdin)
|
|
wrappedStdout = openConsole("CONOUT$", os.Stdout)
|
|
wrappedStderr = wrappedStdout
|
|
}
|
|
|
|
// openConsole opens a console handle, using a backup if it fails.
|
|
// This is used to get the exact console handle instead of the redirected
|
|
// handles from panicwrap.
|
|
func openConsole(name string, backup *os.File) *os.File {
|
|
// Convert to UTF16
|
|
path, err := syscall.UTF16PtrFromString(name)
|
|
if err != nil {
|
|
log.Printf("[ERROR] wrappedstreams: %s", err)
|
|
return backup
|
|
}
|
|
|
|
// Determine the share mode
|
|
var shareMode uint32
|
|
switch name {
|
|
case "CONIN$":
|
|
shareMode = syscall.FILE_SHARE_READ
|
|
case "CONOUT$":
|
|
shareMode = syscall.FILE_SHARE_WRITE
|
|
}
|
|
|
|
// Get the file
|
|
h, err := syscall.CreateFile(
|
|
path,
|
|
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
|
|
shareMode,
|
|
nil,
|
|
syscall.OPEN_EXISTING,
|
|
0, 0)
|
|
if err != nil {
|
|
log.Printf("[ERROR] wrappedstreams: %s", err)
|
|
return backup
|
|
}
|
|
|
|
// Create the Go file
|
|
return os.NewFile(uintptr(h), name)
|
|
}
|