Remove console.Terminal check and use IsTerminal from streams.Out

docker/cli v27 changed the return value of `Err()` to `streams.Out`
which made the typecheck for `console.File` fail.

The check is no longer needed due to the `IsTerminal` method present in
`streams.Out` which also has a special handling for Windows console.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2024-06-24 12:52:15 +02:00 committed by Guillaume Lours
parent 6a000dcff1
commit f79c28168b
3 changed files with 17 additions and 28 deletions

2
go.mod
View File

@ -8,7 +8,6 @@ require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/buger/goterm v1.0.4 github.com/buger/goterm v1.0.4
github.com/compose-spec/compose-go/v2 v2.1.3 github.com/compose-spec/compose-go/v2 v2.1.3
github.com/containerd/console v1.0.4
github.com/containerd/containerd v1.7.18 github.com/containerd/containerd v1.7.18
github.com/davecgh/go-spew v1.1.1 github.com/davecgh/go-spew v1.1.1
github.com/distribution/reference v0.6.0 github.com/distribution/reference v0.6.0
@ -81,6 +80,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/console v1.0.4 // indirect
github.com/containerd/continuity v0.4.3 // indirect github.com/containerd/continuity v0.4.3 // indirect
github.com/containerd/errdefs v0.1.0 // indirect github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect github.com/containerd/log v0.1.0 // indirect

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -129,11 +128,11 @@ func (s *composeService) stdin() *streams.In {
return s.dockerCli.In() return s.dockerCli.In()
} }
func (s *composeService) stderr() io.Writer { func (s *composeService) stderr() *streams.Out {
return s.dockerCli.Err() return s.dockerCli.Err()
} }
func (s *composeService) stdinfo() io.Writer { func (s *composeService) stdinfo() *streams.Out {
if stdioToStdout { if stdioToStdout {
return s.dockerCli.Out() return s.dockerCli.Out()
} }

View File

@ -21,9 +21,7 @@ import (
"io" "io"
"sync" "sync"
"github.com/containerd/console" "github.com/docker/cli/cli/streams"
"github.com/moby/term"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
@ -59,14 +57,14 @@ type progressFunc func(context.Context) error
type progressFuncWithStatus func(context.Context) (string, error) type progressFuncWithStatus func(context.Context) (string, error)
// Run will run a writer and the progress function in parallel // Run will run a writer and the progress function in parallel
func Run(ctx context.Context, pf progressFunc, out io.Writer) error { func Run(ctx context.Context, pf progressFunc, out *streams.Out) error {
_, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) { _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
return "", pf(ctx) return "", pf(ctx)
}, out, "Running") }, out, "Running")
return err return err
} }
func RunWithTitle(ctx context.Context, pf progressFunc, out io.Writer, progressTitle string) error { func RunWithTitle(ctx context.Context, pf progressFunc, out *streams.Out, progressTitle string) error {
_, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) { _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
return "", pf(ctx) return "", pf(ctx)
}, out, progressTitle) }, out, progressTitle)
@ -74,7 +72,7 @@ func RunWithTitle(ctx context.Context, pf progressFunc, out io.Writer, progressT
} }
// RunWithStatus will run a writer and the progress function in parallel and return a status // RunWithStatus will run a writer and the progress function in parallel and return a status
func RunWithStatus(ctx context.Context, pf progressFuncWithStatus, out io.Writer, progressTitle string) (string, error) { func RunWithStatus(ctx context.Context, pf progressFuncWithStatus, out *streams.Out, progressTitle string) (string, error) {
eg, _ := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
w, err := NewWriter(ctx, out, progressTitle) w, err := NewWriter(ctx, out, progressTitle)
var result string var result string
@ -115,8 +113,8 @@ const (
var Mode = ModeAuto var Mode = ModeAuto
// NewWriter returns a new multi-progress writer // NewWriter returns a new multi-progress writer
func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer, error) { func NewWriter(ctx context.Context, out *streams.Out, progressTitle string) (Writer, error) {
_, isTerminal := term.GetFdInfo(out) isTerminal := out.IsTerminal()
dryRun, ok := ctx.Value(api.DryRunKey{}).(bool) dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
if !ok { if !ok {
dryRun = false dryRun = false
@ -124,16 +122,13 @@ func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer
if Mode == ModeQuiet { if Mode == ModeQuiet {
return quiet{}, nil return quiet{}, nil
} }
f, isConsole := out.(console.File) // see https://github.com/docker/compose/issues/10560
if Mode == ModeAuto && isTerminal && isConsole { tty := Mode == ModeTTY
return newTTYWriter(f, dryRun, progressTitle) if Mode == ModeAuto && isTerminal {
} tty = true
if Mode == ModeTTY {
if !isConsole {
logrus.Warn("Terminal is not a POSIX console")
} else {
return newTTYWriter(f, dryRun, progressTitle)
} }
if tty {
return newTTYWriter(out, dryRun, progressTitle)
} }
return &plainWriter{ return &plainWriter{
out: out, out: out,
@ -142,14 +137,9 @@ func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer
}, nil }, nil
} }
func newTTYWriter(out console.File, dryRun bool, progressTitle string) (Writer, error) { func newTTYWriter(out io.Writer, dryRun bool, progressTitle string) (Writer, error) {
con, err := console.ConsoleFromFile(out)
if err != nil {
return nil, err
}
return &ttyWriter{ return &ttyWriter{
out: con, out: out,
eventIDs: []string{}, eventIDs: []string{},
events: map[string]Event{}, events: map[string]Event{},
repeated: false, repeated: false,