Skip to content

Commit

Permalink
restore TTY auto-detection using dockerCli.Out.IsTerminal
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Mar 15, 2022
1 parent d0a5ab6 commit d1c48b1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
7 changes: 4 additions & 3 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package compose
import (
"context"
"fmt"
"github.com/docker/cli/cli/command"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -224,7 +225,7 @@ func RunningAsStandalone() bool {
}

// RootCommand returns the compose command with its child commands
func RootCommand(backend api.Service) *cobra.Command {
func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
opts := projectOptions{}
var (
ansi string
Expand Down Expand Up @@ -300,9 +301,9 @@ func RootCommand(backend api.Service) *cobra.Command {
logsCommand(&opts, backend),
convertCommand(&opts, backend),
killCommand(&opts, backend),
runCommand(&opts, backend),
runCommand(&opts, dockerCli, backend),
removeCommand(&opts, backend),
execCommand(&opts, backend),
execCommand(&opts, dockerCli, backend),
pauseCommand(&opts, backend),
unpauseCommand(&opts, backend),
topCommand(&opts, backend),
Expand Down
5 changes: 3 additions & 2 deletions cmd/compose/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package compose

import (
"context"
"github.com/docker/cli/cli/command"

"github.com/compose-spec/compose-go/types"
"github.com/docker/cli/cli"
Expand All @@ -41,7 +42,7 @@ type execOpts struct {
privileged bool
}

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

runCmd.Flags().BoolP("interactive", "i", true, "Keep STDIN open even if not attached.")
Expand Down
5 changes: 3 additions & 2 deletions cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package compose
import (
"context"
"fmt"
"github.com/docker/cli/cli/command"
"strings"

cgo "github.com/compose-spec/compose-go/cli"
Expand Down Expand Up @@ -106,7 +107,7 @@ func (opts runOptions) apply(project *types.Project) error {
return nil
}

func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
func runCommand(p *projectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
opts := runOptions{
composeOptions: &composeOptions{
projectOptions: p,
Expand Down Expand Up @@ -149,7 +150,7 @@ func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
flags.StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables")
flags.StringArrayVarP(&opts.labels, "label", "l", []string{}, "Add or override a label")
flags.BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits")
flags.BoolVarP(&opts.noTty, "no-TTY", "T", false, "Disable pseudo-noTty allocation. By default docker compose run allocates a TTY")
flags.BoolVarP(&opts.noTty, "no-TTY", "T", !dockerCli.Out().IsTerminal(), "Disable pseudo-noTty allocation. By default docker compose run allocates a TTY")
flags.StringVar(&opts.name, "name", "", " Assign a name to the container")
flags.StringVarP(&opts.user, "user", "u", "", "Run as specified username or uid")
flags.StringVarP(&opts.workdir, "workdir", "w", "", "Working directory inside the container")
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func init() {
func pluginMain() {
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
lazyInit := api.NewServiceProxy()
cmd := commands.RootCommand(lazyInit)
cmd := commands.RootCommand(dockerCli, lazyInit)
originalPreRun := cmd.PersistentPreRunE
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docs/yaml/main/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func generateDocs(opts *options) error {
Use: "docker",
DisableAutoGenTag: true,
}
cmd.AddCommand(compose.RootCommand(nil))
cmd.AddCommand(compose.RootCommand(nil, nil))
disableFlagsInUseLine(cmd)

tool, err := clidocstool.New(clidocstool.Options{
Expand Down
16 changes: 8 additions & 8 deletions pkg/e2e/compose_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func TestLocalComposeRun(t *testing.T) {
c := NewParallelE2eCLI(t, binDir)

t.Run("compose run", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "-T", "back")
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "back")
lines := Lines(res.Stdout())
assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout())
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
res = c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "-T", "back", "echo", "Hello one more time")
res = c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello one more time")
lines = Lines(res.Stdout())
assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout())
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestLocalComposeRun(t *testing.T) {
})

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

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

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

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

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

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

0 comments on commit d1c48b1

Please sign in to comment.