Skip to content

Commit

Permalink
Handling of redirected input (#3451)
Browse files Browse the repository at this point in the history
* User input forwarding

* Shell cleanup

* Errors update

* Added error checking

* imports update

* Improved error checking
  • Loading branch information
denis256 authored Oct 3, 2024
1 parent e3a1cf8 commit 3dad994
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion shell/ptty_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions shell/run_shell_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 3dad994

Please sign in to comment.