Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 3202ab9

Browse files
authored
Merge pull request #1765 from ndeloof/log_printer
2 parents b0a6635 + 919d6c9 commit 3202ab9

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

api/compose/printer.go renamed to local/compose/printer.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ package compose
1919
import (
2020
"fmt"
2121

22+
"github.com/docker/compose-cli/api/compose"
23+
2224
"github.com/sirupsen/logrus"
2325
)
2426

25-
// LogPrinter watch application containers an collect their logs
26-
type LogPrinter interface {
27-
HandleEvent(event ContainerEvent)
27+
// logPrinter watch application containers an collect their logs
28+
type logPrinter interface {
29+
HandleEvent(event compose.ContainerEvent)
2830
Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
2931
Cancel()
3032
}
3133

32-
// NewLogPrinter builds a LogPrinter passing containers logs to LogConsumer
33-
func NewLogPrinter(consumer LogConsumer) LogPrinter {
34-
queue := make(chan ContainerEvent)
34+
// newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
35+
func newLogPrinter(consumer compose.LogConsumer) logPrinter {
36+
queue := make(chan compose.ContainerEvent)
3537
printer := printer{
3638
consumer: consumer,
3739
queue: queue,
@@ -40,17 +42,17 @@ func NewLogPrinter(consumer LogConsumer) LogPrinter {
4042
}
4143

4244
func (p *printer) Cancel() {
43-
p.queue <- ContainerEvent{
44-
Type: UserCancel,
45+
p.queue <- compose.ContainerEvent{
46+
Type: compose.UserCancel,
4547
}
4648
}
4749

4850
type printer struct {
49-
queue chan ContainerEvent
50-
consumer LogConsumer
51+
queue chan compose.ContainerEvent
52+
consumer compose.LogConsumer
5153
}
5254

53-
func (p *printer) HandleEvent(event ContainerEvent) {
55+
func (p *printer) HandleEvent(event compose.ContainerEvent) {
5456
p.queue <- event
5557
}
5658

@@ -64,15 +66,15 @@ func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error
6466
event := <-p.queue
6567
container := event.Container
6668
switch event.Type {
67-
case UserCancel:
69+
case compose.UserCancel:
6870
aborting = true
69-
case ContainerEventAttach:
71+
case compose.ContainerEventAttach:
7072
if _, ok := containers[container]; ok {
7173
continue
7274
}
7375
containers[container] = struct{}{}
7476
p.consumer.Register(container)
75-
case ContainerEventExit:
77+
case compose.ContainerEventExit:
7678
if !event.Restarting {
7779
delete(containers, container)
7880
}
@@ -100,7 +102,7 @@ func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error
100102
// Last container terminated, done
101103
return exitCode, nil
102104
}
103-
case ContainerEventLog:
105+
case compose.ContainerEventLog:
104106
if !aborting {
105107
p.consumer.Log(container, event.Service, event.Line)
106108
}

local/compose/start.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti
3434
})
3535
}
3636

37-
func (s *composeService) start(ctx context.Context, project *types.Project, options compose.StartOptions, listener func(event compose.ContainerEvent)) error {
37+
func (s *composeService) start(ctx context.Context, project *types.Project, options compose.StartOptions, listener compose.ContainerEventListener) error {
3838
if len(options.AttachTo) == 0 {
3939
options.AttachTo = project.ServiceNames()
4040
}
@@ -47,7 +47,9 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
4747
}
4848

4949
eg.Go(func() error {
50-
return s.watchContainers(project, options.AttachTo, listener, attached)
50+
return s.watchContainers(project, options.AttachTo, listener, attached, func(container moby.Container) error {
51+
return s.attachContainer(ctx, container, listener, project)
52+
})
5153
})
5254
}
5355

@@ -60,8 +62,10 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
6062
return eg.Wait()
6163
}
6264

65+
type containerWatchFn func(container moby.Container) error
66+
6367
// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
64-
func (s *composeService) watchContainers(project *types.Project, services []string, listener compose.ContainerEventListener, containers Containers) error {
68+
func (s *composeService) watchContainers(project *types.Project, services []string, listener compose.ContainerEventListener, containers Containers, onStart containerWatchFn) error {
6569
watched := map[string]int{}
6670
for _, c := range containers {
6771
watched[c.ID] = 0
@@ -118,7 +122,7 @@ func (s *composeService) watchContainers(project *types.Project, services []stri
118122
}
119123
if mustAttach {
120124
// Container restarted, need to re-attach
121-
err := s.attachContainer(ctx, container, listener, project)
125+
err := onStart(container)
122126
if err != nil {
123127
return err
124128
}

local/compose/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
4747
return err
4848
}
4949

50-
printer := compose.NewLogPrinter(options.Start.Attach)
50+
printer := newLogPrinter(options.Start.Attach)
5151

5252
signalChan := make(chan os.Signal, 1)
5353
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

0 commit comments

Comments
 (0)