From d177a2647abd9f271a700f734b64eb2a873337af Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 6 Mar 2019 16:47:26 +0100 Subject: [PATCH] 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. --- main.go | 36 +++++++++++++++++------------------- stdin.go | 38 -------------------------------------- 2 files changed, 17 insertions(+), 57 deletions(-) delete mode 100644 stdin.go diff --git a/main.go b/main.go index e47ea05e1..155e57acf 100644 --- a/main.go +++ b/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{ diff --git a/stdin.go b/stdin.go deleted file mode 100644 index 758cc6864..000000000 --- a/stdin.go +++ /dev/null @@ -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.") - }() -}