cmd/run: Don't check the stdin and stdout in a loop in the fallback case

The outcome of checking whether the standard input and output of the
current invocation of toolbox are connected to a terminal device is
going to stay constant for the life cycle of the process.  So, checking
it repeatedly in a loop when falling back to a different command or
working directory is wasteful.

Secondly, it prevents secondary logic like this from intermingling with
the code that actually assembles the list of arguments.  This makes it
easier to get a quick gist of the final command and its structure.

Fallout from a22d7821cb
This commit is contained in:
Debarshi Ray 2022-11-08 18:33:27 +01:00
parent 741603c64e
commit 67849e03a4

View file

@ -295,6 +295,18 @@ func runCommandWithFallbacks(container string, command []string, emitEscapeSeque
envOptions := utils.GetEnvOptionsForPreservedVariables()
var ttyNeeded bool
stdinFd := os.Stdin.Fd()
stdinFdInt := int(stdinFd)
stdoutFd := os.Stdout.Fd()
stdoutFdInt := int(stdoutFd)
if term.IsTerminal(stdinFdInt) && term.IsTerminal(stdoutFdInt) {
ttyNeeded = true
}
runFallbackCommandsIndex := 0
runFallbackWorkDirsIndex := 0
workDir := workingDirectory
@ -305,6 +317,7 @@ func runCommandWithFallbacks(container string, command []string, emitEscapeSeque
detachKeysSupported,
envOptions,
fallbackToBash,
ttyNeeded,
workDir)
if emitEscapeSequence {
@ -444,6 +457,7 @@ func constructExecArgs(container string,
detachKeysSupported bool,
envOptions []string,
fallbackToBash bool,
ttyNeeded bool,
workDir string) []string {
var detachKeys []string
@ -460,13 +474,7 @@ func constructExecArgs(container string,
execArgs = append(execArgs, detachKeys...)
stdinFd := os.Stdin.Fd()
stdinFdInt := int(stdinFd)
stdoutFd := os.Stdout.Fd()
stdoutFdInt := int(stdoutFd)
if term.IsTerminal(stdinFdInt) && term.IsTerminal(stdoutFdInt) {
if ttyNeeded {
execArgs = append(execArgs, "--tty")
}