Skip to content

Commit c843d37

Browse files
committed
restore TTY auto-detection using dockerCli.Out.IsTerminal
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 1d4b4e3 commit c843d37

File tree

8 files changed

+25
-18
lines changed

8 files changed

+25
-18
lines changed

cmd/compose/compose.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/compose-spec/compose-go/types"
3030
dockercli "github.com/docker/cli/cli"
3131
"github.com/docker/cli/cli-plugins/manager"
32+
"github.com/docker/cli/cli/command"
3233
"github.com/morikuni/aec"
3334
"github.com/pkg/errors"
3435
"github.com/sirupsen/logrus"
@@ -224,7 +225,7 @@ func RunningAsStandalone() bool {
224225
}
225226

226227
// RootCommand returns the compose command with its child commands
227-
func RootCommand(backend api.Service) *cobra.Command {
228+
func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
228229
opts := projectOptions{}
229230
var (
230231
ansi string
@@ -300,9 +301,9 @@ func RootCommand(backend api.Service) *cobra.Command {
300301
logsCommand(&opts, backend),
301302
convertCommand(&opts, backend),
302303
killCommand(&opts, backend),
303-
runCommand(&opts, backend),
304+
runCommand(&opts, dockerCli, backend),
304305
removeCommand(&opts, backend),
305-
execCommand(&opts, backend),
306+
execCommand(&opts, dockerCli, backend),
306307
pauseCommand(&opts, backend),
307308
unpauseCommand(&opts, backend),
308309
topCommand(&opts, backend),

cmd/compose/exec.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/compose-spec/compose-go/types"
2323
"github.com/docker/cli/cli"
24+
"github.com/docker/cli/cli/command"
2425
"github.com/docker/compose/v2/pkg/api"
2526
"github.com/docker/compose/v2/pkg/compose"
2627
"github.com/spf13/cobra"
@@ -41,7 +42,7 @@ type execOpts struct {
4142
privileged bool
4243
}
4344

44-
func execCommand(p *projectOptions, backend api.Service) *cobra.Command {
45+
func execCommand(p *projectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
4546
opts := execOpts{
4647
composeOptions: &composeOptions{
4748
projectOptions: p,
@@ -67,7 +68,7 @@ func execCommand(p *projectOptions, backend api.Service) *cobra.Command {
6768
runCmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].")
6869
runCmd.Flags().BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the process.")
6970
runCmd.Flags().StringVarP(&opts.user, "user", "u", "", "Run the command as this user.")
70-
runCmd.Flags().BoolVarP(&opts.noTty, "no-TTY", "T", false, "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
71+
runCmd.Flags().BoolVarP(&opts.noTty, "no-TTY", "T", !dockerCli.Out().IsTerminal(), "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
7172
runCmd.Flags().StringVarP(&opts.workingDir, "workdir", "w", "", "Path to workdir directory for this command.")
7273

7374
runCmd.Flags().BoolP("interactive", "i", true, "Keep STDIN open even if not attached.")

cmd/compose/run.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
cgo "github.com/compose-spec/compose-go/cli"
2525
"github.com/compose-spec/compose-go/loader"
2626
"github.com/compose-spec/compose-go/types"
27+
"github.com/docker/cli/cli/command"
2728
"github.com/mattn/go-shellwords"
2829
"github.com/spf13/cobra"
2930
"github.com/spf13/pflag"
@@ -106,7 +107,7 @@ func (opts runOptions) apply(project *types.Project) error {
106107
return nil
107108
}
108109

109-
func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
110+
func runCommand(p *projectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
110111
opts := runOptions{
111112
composeOptions: &composeOptions{
112113
projectOptions: p,
@@ -149,7 +150,7 @@ func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
149150
flags.StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables")
150151
flags.StringArrayVarP(&opts.labels, "label", "l", []string{}, "Add or override a label")
151152
flags.BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits")
152-
flags.BoolVarP(&opts.noTty, "no-TTY", "T", false, "Disable pseudo-noTty allocation. By default docker compose run allocates a TTY")
153+
flags.BoolVarP(&opts.noTty, "no-TTY", "T", !dockerCli.Out().IsTerminal(), "Disable pseudo-noTty allocation. By default docker compose run allocates a TTY")
153154
flags.StringVar(&opts.name, "name", "", " Assign a name to the container")
154155
flags.StringVarP(&opts.user, "user", "u", "", "Run as specified username or uid")
155156
flags.StringVarP(&opts.workdir, "workdir", "w", "", "Working directory inside the container")

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func init() {
4040
func pluginMain() {
4141
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
4242
lazyInit := api.NewServiceProxy()
43-
cmd := commands.RootCommand(lazyInit)
43+
cmd := commands.RootCommand(dockerCli, lazyInit)
4444
originalPreRun := cmd.PersistentPreRunE
4545
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
4646
if err := plugin.PersistentPreRunE(cmd, args); err != nil {

docs/yaml/main/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func generateDocs(opts *options) error {
3131
Use: "docker",
3232
DisableAutoGenTag: true,
3333
}
34-
cmd.AddCommand(compose.RootCommand(nil))
34+
cmd.AddCommand(compose.RootCommand(nil, nil))
3535
disableFlagsInUseLine(cmd)
3636

3737
tool, err := clidocstool.New(clidocstool.Options{

pkg/compose/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22+
2223
"github.com/compose-spec/compose-go/types"
2324
"github.com/docker/cli/cli"
2425
cmd "github.com/docker/cli/cli/command/container"
@@ -55,6 +56,10 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
5556

5657
applyRunOptions(project, &service, opts)
5758

59+
if err := s.dockerCli.In().CheckTty(opts.Interactive, service.Tty); err != nil {
60+
return "", err
61+
}
62+
5863
slug := stringid.GenerateRandomID()
5964
if service.ContainerName == "" {
6065
service.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, service.Name, stringid.TruncateID(slug))

pkg/e2e/compose_run_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ func TestLocalComposeRun(t *testing.T) {
2929
c := NewParallelE2eCLI(t, binDir)
3030

3131
t.Run("compose run", func(t *testing.T) {
32-
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "-T", "back")
32+
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "back")
3333
lines := Lines(res.Stdout())
3434
assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout())
3535
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
36-
res = c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "-T", "back", "echo", "Hello one more time")
36+
res = c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello one more time")
3737
lines = Lines(res.Stdout())
3838
assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout())
3939
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
@@ -68,7 +68,7 @@ func TestLocalComposeRun(t *testing.T) {
6868
})
6969

7070
t.Run("compose run --rm", func(t *testing.T) {
71-
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "-T", "--rm", "back", "echo", "Hello again")
71+
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "--rm", "back", "echo", "Hello again")
7272
lines := Lines(res.Stdout())
7373
assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout())
7474

@@ -85,26 +85,26 @@ func TestLocalComposeRun(t *testing.T) {
8585
t.Run("compose run --volumes", func(t *testing.T) {
8686
wd, err := os.Getwd()
8787
assert.NilError(t, err)
88-
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "-T", "--volumes", wd+":/foo", "back", "/bin/sh", "-c", "ls /foo")
88+
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "--volumes", wd+":/foo", "back", "/bin/sh", "-c", "ls /foo")
8989
res.Assert(t, icmd.Expected{Out: "compose_run_test.go"})
9090

9191
res = c.RunDockerCmd("ps", "--all")
9292
assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout())
9393
})
9494

9595
t.Run("compose run --publish", func(t *testing.T) {
96-
c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "-T", "--publish", "8081:80", "-d", "back", "/bin/sh", "-c", "sleep 1")
96+
c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "--publish", "8081:80", "-d", "back", "/bin/sh", "-c", "sleep 1")
9797
res := c.RunDockerCmd("ps")
9898
assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
9999
})
100100

101101
t.Run("compose run orphan", func(t *testing.T) {
102102
// Use different compose files to get an orphan container
103-
c.RunDockerComposeCmd("-f", "./fixtures/run-test/orphan.yaml", "run", "-T", "simple")
104-
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "-T", "back", "echo", "Hello")
103+
c.RunDockerComposeCmd("-f", "./fixtures/run-test/orphan.yaml", "run", "simple")
104+
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
105105
assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
106106

107-
cmd := c.NewDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "-T", "back", "echo", "Hello")
107+
cmd := c.NewDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
108108
res = icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
109109
cmd.Env = append(cmd.Env, "COMPOSE_IGNORE_ORPHANS=True")
110110
})

pkg/e2e/toto.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)