Skip to content

Commit

Permalink
Remove console.Terminal check and use IsTerminal from streams.Out
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
vvoland authored and glours committed Jun 24, 2024
1 parent 6a000dc commit f79c281
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -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()
}
Expand Down
38 changes: 14 additions & 24 deletions pkg/progress/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -59,22 +57,22 @@ 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)
return err
}

// 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
Expand Down Expand Up @@ -115,25 +113,22 @@ 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
}
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,
Expand All @@ -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,
Expand Down

0 comments on commit f79c281

Please sign in to comment.