cmd/run: Don't allocate pseudo-TTY if not connected to terminal

Passing '--tty' to 'podman exec' unconditionally causes Podman to
allocate a pseudo-TTY for the command execution. This causes problems
with piping (values not being piped in and values being piped out with
carriage return at the end of a line). The solution is to track the
presence of a terminal on stdin/stdout and based on its presence use the
'--tty' flag.

Original behaviour:
; echo foo | toolbox run less

; toolbox echo foo | od -c
0000000   f   o   o  \r  \n
0000005

New behaviour:
; echo foo | toolbox run less
foo

; toolbox echo foo | od -c
0000000   f   o   o  \n
0000004

As seen in the 'Piping in' example, the value gets only printed into
stdout. Not ideal from the point of view of using 'less' (or similar
tools) but still a move forward.

Based on a discussion in Podman's bugtracker[0].

Fixes https://github.com/containers/toolbox/issues/157
Fixes https://github.com/containers/toolbox/issues/848

[0] https://github.com/containers/podman/issues/9718

https://github.com/containers/toolbox/pull/1013
This commit is contained in:
Ondřej Míchal 2022-03-20 20:40:34 +02:00
parent 9d3601e0a6
commit a22d7821cb

View file

@ -26,6 +26,7 @@ import (
"github.com/containers/toolbox/pkg/podman" "github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/shell" "github.com/containers/toolbox/pkg/shell"
"github.com/containers/toolbox/pkg/utils" "github.com/containers/toolbox/pkg/utils"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -453,9 +454,12 @@ func constructExecArgs(container string,
execArgs = append(execArgs, detachKeys...) execArgs = append(execArgs, detachKeys...)
if isatty.IsTerminal(os.Stdin.Fd()) && isatty.IsTerminal(os.Stdout.Fd()) {
execArgs = append(execArgs, "--tty")
}
execArgs = append(execArgs, []string{ execArgs = append(execArgs, []string{
"--interactive", "--interactive",
"--tty",
"--user", currentUser.Username, "--user", currentUser.Username,
"--workdir", workDir, "--workdir", workDir,
}...) }...)