Skip to content

Commit a14abb9

Browse files
authored
cli: option to write status messages on stdout (docker#10549)
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 0363d92 commit a14abb9

File tree

15 files changed

+44
-26
lines changed

15 files changed

+44
-26
lines changed

pkg/compose/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
5252
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
5353
_, err := s.build(ctx, project, options)
5454
return err
55-
}, s.stderr(), "Building")
55+
}, s.stdinfo(), "Building")
5656
}
5757

5858
func (s *composeService) build(ctx context.Context, project *types.Project, options api.BuildOptions) (map[string]string, error) { //nolint:gocyclo

pkg/compose/compose.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24+
"os"
2425
"strconv"
2526
"strings"
2627

@@ -40,6 +41,15 @@ import (
4041
"github.com/docker/compose/v2/pkg/api"
4142
)
4243

44+
var stdioToStdout bool
45+
46+
func init() {
47+
out, ok := os.LookupEnv("COMPOSE_STATUS_STDOUT")
48+
if ok {
49+
stdioToStdout, _ = strconv.ParseBool(out)
50+
}
51+
}
52+
4353
// NewComposeService create a local implementation of the compose.Service API
4454
func NewComposeService(dockerCli command.Cli) api.Service {
4555
return &composeService{
@@ -97,6 +107,13 @@ func (s *composeService) stderr() io.Writer {
97107
return s.dockerCli.Err()
98108
}
99109

110+
func (s *composeService) stdinfo() io.Writer {
111+
if stdioToStdout {
112+
return s.dockerCli.Out()
113+
}
114+
return s.dockerCli.Err()
115+
}
116+
100117
func getCanonicalContainerName(c moby.Container) string {
101118
if len(c.Names) == 0 {
102119
// corner case, sometime happens on removal. return short ID as a safeguard value

pkg/compose/cp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const (
4646
func (s *composeService) Copy(ctx context.Context, projectName string, options api.CopyOptions) error {
4747
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
4848
return s.copy(ctx, projectName, options)
49-
}, s.stderr(), "Copying")
49+
}, s.stdinfo(), "Copying")
5050
}
5151

5252
func (s *composeService) copy(ctx context.Context, projectName string, options api.CopyOptions) error {

pkg/compose/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import (
5151
func (s *composeService) Create(ctx context.Context, project *types.Project, options api.CreateOptions) error {
5252
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
5353
return s.create(ctx, project, options)
54-
}, s.stderr(), "Creating")
54+
}, s.stdinfo(), "Creating")
5555
}
5656

5757
func (s *composeService) create(ctx context.Context, project *types.Project, options api.CreateOptions) error {

pkg/compose/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type downOp func() error
4141
func (s *composeService) Down(ctx context.Context, projectName string, options api.DownOptions) error {
4242
return progress.Run(ctx, func(ctx context.Context) error {
4343
return s.down(ctx, strings.ToLower(projectName), options)
44-
}, s.stderr())
44+
}, s.stdinfo())
4545
}
4646

4747
func (s *composeService) down(ctx context.Context, projectName string, options api.DownOptions) error {

pkg/compose/kill.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
func (s *composeService) Kill(ctx context.Context, projectName string, options api.KillOptions) error {
3232
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
3333
return s.kill(ctx, strings.ToLower(projectName), options)
34-
}, s.stderr(), "Killing")
34+
}, s.stdinfo(), "Killing")
3535
}
3636

3737
func (s *composeService) kill(ctx context.Context, projectName string, options api.KillOptions) error {
@@ -57,7 +57,8 @@ func (s *composeService) kill(ctx context.Context, projectName string, options a
5757
containers = containers.filter(isService(project.ServiceNames()...))
5858
}
5959
if len(containers) == 0 {
60-
fmt.Fprintf(s.stderr(), "no container to kill")
60+
fmt.Fprintf(s.stdinfo(), "no container to kill")
61+
return nil
6162
}
6263

6364
eg, ctx := errgroup.WithContext(ctx)

pkg/compose/pause.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
func (s *composeService) Pause(ctx context.Context, projectName string, options api.PauseOptions) error {
3131
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
3232
return s.pause(ctx, strings.ToLower(projectName), options)
33-
}, s.stderr(), "Pausing")
33+
}, s.stdinfo(), "Pausing")
3434
}
3535

3636
func (s *composeService) pause(ctx context.Context, projectName string, options api.PauseOptions) error {
@@ -62,7 +62,7 @@ func (s *composeService) pause(ctx context.Context, projectName string, options
6262
func (s *composeService) UnPause(ctx context.Context, projectName string, options api.PauseOptions) error {
6363
return progress.Run(ctx, func(ctx context.Context) error {
6464
return s.unPause(ctx, strings.ToLower(projectName), options)
65-
}, s.stderr())
65+
}, s.stdinfo())
6666
}
6767

6868
func (s *composeService) unPause(ctx context.Context, projectName string, options api.PauseOptions) error {

pkg/compose/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (s *composeService) Pull(ctx context.Context, project *types.Project, optio
4444
}
4545
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
4646
return s.pull(ctx, project, options)
47-
}, s.stderr(), "Pulling")
47+
}, s.stdinfo(), "Pulling")
4848
}
4949

5050
func (s *composeService) pull(ctx context.Context, project *types.Project, opts api.PullOptions) error { //nolint:gocyclo
@@ -305,7 +305,7 @@ func (s *composeService) pullRequiredImages(ctx context.Context, project *types.
305305
}
306306
}
307307
return err
308-
}, s.stderr())
308+
}, s.stdinfo())
309309
}
310310

311311
func isServiceImageToBuild(service types.ServiceConfig, services []types.ServiceConfig) bool {

pkg/compose/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (s *composeService) Push(ctx context.Context, project *types.Project, optio
4242
}
4343
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
4444
return s.push(ctx, project, options)
45-
}, s.stderr(), "Pushing")
45+
}, s.stdinfo(), "Pushing")
4646
}
4747

4848
func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error {

pkg/compose/remove.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options
7575
stoppedContainers.forEach(func(c moby.Container) {
7676
names = append(names, getCanonicalContainerName(c))
7777
})
78-
fmt.Fprintln(s.stderr(), names)
78+
fmt.Fprintln(s.stdinfo(), names)
7979

8080
if len(names) == 0 {
81-
fmt.Fprintln(s.stderr(), "No stopped containers")
81+
fmt.Fprintln(s.stdinfo(), "No stopped containers")
8282
return nil
8383
}
8484
msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))
@@ -95,7 +95,7 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options
9595
}
9696
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
9797
return s.remove(ctx, stoppedContainers, options)
98-
}, s.stderr(), "Removing")
98+
}, s.stdinfo(), "Removing")
9999
}
100100

101101
func (s *composeService) remove(ctx context.Context, containers Containers, options api.RemoveOptions) error {

0 commit comments

Comments
 (0)