Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 1d38001

Browse files
committed
automatically disable TTY if shell isn't a terminal
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 4987a52 commit 1d38001

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

cli/cmd/compose/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func execCommand(p *projectOptions, backend compose.Service) *cobra.Command {
6868
runCmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].")
6969
runCmd.Flags().BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the process.")
7070
runCmd.Flags().StringVarP(&opts.user, "user", "u", "", "Run the command as this user.")
71-
runCmd.Flags().BoolVarP(&opts.noTty, "", "T", false, "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
71+
runCmd.Flags().BoolVarP(&opts.noTty, "", "T", notAtTTY(), "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
7272
runCmd.Flags().StringVarP(&opts.workingDir, "workdir", "w", "", "Path to workdir directory for this command.")
7373

7474
runCmd.Flags().SetInterspersed(false)

cli/cmd/compose/run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/compose-spec/compose-go/loader"
2626
"github.com/compose-spec/compose-go/types"
27+
"github.com/mattn/go-isatty"
2728
"github.com/mattn/go-shellwords"
2829
"github.com/spf13/cobra"
2930

@@ -132,7 +133,7 @@ func runCommand(p *projectOptions, backend compose.Service) *cobra.Command {
132133
flags.StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables")
133134
flags.StringArrayVarP(&opts.labels, "labels", "l", []string{}, "Add or override a label")
134135
flags.BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits")
135-
flags.BoolVarP(&opts.noTty, "no-TTY", "T", false, "Disable pseudo-noTty allocation. By default docker compose run allocates a TTY")
136+
flags.BoolVarP(&opts.noTty, "no-TTY", "T", notAtTTY(), "Disable pseudo-noTty allocation. By default docker compose run allocates a TTY")
136137
flags.StringVar(&opts.name, "name", "", " Assign a name to the container")
137138
flags.StringVarP(&opts.user, "user", "u", "", "Run as specified username or uid")
138139
flags.StringVarP(&opts.workdir, "workdir", "w", "", "Working directory inside the container")
@@ -147,6 +148,10 @@ func runCommand(p *projectOptions, backend compose.Service) *cobra.Command {
147148
return cmd
148149
}
149150

151+
func notAtTTY() bool {
152+
return !isatty.IsTerminal(os.Stdout.Fd())
153+
}
154+
150155
func runRun(ctx context.Context, backend compose.Service, project *types.Project, opts runOptions) error {
151156
err := opts.apply(project)
152157
if err != nil {

0 commit comments

Comments
 (0)