Skip to content

Commit db2c4b3

Browse files
committed
register TTYWritter as an Event Processor
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent c366fc9 commit db2c4b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+408
-709
lines changed

cmd/compose/compose.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,29 +508,42 @@ func RootCommand(dockerCli command.Cli, backendOptions *BackendOptions) *cobra.C
508508
ui.Mode = ui.ModeTTY
509509
}
510510

511+
var ep ui.EventProcessor
511512
switch opts.Progress {
512513
case "", ui.ModeAuto:
513-
if ansi == "never" {
514+
switch {
515+
case ansi == "never":
514516
ui.Mode = ui.ModePlain
517+
ep = ui.NewPlainWriter(dockerCli.Err())
518+
case dockerCli.Out().IsTerminal():
519+
ep = ui.NewTTYWriter(dockerCli.Err())
520+
default:
521+
ep = ui.NewPlainWriter(dockerCli.Err())
515522
}
516523
case ui.ModeTTY:
517524
if ansi == "never" {
518525
return fmt.Errorf("can't use --progress tty while ANSI support is disabled")
519526
}
520527
ui.Mode = ui.ModeTTY
528+
ep = ui.NewTTYWriter(dockerCli.Err())
529+
521530
case ui.ModePlain:
522531
if ansi == "always" {
523532
return fmt.Errorf("can't use --progress plain while ANSI support is forced")
524533
}
525534
ui.Mode = ui.ModePlain
535+
ep = ui.NewPlainWriter(dockerCli.Err())
526536
case ui.ModeQuiet, "none":
527537
ui.Mode = ui.ModeQuiet
538+
ep = ui.NewQuiedWriter()
528539
case ui.ModeJSON:
529540
ui.Mode = ui.ModeJSON
530541
logrus.SetFormatter(&logrus.JSONFormatter{})
542+
ep = ui.NewJSONWriter(dockerCli.Err())
531543
default:
532544
return fmt.Errorf("unsupported --progress value %q", opts.Progress)
533545
}
546+
backendOptions.Add(compose.WithEventProcessor(ep))
534547

535548
// (4) options validation / normalization
536549
if opts.WorkDir != "" {

cmd/compose/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/compose-spec/compose-go/v2/dotenv"
2626
"github.com/compose-spec/compose-go/v2/format"
2727
"github.com/docker/compose/v2/pkg/compose"
28+
"github.com/docker/compose/v2/pkg/progress"
2829
xprogress "github.com/moby/buildkit/util/progress/progressui"
2930
"github.com/sirupsen/logrus"
3031

@@ -38,7 +39,6 @@ import (
3839

3940
"github.com/docker/cli/cli"
4041
"github.com/docker/compose/v2/pkg/api"
41-
"github.com/docker/compose/v2/pkg/progress"
4242
"github.com/docker/compose/v2/pkg/utils"
4343
)
4444

cmd/compose/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ import (
2727
"github.com/compose-spec/compose-go/v2/types"
2828
"github.com/docker/cli/cli/command"
2929
"github.com/docker/compose/v2/pkg/compose"
30+
ui "github.com/docker/compose/v2/pkg/progress"
3031
xprogress "github.com/moby/buildkit/util/progress/progressui"
3132
"github.com/sirupsen/logrus"
3233
"github.com/spf13/cobra"
3334
"github.com/spf13/pflag"
3435

3536
"github.com/docker/compose/v2/cmd/formatter"
3637
"github.com/docker/compose/v2/pkg/api"
37-
ui "github.com/docker/compose/v2/pkg/progress"
3838
"github.com/docker/compose/v2/pkg/utils"
3939
)
4040

pkg/compose/build.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
6666
_, err := s.build(ctx, project, options, nil)
6767
return err
6868
})(ctx)
69-
}, s.stdinfo(), "build")
69+
}, "build", s.events)
7070
}
7171

7272
//nolint:gocyclo
@@ -228,7 +228,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
228228
if err != nil {
229229
return err
230230
}
231-
s.events(ctx, progress.BuildingEvent("Image "+buildOptions.Tags[0]))
231+
s.events.On(progress.BuildingEvent("Image " + buildOptions.Tags[0]))
232232

233233
trace.SpanFromContext(ctx).SetAttributes(attribute.String("builder", "buildkit"))
234234
digest, err := s.doBuildBuildkit(ctx, name, buildOptions, w, nodes)
@@ -258,7 +258,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
258258
service := project.Services[names[i]]
259259
imageRef := api.GetImageNameOrDefault(service, project.Name)
260260
imageIDs[imageRef] = imageDigest
261-
s.events(ctx, progress.BuiltEvent("Image "+imageRef))
261+
s.events.On(progress.BuiltEvent("Image " + imageRef))
262262
}
263263
}
264264
return imageIDs, err

pkg/compose/build_bake.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
338338
logrus.Debugf("Executing bake with args: %v", args)
339339

340340
if s.dryRun {
341-
return s.dryRunBake(ctx, cfg), nil
341+
return s.dryRunBake(cfg), nil
342342
}
343343
cmd := exec.CommandContext(ctx, buildx.Path, args...)
344344

@@ -424,7 +424,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
424424
return nil, fmt.Errorf("build result not found in Bake metadata for service %s", name)
425425
}
426426
results[image] = built.Digest
427-
s.events(ctx, progress.BuiltEvent("Image "+image))
427+
s.events.On(progress.BuiltEvent("Image " + image))
428428
}
429429
return results, nil
430430
}
@@ -530,26 +530,26 @@ func dockerFilePath(ctxName string, dockerfile string) string {
530530
return dockerfile
531531
}
532532

533-
func (s composeService) dryRunBake(ctx context.Context, cfg bakeConfig) map[string]string {
533+
func (s composeService) dryRunBake(cfg bakeConfig) map[string]string {
534534
bakeResponse := map[string]string{}
535535
for name, target := range cfg.Targets {
536536
dryRunUUID := fmt.Sprintf("dryRun-%x", sha1.Sum([]byte(name)))
537-
s.displayDryRunBuildEvent(ctx, name, dryRunUUID, target.Tags[0])
537+
s.displayDryRunBuildEvent(name, dryRunUUID, target.Tags[0])
538538
bakeResponse[name] = dryRunUUID
539539
}
540540
for name := range bakeResponse {
541-
s.events(ctx, progress.BuiltEvent(name))
541+
s.events.On(progress.BuiltEvent(name))
542542
}
543543
return bakeResponse
544544
}
545545

546-
func (s composeService) displayDryRunBuildEvent(ctx context.Context, name, dryRunUUID, tag string) {
547-
s.events(ctx, progress.Event{
546+
func (s composeService) displayDryRunBuildEvent(name, dryRunUUID, tag string) {
547+
s.events.On(progress.Event{
548548
ID: name + " ==>",
549549
Status: progress.Done,
550550
Text: fmt.Sprintf("==> writing image %s", dryRunUUID),
551551
})
552-
s.events(ctx, progress.Event{
552+
s.events.On(progress.Event{
553553
ID: name + " ==> ==>",
554554
Status: progress.Done,
555555
Text: fmt.Sprintf(`naming to %s`, tag),

pkg/compose/build_buildkit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, service string, op
3939
err error
4040
)
4141
if s.dryRun {
42-
response = s.dryRunBuildResponse(ctx, service, opts)
42+
response = s.dryRunBuildResponse(service, opts)
4343
} else {
4444
response, err = build.Build(ctx, nodes,
4545
map[string]build.Options{service: opts},
@@ -65,10 +65,10 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, service string, op
6565
return "", fmt.Errorf("buildkit response is missing expected result for %s", service)
6666
}
6767

68-
func (s composeService) dryRunBuildResponse(ctx context.Context, name string, options build.Options) map[string]*client.SolveResponse {
68+
func (s composeService) dryRunBuildResponse(name string, options build.Options) map[string]*client.SolveResponse {
6969
buildResponse := map[string]*client.SolveResponse{}
7070
dryRunUUID := fmt.Sprintf("dryRun-%x", sha1.Sum([]byte(name)))
71-
s.displayDryRunBuildEvent(ctx, name, dryRunUUID, options.Tags[0])
71+
s.displayDryRunBuildEvent(name, dryRunUUID, options.Tags[0])
7272
buildResponse[name] = &client.SolveResponse{ExporterResponse: map[string]string{
7373
"containerimage.digest": dryRunUUID,
7474
}}

pkg/compose/build_classic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, project *types.Proj
185185

186186
ctx, cancel := context.WithCancel(ctx)
187187
defer cancel()
188-
s.events(ctx, progress2.BuildingEvent("Image "+imageName))
188+
s.events.On(progress2.BuildingEvent("Image " + imageName))
189189
response, err := s.apiClient().ImageBuild(ctx, body, buildOpts)
190190
if err != nil {
191191
return "", err
@@ -214,7 +214,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, project *types.Proj
214214
}
215215
return "", err
216216
}
217-
s.events(ctx, progress2.BuiltEvent("Image "+imageName))
217+
s.events.On(progress2.BuiltEvent("Image " + imageName))
218218
return imageID, nil
219219
}
220220

pkg/compose/commit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
func (s *composeService) Commit(ctx context.Context, projectName string, options api.CommitOptions) error {
3030
return progress.Run(ctx, func(ctx context.Context) error {
3131
return s.commit(ctx, projectName, options)
32-
}, s.stdinfo(), "commit")
32+
}, "commit", s.events)
3333
}
3434

3535
func (s *composeService) commit(ctx context.Context, projectName string, options api.CommitOptions) error {
@@ -45,15 +45,15 @@ func (s *composeService) commit(ctx context.Context, projectName string, options
4545
name := getCanonicalContainerName(ctr)
4646
msg := fmt.Sprintf("Commit %s", name)
4747

48-
s.events(ctx, progress.Event{
48+
s.events.On(progress.Event{
4949
ID: name,
5050
Text: msg,
5151
Status: progress.Working,
5252
StatusText: "Committing",
5353
})
5454

5555
if s.dryRun {
56-
s.events(ctx, progress.Event{
56+
s.events.On(progress.Event{
5757
ID: name,
5858
Text: msg,
5959
Status: progress.Done,
@@ -74,7 +74,7 @@ func (s *composeService) commit(ctx context.Context, projectName string, options
7474
return err
7575
}
7676

77-
s.events(ctx, progress.Event{
77+
s.events.On(progress.Event{
7878
ID: name,
7979
Text: msg,
8080
Status: progress.Done,

pkg/compose/compose.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ func NewComposeService(dockerCli command.Cli, options ...Option) (api.Compose, e
6161
clock: clockwork.NewRealClock(),
6262
maxConcurrency: -1,
6363
dryRun: false,
64-
events: func(ctx context.Context, e ...progress.Event) {
65-
// FIXME(ndeloof) temporary during refactoring
66-
progress.ContextWriter(ctx).Events(e)
67-
},
6864
}
6965
for _, option := range options {
7066
if err := option(s); err != nil {
@@ -78,6 +74,9 @@ func NewComposeService(dockerCli command.Cli, options ...Option) (api.Compose, e
7874
return defaultValue, nil
7975
}
8076
}
77+
if s.events == nil {
78+
s.events = progress.NewQuiedWriter()
79+
}
8180
return s, nil
8281
}
8382

@@ -119,14 +118,21 @@ func WithDryRun(s *composeService) error {
119118

120119
type Prompt func(message string, defaultValue bool) (bool, error)
121120

122-
type EventBus func(ctx context.Context, e ...progress.Event)
121+
// WithEventProcessor configure component to get notified on Compose operation and progress events.
122+
// Typically used to configure a progress UI
123+
func WithEventProcessor(bus progress.EventProcessor) Option {
124+
return func(s *composeService) error {
125+
s.events = bus
126+
return nil
127+
}
128+
}
123129

124130
type composeService struct {
125131
dockerCli command.Cli
126132
// prompt is used to interact with user and confirm actions
127133
prompt Prompt
128134
// eventBus collects tasks execution events
129-
events EventBus
135+
events progress.EventProcessor
130136

131137
clock clockwork.Clock
132138
maxConcurrency int

0 commit comments

Comments
 (0)