Skip to content

Commit

Permalink
Merge pull request #3300 from apostasie/dev-compose-tty
Browse files Browse the repository at this point in the history
Fix panic when stdout is not a tty
  • Loading branch information
AkihiroSuda authored Aug 13, 2024
2 parents 012ffe8 + 7f05aad commit 6ba6b3b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/nerdctl/compose_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package main

import (
"errors"
"os"

"github.com/moby/term"
"github.com/spf13/cobra"

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

composeExecCommand.Flags().BoolP("no-TTY", "T", false, "Disable pseudo-TTY allocation. By default nerdctl compose exec allocates a TTY.")
_, isTerminal := term.GetFdInfo(os.Stdout)
composeExecCommand.Flags().BoolP("no-TTY", "T", !isTerminal, "Disable pseudo-TTY allocation. By default nerdctl compose exec allocates a TTY.")
composeExecCommand.Flags().BoolP("detach", "d", false, "Detached mode: Run containers in the background")
composeExecCommand.Flags().StringP("workdir", "w", "", "Working directory inside the container")
// env needs to be StringArray, not StringSlice, to prevent "FOO=foo1,foo2" from being split to {"FOO=foo1", "foo2"}
Expand Down
9 changes: 9 additions & 0 deletions cmd/nerdctl/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package main

import (
"errors"
"os"

"github.com/moby/term"
"github.com/spf13/cobra"

containerd "github.com/containerd/containerd/v2/client"
Expand Down Expand Up @@ -84,6 +86,13 @@ func processExecCommandOptions(cmd *cobra.Command) (types.ContainerExecOptions,
}
}

_, isTerminal := term.GetFdInfo(os.Stdin)
if !flagD {
if flagT && flagI && !isTerminal {
return types.ContainerExecOptions{}, errors.New("the input device is not a TTY")
}
}

workdir, err := cmd.Flags().GetString("workdir")
if err != nil {
return types.ContainerExecOptions{}, err
Expand Down

0 comments on commit 6ba6b3b

Please sign in to comment.