-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper/wrappedstreams: get original console input/output on Windows
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.
- Loading branch information
Showing
5 changed files
with
83 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// +build !windows | ||
|
||
package wrappedstreams | ||
|
||
func initPlatform() { | ||
// The standard streams are passed in via extra file descriptors. | ||
wrappedStdin = os.NewFile(uintptr(3), "stdin") | ||
wrappedStdout = os.NewFile(uintptr(4), "stdout") | ||
wrappedStderr = os.NewFile(uintptr(5), "stderr") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// +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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters