Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce ability to write status messages on stdout #10549

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
_, err := s.build(ctx, project, options)
return err
}, s.stderr(), "Building")
}, s.stdinfo(), "Building")
}

func (s *composeService) build(ctx context.Context, project *types.Project, options api.BuildOptions) (map[string]string, error) { //nolint:gocyclo
Expand Down
17 changes: 17 additions & 0 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"

Expand All @@ -40,6 +41,15 @@
"github.com/docker/compose/v2/pkg/api"
)

var stdioToStdout bool

func init() {
out, ok := os.LookupEnv("COMPOSE_STATUS_STDOUT")
if ok {
stdioToStdout, _ = strconv.ParseBool(out)
}

Check warning on line 50 in pkg/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/compose.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}

// NewComposeService create a local implementation of the compose.Service API
func NewComposeService(dockerCli command.Cli) api.Service {
return &composeService{
Expand Down Expand Up @@ -97,6 +107,13 @@
return s.dockerCli.Err()
}

func (s *composeService) stdinfo() io.Writer {
if stdioToStdout {
return s.dockerCli.Out()
}

Check warning on line 113 in pkg/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/compose.go#L112-L113

Added lines #L112 - L113 were not covered by tests
return s.dockerCli.Err()
}

func getCanonicalContainerName(c moby.Container) string {
if len(c.Names) == 0 {
// corner case, sometime happens on removal. return short ID as a safeguard value
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
func (s *composeService) Copy(ctx context.Context, projectName string, options api.CopyOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.copy(ctx, projectName, options)
}, s.stderr(), "Copying")
}, s.stdinfo(), "Copying")
}

func (s *composeService) copy(ctx context.Context, projectName string, options api.CopyOptions) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import (
func (s *composeService) Create(ctx context.Context, project *types.Project, options api.CreateOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.create(ctx, project, options)
}, s.stderr(), "Creating")
}, s.stdinfo(), "Creating")
}

func (s *composeService) create(ctx context.Context, project *types.Project, options api.CreateOptions) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type downOp func() error
func (s *composeService) Down(ctx context.Context, projectName string, options api.DownOptions) error {
return progress.Run(ctx, func(ctx context.Context) error {
return s.down(ctx, strings.ToLower(projectName), options)
}, s.stderr())
}, s.stdinfo())
}

func (s *composeService) down(ctx context.Context, projectName string, options api.DownOptions) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/compose/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
func (s *composeService) Kill(ctx context.Context, projectName string, options api.KillOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.kill(ctx, strings.ToLower(projectName), options)
}, s.stderr(), "Killing")
}, s.stdinfo(), "Killing")

Check warning on line 34 in pkg/compose/kill.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/kill.go#L34

Added line #L34 was not covered by tests
}

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

Check warning on line 61 in pkg/compose/kill.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/kill.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}

eg, ctx := errgroup.WithContext(ctx)
Expand Down
4 changes: 2 additions & 2 deletions pkg/compose/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
func (s *composeService) Pause(ctx context.Context, projectName string, options api.PauseOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.pause(ctx, strings.ToLower(projectName), options)
}, s.stderr(), "Pausing")
}, s.stdinfo(), "Pausing")
}

func (s *composeService) pause(ctx context.Context, projectName string, options api.PauseOptions) error {
Expand Down Expand Up @@ -62,7 +62,7 @@
func (s *composeService) UnPause(ctx context.Context, projectName string, options api.PauseOptions) error {
return progress.Run(ctx, func(ctx context.Context) error {
return s.unPause(ctx, strings.ToLower(projectName), options)
}, s.stderr())
}, s.stdinfo())

Check warning on line 65 in pkg/compose/pause.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/pause.go#L65

Added line #L65 was not covered by tests
}

func (s *composeService) unPause(ctx context.Context, projectName string, options api.PauseOptions) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/compose/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *composeService) Pull(ctx context.Context, project *types.Project, optio
}
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.pull(ctx, project, options)
}, s.stderr(), "Pulling")
}, s.stdinfo(), "Pulling")
}

func (s *composeService) pull(ctx context.Context, project *types.Project, opts api.PullOptions) error { //nolint:gocyclo
Expand Down Expand Up @@ -305,7 +305,7 @@ func (s *composeService) pullRequiredImages(ctx context.Context, project *types.
}
}
return err
}, s.stderr())
}, s.stdinfo())
}

func isServiceImageToBuild(service types.ServiceConfig, services []types.ServiceConfig) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
}
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.push(ctx, project, options)
}, s.stderr(), "Pushing")
}, s.stdinfo(), "Pushing")

Check warning on line 45 in pkg/compose/push.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/push.go#L45

Added line #L45 was not covered by tests
}

func (s *composeService) push(ctx context.Context, project *types.Project, options api.PushOptions) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/compose/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options
stoppedContainers.forEach(func(c moby.Container) {
names = append(names, getCanonicalContainerName(c))
})
fmt.Fprintln(s.stderr(), names)
fmt.Fprintln(s.stdinfo(), names)

if len(names) == 0 {
fmt.Fprintln(s.stderr(), "No stopped containers")
fmt.Fprintln(s.stdinfo(), "No stopped containers")
return nil
}
msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))
Expand All @@ -95,7 +95,7 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options
}
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.remove(ctx, stoppedContainers, options)
}, s.stderr(), "Removing")
}, s.stdinfo(), "Removing")
}

func (s *composeService) remove(ctx context.Context, containers Containers, options api.RemoveOptions) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
func (s *composeService) Restart(ctx context.Context, projectName string, options api.RestartOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.restart(ctx, strings.ToLower(projectName), options)
}, s.stderr(), "Restarting")
}, s.stdinfo(), "Restarting")
}

func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
func (s *composeService) Start(ctx context.Context, projectName string, options api.StartOptions) error {
return progress.Run(ctx, func(ctx context.Context) error {
return s.start(ctx, strings.ToLower(projectName), options, nil)
}, s.stderr())
}, s.stdinfo())
}

func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func (s *composeService) Stop(ctx context.Context, projectName string, options api.StopOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.stop(ctx, strings.ToLower(projectName), options)
}, s.stderr(), "Stopping")
}, s.stdinfo(), "Stopping")
}

func (s *composeService) stop(ctx context.Context, projectName string, options api.StopOptions) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
return s.start(ctx, project.Name, options.Start, nil)
}
return nil
}, s.stderr())
}, s.stdinfo())
if err != nil {
return err
}
Expand All @@ -59,7 +59,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

stopFunc := func() error {
fmt.Fprintln(s.stderr(), "Aborting on container exit...")
fmt.Fprintln(s.stdinfo(), "Aborting on container exit...")
ctx := context.Background()
return progress.Run(ctx, func(ctx context.Context) error {
go func() {
Expand All @@ -74,7 +74,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
Services: options.Create.Services,
Project: project,
})
}, s.stderr())
}, s.stdinfo())
}

var isTerminated bool
Expand All @@ -83,7 +83,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
<-signalChan
isTerminated = true
printer.Cancel()
fmt.Fprintln(s.stderr(), "Gracefully stopping... (press Ctrl+C again to force)")
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
eg.Go(stopFunc)
}()

Expand Down
10 changes: 5 additions & 5 deletions pkg/compose/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
return err
}

fmt.Fprintf(s.stderr(), "watching %s\n", bc)
fmt.Fprintf(s.stdinfo(), "watching %s\n", bc)

Check warning on line 158 in pkg/compose/watch.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/watch.go#L158

Added line #L158 was not covered by tests
err = watcher.Start()
if err != nil {
return err
Expand Down Expand Up @@ -207,7 +207,7 @@
continue
}

fmt.Fprintf(s.stderr(), "change detected on %s\n", hostPath)
fmt.Fprintf(s.stdinfo(), "change detected on %s\n", hostPath)

f := fileMapping{
HostPath: hostPath,
Expand Down Expand Up @@ -283,7 +283,7 @@
}

fmt.Fprintf(
s.stderr(),
s.stdinfo(),

Check warning on line 286 in pkg/compose/watch.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/watch.go#L286

Added line #L286 was not covered by tests
"Rebuilding %s after changes were detected:%s\n",
strings.Join(serviceNames, ", "),
strings.Join(append([]string{""}, allPaths.Elements()...), "\n - "),
Expand Down Expand Up @@ -319,7 +319,7 @@
if err != nil {
return err
}
fmt.Fprintf(s.stderr(), "%s updated\n", opt.ContainerPath)
fmt.Fprintf(s.stdinfo(), "%s updated\n", opt.ContainerPath)

Check warning on line 322 in pkg/compose/watch.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/watch.go#L322

Added line #L322 was not covered by tests
} else if errors.Is(statErr, fs.ErrNotExist) {
_, err := s.Exec(ctx, project.Name, api.RunOptions{
Service: opt.Service,
Expand All @@ -329,7 +329,7 @@
if err != nil {
logrus.Warnf("failed to delete %q from %s: %v", opt.ContainerPath, opt.Service, err)
}
fmt.Fprintf(s.stderr(), "%s deleted from container\n", opt.ContainerPath)
fmt.Fprintf(s.stdinfo(), "%s deleted from container\n", opt.ContainerPath)

Check warning on line 332 in pkg/compose/watch.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/watch.go#L332

Added line #L332 was not covered by tests
}
}
}
Expand Down