Skip to content

Commit 876625c

Browse files
committed
fix terminal console check
this was broken by cli v27 release and usage of stream writer for the stderr the new type encapsulates the io.Writer and don't pass the console.File type assertion Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
1 parent 6a000dc commit 876625c

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

pkg/compose/compose.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"io"
2423
"os"
2524
"strconv"
2625
"strings"
@@ -129,11 +128,11 @@ func (s *composeService) stdin() *streams.In {
129128
return s.dockerCli.In()
130129
}
131130

132-
func (s *composeService) stderr() io.Writer {
131+
func (s *composeService) stderr() *streams.Out {
133132
return s.dockerCli.Err()
134133
}
135134

136-
func (s *composeService) stdinfo() io.Writer {
135+
func (s *composeService) stdinfo() *streams.Out {
137136
if stdioToStdout {
138137
return s.dockerCli.Out()
139138
}

pkg/progress/console.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright 2020 Docker Compose CLI authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package progress
21+
22+
import (
23+
"os"
24+
25+
"github.com/containerd/console"
26+
)
27+
28+
func checkConsole(fd uintptr) (console.File, bool) {
29+
file := os.NewFile(fd, "")
30+
return file, file != nil
31+
}

pkg/progress/console_windows.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright 2020 Docker Compose CLI authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package progress
21+
22+
import (
23+
"os"
24+
25+
"github.com/containerd/console"
26+
"golang.org/x/sys/windows"
27+
)
28+
29+
func checkConsole(fd uintptr) (console.File, bool) {
30+
var mode uint32
31+
if err := windows.GetConsoleMode(windows.Handle(fd), &mode); err != nil {
32+
return nil, false
33+
}
34+
file := os.NewFile(fd, "")
35+
return file, file != nil
36+
}

pkg/progress/writer.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import (
2222
"sync"
2323

2424
"github.com/containerd/console"
25+
"github.com/docker/cli/cli/streams"
26+
"github.com/docker/compose/v2/pkg/api"
2527
"github.com/moby/term"
2628
"github.com/sirupsen/logrus"
2729
"golang.org/x/sync/errgroup"
28-
29-
"github.com/docker/compose/v2/pkg/api"
3030
)
3131

3232
// Writer can write multiple progress events
@@ -116,15 +116,22 @@ var Mode = ModeAuto
116116

117117
// NewWriter returns a new multi-progress writer
118118
func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer, error) {
119-
_, isTerminal := term.GetFdInfo(out)
119+
var isTerminal bool
120+
var fd uintptr
121+
if stream, ok := out.(*streams.Out); ok {
122+
isTerminal = stream.IsTerminal()
123+
fd = stream.FD()
124+
} else {
125+
fd, isTerminal = term.GetFdInfo(out)
126+
}
120127
dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
121128
if !ok {
122129
dryRun = false
123130
}
124131
if Mode == ModeQuiet {
125132
return quiet{}, nil
126133
}
127-
f, isConsole := out.(console.File) // see https://github.com/docker/compose/issues/10560
134+
f, isConsole := checkConsole(fd) // see https://github.com/docker/compose/issues/10560
128135
if Mode == ModeAuto && isTerminal && isConsole {
129136
return newTTYWriter(f, dryRun, progressTitle)
130137
}

0 commit comments

Comments
 (0)