From 67849e03a45b5580227e432444c6b0eade865f84 Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Tue, 8 Nov 2022 18:33:27 +0100 Subject: [PATCH] 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 a22d7821cb8cea134791dcd4643de21ef565d645 --- src/cmd/run.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/cmd/run.go b/src/cmd/run.go index 83e6480..ed2c0e8 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -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") }