Skip to content

Commit 6ba6b3b

Browse files
authored
Merge pull request #3300 from apostasie/dev-compose-tty
Fix panic when stdout is not a tty
2 parents 012ffe8 + 7f05aad commit 6ba6b3b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

cmd/nerdctl/compose_exec.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package main
1818

1919
import (
2020
"errors"
21+
"os"
2122

23+
"github.com/moby/term"
2224
"github.com/spf13/cobra"
2325

2426
"github.com/containerd/nerdctl/v2/pkg/clientutil"
@@ -37,7 +39,8 @@ func newComposeExecCommand() *cobra.Command {
3739
}
3840
composeExecCommand.Flags().SetInterspersed(false)
3941

40-
composeExecCommand.Flags().BoolP("no-TTY", "T", false, "Disable pseudo-TTY allocation. By default nerdctl compose exec allocates a TTY.")
42+
_, isTerminal := term.GetFdInfo(os.Stdout)
43+
composeExecCommand.Flags().BoolP("no-TTY", "T", !isTerminal, "Disable pseudo-TTY allocation. By default nerdctl compose exec allocates a TTY.")
4144
composeExecCommand.Flags().BoolP("detach", "d", false, "Detached mode: Run containers in the background")
4245
composeExecCommand.Flags().StringP("workdir", "w", "", "Working directory inside the container")
4346
// env needs to be StringArray, not StringSlice, to prevent "FOO=foo1,foo2" from being split to {"FOO=foo1", "foo2"}

cmd/nerdctl/container_exec.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package main
1818

1919
import (
2020
"errors"
21+
"os"
2122

23+
"github.com/moby/term"
2224
"github.com/spf13/cobra"
2325

2426
containerd "github.com/containerd/containerd/v2/client"
@@ -84,6 +86,13 @@ func processExecCommandOptions(cmd *cobra.Command) (types.ContainerExecOptions,
8486
}
8587
}
8688

89+
_, isTerminal := term.GetFdInfo(os.Stdin)
90+
if !flagD {
91+
if flagT && flagI && !isTerminal {
92+
return types.ContainerExecOptions{}, errors.New("the input device is not a TTY")
93+
}
94+
}
95+
8796
workdir, err := cmd.Flags().GetString("workdir")
8897
if err != nil {
8998
return types.ContainerExecOptions{}, err

0 commit comments

Comments
 (0)