From 3dad9946c633f684979384327d113954bc58598e Mon Sep 17 00:00:00 2001 From: Denis O Date: Thu, 3 Oct 2024 17:06:22 +0300 Subject: [PATCH] Handling of redirected input (#3451) * User input forwarding * Shell cleanup * Errors update * Added error checking * imports update * Improved error checking --- shell/ptty_unix.go | 13 ++++++++++++- shell/run_shell_cmd.go | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/shell/ptty_unix.go b/shell/ptty_unix.go index cad84fc86..c7f1ffee2 100644 --- a/shell/ptty_unix.go +++ b/shell/ptty_unix.go @@ -71,12 +71,17 @@ func runCommandWithPTTY(terragruntOptions *options.TerragruntOptions, cmd *exec. } }() + stdinDone := make(chan error, 1) // Copy stdin to the pty go func() { _, copyStdinErr := io.Copy(pseudoTerminal, os.Stdin) // We don't propagate this error upstream because it does not affect normal operation of the command. A repeat // of the same stdin in this case should resolve the issue. - terragruntOptions.Logger.Errorf("Error forwarding stdin: %s", copyStdinErr) + if copyStdinErr != nil { + terragruntOptions.Logger.Errorf("Error forwarding stdin: %s", copyStdinErr) + } + // signal that stdin copy is done + stdinDone <- copyStdinErr }() // ... and the pty to stdout. @@ -85,6 +90,12 @@ func runCommandWithPTTY(terragruntOptions *options.TerragruntOptions, cmd *exec. return errors.WithStackTrace(copyStdoutErr) } + // Wait for stdin copy to complete before returning + + if copyStdinErr := <-stdinDone; copyStdinErr != nil && !errors.IsError(copyStdinErr, io.EOF) { + terragruntOptions.Logger.Errorf("Error forwarding stdin: %s", copyStdinErr) + } + return nil } diff --git a/shell/run_shell_cmd.go b/shell/run_shell_cmd.go index 31576c000..a3128bdb5 100644 --- a/shell/run_shell_cmd.go +++ b/shell/run_shell_cmd.go @@ -14,6 +14,8 @@ import ( "strings" "time" + "github.com/mattn/go-isatty" + "github.com/gruntwork-io/terragrunt/internal/cache" "github.com/gruntwork-io/terragrunt/engine" @@ -293,6 +295,11 @@ func isTerraformCommandThatNeedsPty(args []string) (bool, error) { return false, nil } + // if the stdin is not a terminal, then the terraform console is used in non-interactive mode + if !isatty.IsTerminal(os.Stdin.Fd()) { + return false, nil + } + return true, nil }