Просмотр исходного кода

LogPrinter should not be part of the API

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 лет назад
Родитель
Сommit
919d6c9ed8
3 измененных файлов с 26 добавлено и 20 удалено
  1. 17 15
      local/compose/printer.go
  2. 8 4
      local/compose/start.go
  3. 1 1
      local/compose/up.go

+ 17 - 15
api/compose/printer.go → 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)
 			}

+ 8 - 4
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
 					}

+ 1 - 1
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)