From f79c28168bf6a95ea0cbd1c298f4a0ff4c3ac36b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Mon, 24 Jun 2024 12:52:15 +0200 Subject: [PATCH] Remove `console.Terminal` check and use `IsTerminal` from `streams.Out` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- go.mod | 2 +- pkg/compose/compose.go | 5 ++--- pkg/progress/writer.go | 38 ++++++++++++++------------------------ 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 29311a4da..feb26312e 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d github.com/buger/goterm v1.0.4 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/davecgh/go-spew v1.1.1 github.com/distribution/reference v0.6.0 @@ -81,6 +80,7 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // 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/errdefs v0.1.0 // indirect github.com/containerd/log v0.1.0 // indirect diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index e8a87561c..86de6eda9 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -20,7 +20,6 @@ import ( "context" "errors" "fmt" - "io" "os" "strconv" "strings" @@ -129,11 +128,11 @@ func (s *composeService) stdin() *streams.In { return s.dockerCli.In() } -func (s *composeService) stderr() io.Writer { +func (s *composeService) stderr() *streams.Out { return s.dockerCli.Err() } -func (s *composeService) stdinfo() io.Writer { +func (s *composeService) stdinfo() *streams.Out { if stdioToStdout { return s.dockerCli.Out() } diff --git a/pkg/progress/writer.go b/pkg/progress/writer.go index f1dec3c79..0f1bd568c 100644 --- a/pkg/progress/writer.go +++ b/pkg/progress/writer.go @@ -21,9 +21,7 @@ import ( "io" "sync" - "github.com/containerd/console" - "github.com/moby/term" - "github.com/sirupsen/logrus" + "github.com/docker/cli/cli/streams" "golang.org/x/sync/errgroup" "github.com/docker/compose/v2/pkg/api" @@ -59,14 +57,14 @@ type progressFunc func(context.Context) error type progressFuncWithStatus func(context.Context) (string, error) // 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) { return "", pf(ctx) }, out, "Running") 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) { return "", pf(ctx) }, 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 -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) w, err := NewWriter(ctx, out, progressTitle) var result string @@ -115,8 +113,8 @@ const ( var Mode = ModeAuto // NewWriter returns a new multi-progress writer -func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer, error) { - _, isTerminal := term.GetFdInfo(out) +func NewWriter(ctx context.Context, out *streams.Out, progressTitle string) (Writer, error) { + isTerminal := out.IsTerminal() dryRun, ok := ctx.Value(api.DryRunKey{}).(bool) if !ok { dryRun = false @@ -124,16 +122,13 @@ func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer if Mode == ModeQuiet { return quiet{}, nil } - f, isConsole := out.(console.File) // see https://github.com/docker/compose/issues/10560 - if Mode == ModeAuto && isTerminal && isConsole { - return newTTYWriter(f, dryRun, progressTitle) + + tty := Mode == ModeTTY + 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{ out: out, @@ -142,14 +137,9 @@ func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer }, nil } -func newTTYWriter(out console.File, dryRun bool, progressTitle string) (Writer, error) { - con, err := console.ConsoleFromFile(out) - if err != nil { - return nil, err - } - +func newTTYWriter(out io.Writer, dryRun bool, progressTitle string) (Writer, error) { return &ttyWriter{ - out: con, + out: out, eventIDs: []string{}, events: map[string]Event{}, repeated: false,