Skip to content

Commit 24f8327

Browse files
committed
don't assume os.Stdout and rely on dockerCLI.streams
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent dacf243 commit 24f8327

File tree

21 files changed

+138
-104
lines changed

21 files changed

+138
-104
lines changed

cmd/compose/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var printerModes = []string{
7373
buildx.PrinterModeQuiet,
7474
}
7575

76-
func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
76+
func buildCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
7777
opts := buildOptions{
7878
ProjectOptions: p,
7979
}
@@ -82,7 +82,7 @@ func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
8282
Short: "Build or rebuild services",
8383
PreRunE: Adapt(func(ctx context.Context, args []string) error {
8484
if opts.memory != "" {
85-
fmt.Println("WARNING --memory is ignored as not supported in buildkit.")
85+
fmt.Fprintln(streams.Err(), "WARNING --memory is ignored as not supported in buildkit.")
8686
}
8787
if opts.quiet {
8888
opts.progress = buildx.PrinterModeQuiet

cmd/compose/compose.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/docker/buildx/util/logutil"
3232
dockercli "github.com/docker/cli/cli"
3333
"github.com/docker/cli/cli-plugins/manager"
34-
"github.com/docker/cli/cli/command"
3534
"github.com/morikuni/aec"
3635
"github.com/pkg/errors"
3736
"github.com/sirupsen/logrus"
@@ -243,7 +242,7 @@ func RunningAsStandalone() bool {
243242
}
244243

245244
// RootCommand returns the compose command with its child commands
246-
func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //nolint:gocyclo
245+
func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //nolint:gocyclo
247246
// filter out useless commandConn.CloseWrite warning message that can occur
248247
// when using a remote context that is unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
249248
// https://github.com/docker/cli/blob/e1f24d3c93df6752d3c27c8d61d18260f141310c/cli/connhelper/commandconn/commandconn.go#L203-L215
@@ -305,7 +304,7 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
305304
if verbose {
306305
logrus.SetLevel(logrus.TraceLevel)
307306
}
308-
formatter.SetANSIMode(ansi)
307+
formatter.SetANSIMode(streams, ansi)
309308
switch ansi {
310309
case "never":
311310
progress.Mode = progress.ModePlain
@@ -333,27 +332,27 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
333332
}
334333

335334
c.AddCommand(
336-
upCommand(&opts, backend),
335+
upCommand(&opts, streams, backend),
337336
downCommand(&opts, backend),
338337
startCommand(&opts, backend),
339338
restartCommand(&opts, backend),
340339
stopCommand(&opts, backend),
341-
psCommand(&opts, backend),
342-
listCommand(backend),
343-
logsCommand(&opts, backend),
344-
convertCommand(&opts, backend),
340+
psCommand(&opts, streams, backend),
341+
listCommand(streams, backend),
342+
logsCommand(&opts, streams, backend),
343+
convertCommand(&opts, streams, backend),
345344
killCommand(&opts, backend),
346-
runCommand(&opts, dockerCli, backend),
345+
runCommand(&opts, streams, backend),
347346
removeCommand(&opts, backend),
348-
execCommand(&opts, dockerCli, backend),
347+
execCommand(&opts, streams, backend),
349348
pauseCommand(&opts, backend),
350349
unpauseCommand(&opts, backend),
351-
topCommand(&opts, backend),
352-
eventsCommand(&opts, backend),
353-
portCommand(&opts, backend),
354-
imagesCommand(&opts, backend),
350+
topCommand(&opts, streams, backend),
351+
eventsCommand(&opts, streams, backend),
352+
portCommand(&opts, streams, backend),
353+
imagesCommand(&opts, streams, backend),
355354
versionCommand(),
356-
buildCommand(&opts, backend),
355+
buildCommand(&opts, streams, backend),
357356
pushCommand(&opts, backend),
358357
pullCommand(&opts, backend),
359358
createCommand(&opts, backend),

cmd/compose/convert.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type convertOptions struct {
5050
noConsistency bool
5151
}
5252

53-
func convertCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
53+
func convertCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
5454
opts := convertOptions{
5555
ProjectOptions: p,
5656
}
@@ -73,22 +73,22 @@ func convertCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
7373
}),
7474
RunE: Adapt(func(ctx context.Context, args []string) error {
7575
if opts.services {
76-
return runServices(opts)
76+
return runServices(streams, opts)
7777
}
7878
if opts.volumes {
79-
return runVolumes(opts)
79+
return runVolumes(streams, opts)
8080
}
8181
if opts.hash != "" {
82-
return runHash(opts)
82+
return runHash(streams, opts)
8383
}
8484
if opts.profiles {
85-
return runProfiles(opts, args)
85+
return runProfiles(streams, opts, args)
8686
}
8787
if opts.images {
88-
return runConfigImages(opts, args)
88+
return runConfigImages(streams, opts, args)
8989
}
9090

91-
return runConvert(ctx, backend, opts, args)
91+
return runConvert(ctx, streams, backend, opts, args)
9292
}),
9393
ValidArgsFunction: completeServiceNames(p),
9494
}
@@ -110,7 +110,7 @@ func convertCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
110110
return cmd
111111
}
112112

113-
func runConvert(ctx context.Context, backend api.Service, opts convertOptions, services []string) error {
113+
func runConvert(ctx context.Context, streams api.Streams, backend api.Service, opts convertOptions, services []string) error {
114114
var content []byte
115115
project, err := opts.ToProject(services,
116116
cli.WithInterpolation(!opts.noInterpolate),
@@ -139,7 +139,7 @@ func runConvert(ctx context.Context, backend api.Service, opts convertOptions, s
139139
return nil
140140
}
141141

142-
var out io.Writer = os.Stdout
142+
var out io.Writer = streams.Out()
143143
if opts.Output != "" && len(content) > 0 {
144144
file, err := os.Create(opts.Output)
145145
if err != nil {
@@ -151,29 +151,29 @@ func runConvert(ctx context.Context, backend api.Service, opts convertOptions, s
151151
return err
152152
}
153153

154-
func runServices(opts convertOptions) error {
154+
func runServices(streams api.Streams, opts convertOptions) error {
155155
project, err := opts.ToProject(nil)
156156
if err != nil {
157157
return err
158158
}
159159
return project.WithServices(project.ServiceNames(), func(s types.ServiceConfig) error {
160-
fmt.Println(s.Name)
160+
fmt.Fprintln(streams.Out(), s.Name)
161161
return nil
162162
})
163163
}
164164

165-
func runVolumes(opts convertOptions) error {
165+
func runVolumes(streams api.Streams, opts convertOptions) error {
166166
project, err := opts.ToProject(nil)
167167
if err != nil {
168168
return err
169169
}
170170
for n := range project.Volumes {
171-
fmt.Println(n)
171+
fmt.Fprintln(streams.Out(), n)
172172
}
173173
return nil
174174
}
175175

176-
func runHash(opts convertOptions) error {
176+
func runHash(streams api.Streams, opts convertOptions) error {
177177
var services []string
178178
if opts.hash != "*" {
179179
services = append(services, strings.Split(opts.hash, ",")...)
@@ -187,12 +187,12 @@ func runHash(opts convertOptions) error {
187187
if err != nil {
188188
return err
189189
}
190-
fmt.Printf("%s %s\n", s.Name, hash)
190+
fmt.Fprintf(streams.Out(), "%s %s\n", s.Name, hash)
191191
}
192192
return nil
193193
}
194194

195-
func runProfiles(opts convertOptions, services []string) error {
195+
func runProfiles(streams api.Streams, opts convertOptions, services []string) error {
196196
set := map[string]struct{}{}
197197
project, err := opts.ToProject(services)
198198
if err != nil {
@@ -209,21 +209,21 @@ func runProfiles(opts convertOptions, services []string) error {
209209
}
210210
sort.Strings(profiles)
211211
for _, p := range profiles {
212-
fmt.Println(p)
212+
fmt.Fprintln(streams.Out(), p)
213213
}
214214
return nil
215215
}
216216

217-
func runConfigImages(opts convertOptions, services []string) error {
217+
func runConfigImages(streams api.Streams, opts convertOptions, services []string) error {
218218
project, err := opts.ToProject(services)
219219
if err != nil {
220220
return err
221221
}
222222
for _, s := range project.Services {
223223
if s.Image != "" {
224-
fmt.Println(s.Image)
224+
fmt.Fprintln(streams.Out(), s.Image)
225225
} else {
226-
fmt.Printf("%s%s%s\n", project.Name, api.Separator, s.Name)
226+
fmt.Fprintf(streams.Out(), "%s%s%s\n", project.Name, api.Separator, s.Name)
227227
}
228228
}
229229
return nil

cmd/compose/events.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type eventsOpts struct {
3131
json bool
3232
}
3333

34-
func eventsCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
34+
func eventsCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
3535
opts := eventsOpts{
3636
composeOptions: &composeOptions{
3737
ProjectOptions: p,
@@ -41,7 +41,7 @@ func eventsCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
4141
Use: "events [OPTIONS] [SERVICE...]",
4242
Short: "Receive real time events from containers.",
4343
RunE: Adapt(func(ctx context.Context, args []string) error {
44-
return runEvents(ctx, backend, opts, args)
44+
return runEvents(ctx, streams, backend, opts, args)
4545
}),
4646
ValidArgsFunction: completeServiceNames(p),
4747
}
@@ -50,7 +50,7 @@ func eventsCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
5050
return cmd
5151
}
5252

53-
func runEvents(ctx context.Context, backend api.Service, opts eventsOpts, services []string) error {
53+
func runEvents(ctx context.Context, streams api.Streams, backend api.Service, opts eventsOpts, services []string) error {
5454
name, err := opts.toProjectName()
5555
if err != nil {
5656
return err
@@ -71,9 +71,9 @@ func runEvents(ctx context.Context, backend api.Service, opts eventsOpts, servic
7171
if err != nil {
7272
return err
7373
}
74-
fmt.Println(string(marshal))
74+
fmt.Fprintln(streams.Out(), string(marshal))
7575
} else {
76-
fmt.Println(event)
76+
fmt.Fprintln(streams.Out(), event)
7777
}
7878
return nil
7979
},

cmd/compose/exec.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/compose-spec/compose-go/types"
2323
"github.com/docker/cli/cli"
24-
"github.com/docker/cli/cli/command"
2524
"github.com/docker/compose/v2/pkg/api"
2625
"github.com/docker/compose/v2/pkg/compose"
2726
"github.com/spf13/cobra"
@@ -43,7 +42,7 @@ type execOpts struct {
4342
interactive bool
4443
}
4544

46-
func execCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
45+
func execCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
4746
opts := execOpts{
4847
composeOptions: &composeOptions{
4948
ProjectOptions: p,
@@ -69,7 +68,7 @@ func execCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service)
6968
runCmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].")
7069
runCmd.Flags().BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the process.")
7170
runCmd.Flags().StringVarP(&opts.user, "user", "u", "", "Run the command as this user.")
72-
runCmd.Flags().BoolVarP(&opts.noTty, "no-TTY", "T", !dockerCli.Out().IsTerminal(), "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
71+
runCmd.Flags().BoolVarP(&opts.noTty, "no-TTY", "T", !streams.Out().IsTerminal(), "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
7372
runCmd.Flags().StringVarP(&opts.workingDir, "workdir", "w", "", "Path to workdir directory for this command.")
7473

7574
runCmd.Flags().BoolVarP(&opts.interactive, "interactive", "i", true, "Keep STDIN open even if not attached.")

cmd/compose/images.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23-
"os"
2423
"sort"
2524
"strings"
2625

@@ -39,15 +38,15 @@ type imageOptions struct {
3938
Format string
4039
}
4140

42-
func imagesCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
41+
func imagesCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
4342
opts := imageOptions{
4443
ProjectOptions: p,
4544
}
4645
imgCmd := &cobra.Command{
4746
Use: "images [OPTIONS] [SERVICE...]",
4847
Short: "List images used by the created containers",
4948
RunE: Adapt(func(ctx context.Context, args []string) error {
50-
return runImages(ctx, backend, opts, args)
49+
return runImages(ctx, streams, backend, opts, args)
5150
}),
5251
ValidArgsFunction: completeServiceNames(p),
5352
}
@@ -56,7 +55,7 @@ func imagesCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
5655
return imgCmd
5756
}
5857

59-
func runImages(ctx context.Context, backend api.Service, opts imageOptions, services []string) error {
58+
func runImages(ctx context.Context, streams api.Streams, backend api.Service, opts imageOptions, services []string) error {
6059
projectName, err := opts.toProjectName()
6160
if err != nil {
6261
return err
@@ -81,7 +80,7 @@ func runImages(ctx context.Context, backend api.Service, opts imageOptions, serv
8180
}
8281
}
8382
for _, img := range ids {
84-
fmt.Println(img)
83+
fmt.Fprintln(streams.Out(), img)
8584
}
8685
return nil
8786
}
@@ -90,7 +89,7 @@ func runImages(ctx context.Context, backend api.Service, opts imageOptions, serv
9089
return images[i].ContainerName < images[j].ContainerName
9190
})
9291

93-
return formatter.Print(images, opts.Format, os.Stdout,
92+
return formatter.Print(images, opts.Format, streams.Out(),
9493
func(w io.Writer) {
9594
for _, img := range images {
9695
id := stringid.TruncateID(img.ID)

cmd/compose/list.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23-
"os"
2423
"strings"
2524

2625
"github.com/docker/compose/v2/cmd/formatter"
@@ -38,13 +37,13 @@ type lsOptions struct {
3837
Filter opts.FilterOpt
3938
}
4039

41-
func listCommand(backend api.Service) *cobra.Command {
40+
func listCommand(streams api.Streams, backend api.Service) *cobra.Command {
4241
lsOpts := lsOptions{Filter: opts.NewFilterOpt()}
4342
lsCmd := &cobra.Command{
4443
Use: "ls [OPTIONS]",
4544
Short: "List running compose projects",
4645
RunE: Adapt(func(ctx context.Context, args []string) error {
47-
return runList(ctx, backend, lsOpts)
46+
return runList(ctx, streams, backend, lsOpts)
4847
}),
4948
Args: cobra.NoArgs,
5049
ValidArgsFunction: noCompletion(),
@@ -61,7 +60,7 @@ var acceptedListFilters = map[string]bool{
6160
"name": true,
6261
}
6362

64-
func runList(ctx context.Context, backend api.Service, lsOpts lsOptions) error {
63+
func runList(ctx context.Context, streams api.Streams, backend api.Service, lsOpts lsOptions) error {
6564
filters := lsOpts.Filter.Value()
6665
err := filters.Validate(acceptedListFilters)
6766
if err != nil {
@@ -74,7 +73,7 @@ func runList(ctx context.Context, backend api.Service, lsOpts lsOptions) error {
7473
}
7574
if lsOpts.Quiet {
7675
for _, s := range stackList {
77-
fmt.Println(s.Name)
76+
fmt.Fprintln(streams.Out(), s.Name)
7877
}
7978
return nil
8079
}
@@ -91,7 +90,7 @@ func runList(ctx context.Context, backend api.Service, lsOpts lsOptions) error {
9190
}
9291

9392
view := viewFromStackList(stackList)
94-
return formatter.Print(view, lsOpts.Format, os.Stdout, func(w io.Writer) {
93+
return formatter.Print(view, lsOpts.Format, streams.Out(), func(w io.Writer) {
9594
for _, stack := range view {
9695
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", stack.Name, stack.Status, stack.ConfigFiles)
9796
}

0 commit comments

Comments
 (0)