stop piping stdin
setupStdin switched out stdin for a pipe so that we could close the writer end of the pipe when we receive an interrupt so plugins blocked on reading from stdin are unblocked. But this is now handled using contexts.
This commit is contained in:
parent
55261f1bf3
commit
d177a2647a
36
main.go
36
main.go
|
@ -145,12 +145,6 @@ func wrappedMain() int {
|
|||
|
||||
inPlugin := os.Getenv(plugin.MagicCookieKey) == plugin.MagicCookieValue
|
||||
|
||||
// Prepare stdin for plugin usage by switching it to a pipe
|
||||
// But do not switch to pipe in plugin
|
||||
if !inPlugin {
|
||||
setupStdin()
|
||||
}
|
||||
|
||||
config, err := loadConfig()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error loading configuration: \n\n%s\n", err)
|
||||
|
@ -186,18 +180,7 @@ func wrappedMain() int {
|
|||
|
||||
defer plugin.CleanupClients()
|
||||
|
||||
tty, err := tty.Open()
|
||||
if err != nil {
|
||||
log.Printf("running packer without a tty: %s", err)
|
||||
}
|
||||
|
||||
// Setup the UI if we're being machine-readable
|
||||
var ui packer.Ui = &packer.BasicUi{
|
||||
Reader: os.Stdin,
|
||||
Writer: os.Stdout,
|
||||
ErrorWriter: os.Stdout,
|
||||
TTY: tty,
|
||||
}
|
||||
var ui packer.Ui
|
||||
if machineReadable {
|
||||
ui = &packer.MachineReadableUi{
|
||||
Writer: os.Stdout,
|
||||
|
@ -209,8 +192,23 @@ func wrappedMain() int {
|
|||
fmt.Fprintf(os.Stderr, "Packer failed to initialize UI: %s\n", err)
|
||||
return 1
|
||||
}
|
||||
} else {
|
||||
var TTY packer.TTY
|
||||
if !inPlugin {
|
||||
var err error
|
||||
TTY, err = tty.Open()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "No tty available: %s\n", err)
|
||||
}
|
||||
}
|
||||
// Setup the UI if we're being machine-readable
|
||||
ui = &packer.BasicUi{
|
||||
Reader: os.Stdin,
|
||||
Writer: os.Stdout,
|
||||
ErrorWriter: os.Stdout,
|
||||
TTY: TTY,
|
||||
}
|
||||
}
|
||||
|
||||
// Create the CLI meta
|
||||
CommandMeta = &command.Meta{
|
||||
CoreConfig: &packer.CoreConfig{
|
||||
|
|
38
stdin.go
38
stdin.go
|
@ -1,38 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// setupStdin switches out stdin for a pipe. We do this so that we can
|
||||
// close the writer end of the pipe when we receive an interrupt so plugins
|
||||
// blocked on reading from stdin are unblocked.
|
||||
func setupStdin() {
|
||||
// Create the pipe and swap stdin for the reader end
|
||||
r, w, _ := os.Pipe()
|
||||
originalStdin := os.Stdin
|
||||
os.Stdin = r
|
||||
|
||||
// Create a goroutine that copies data from the original stdin
|
||||
// into the writer end of the pipe forever.
|
||||
go func() {
|
||||
defer w.Close()
|
||||
io.Copy(w, originalStdin)
|
||||
}()
|
||||
|
||||
// Register a signal handler for interrupt in order to close the
|
||||
// writer end of our pipe so that readers get EOF downstream.
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
defer signal.Stop(ch)
|
||||
defer w.Close()
|
||||
<-ch
|
||||
log.Println("Closing stdin because interrupt received.")
|
||||
}()
|
||||
}
|
Loading…
Reference in New Issue