From 919d6c9ed8bba872236f21790d336347e25d95e7 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 9 Jun 2021 10:05:49 +0200 Subject: [PATCH] LogPrinter should not be part of the API Signed-off-by: Nicolas De Loof --- {api => local}/compose/printer.go | 32 ++++++++++++++++--------------- local/compose/start.go | 12 ++++++++---- local/compose/up.go | 2 +- 3 files changed, 26 insertions(+), 20 deletions(-) rename {api => local}/compose/printer.go (75%) diff --git a/api/compose/printer.go b/local/compose/printer.go similarity index 75% rename from api/compose/printer.go rename to local/compose/printer.go index 31c8f1d35..42bee46ec 100644 --- a/api/compose/printer.go +++ b/local/compose/printer.go @@ -19,19 +19,21 @@ package compose import ( "fmt" + "github.com/docker/compose-cli/api/compose" + "github.com/sirupsen/logrus" ) -// LogPrinter watch application containers an collect their logs -type LogPrinter interface { - HandleEvent(event ContainerEvent) +// logPrinter watch application containers an collect their logs +type logPrinter interface { + HandleEvent(event compose.ContainerEvent) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) Cancel() } -// NewLogPrinter builds a LogPrinter passing containers logs to LogConsumer -func NewLogPrinter(consumer LogConsumer) LogPrinter { - queue := make(chan ContainerEvent) +// newLogPrinter builds a LogPrinter passing containers logs to LogConsumer +func newLogPrinter(consumer compose.LogConsumer) logPrinter { + queue := make(chan compose.ContainerEvent) printer := printer{ consumer: consumer, queue: queue, @@ -40,17 +42,17 @@ func NewLogPrinter(consumer LogConsumer) LogPrinter { } func (p *printer) Cancel() { - p.queue <- ContainerEvent{ - Type: UserCancel, + p.queue <- compose.ContainerEvent{ + Type: compose.UserCancel, } } type printer struct { - queue chan ContainerEvent - consumer LogConsumer + queue chan compose.ContainerEvent + consumer compose.LogConsumer } -func (p *printer) HandleEvent(event ContainerEvent) { +func (p *printer) HandleEvent(event compose.ContainerEvent) { p.queue <- event } @@ -64,15 +66,15 @@ func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error event := <-p.queue container := event.Container switch event.Type { - case UserCancel: + case compose.UserCancel: aborting = true - case ContainerEventAttach: + case compose.ContainerEventAttach: if _, ok := containers[container]; ok { continue } containers[container] = struct{}{} p.consumer.Register(container) - case ContainerEventExit: + case compose.ContainerEventExit: if !event.Restarting { delete(containers, container) } @@ -100,7 +102,7 @@ func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error // Last container terminated, done return exitCode, nil } - case ContainerEventLog: + case compose.ContainerEventLog: if !aborting { p.consumer.Log(container, event.Service, event.Line) } diff --git a/local/compose/start.go b/local/compose/start.go index a5c513072..08b758e33 100644 --- a/local/compose/start.go +++ b/local/compose/start.go @@ -34,7 +34,7 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti }) } -func (s *composeService) start(ctx context.Context, project *types.Project, options compose.StartOptions, listener func(event compose.ContainerEvent)) error { +func (s *composeService) start(ctx context.Context, project *types.Project, options compose.StartOptions, listener compose.ContainerEventListener) error { if len(options.AttachTo) == 0 { options.AttachTo = project.ServiceNames() } @@ -47,7 +47,9 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti } eg.Go(func() error { - return s.watchContainers(project, options.AttachTo, listener, attached) + return s.watchContainers(project, options.AttachTo, listener, attached, func(container moby.Container) error { + return s.attachContainer(ctx, container, listener, project) + }) }) } @@ -60,8 +62,10 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti return eg.Wait() } +type containerWatchFn func(container moby.Container) error + // watchContainers uses engine events to capture container start/die and notify ContainerEventListener -func (s *composeService) watchContainers(project *types.Project, services []string, listener compose.ContainerEventListener, containers Containers) error { +func (s *composeService) watchContainers(project *types.Project, services []string, listener compose.ContainerEventListener, containers Containers, onStart containerWatchFn) error { watched := map[string]int{} for _, c := range containers { watched[c.ID] = 0 @@ -118,7 +122,7 @@ func (s *composeService) watchContainers(project *types.Project, services []stri } if mustAttach { // Container restarted, need to re-attach - err := s.attachContainer(ctx, container, listener, project) + err := onStart(container) if err != nil { return err } diff --git a/local/compose/up.go b/local/compose/up.go index 72a30165b..3d2bfd96d 100644 --- a/local/compose/up.go +++ b/local/compose/up.go @@ -47,7 +47,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options return err } - printer := compose.NewLogPrinter(options.Start.Attach) + printer := newLogPrinter(options.Start.Attach) signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)