Skip to content

Commit 43665d4

Browse files
committed
use --progress to configure progress UI stylet push
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 9384e5f commit 43665d4

8 files changed

Lines changed: 106 additions & 34 deletions

File tree

cmd/compose/build.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ import (
2727
"github.com/compose-spec/compose-go/types"
2828
buildx "github.com/docker/buildx/util/progress"
2929
cliopts "github.com/docker/cli/opts"
30-
"github.com/docker/compose/v2/pkg/progress"
31-
"github.com/docker/compose/v2/pkg/utils"
30+
ui "github.com/docker/compose/v2/pkg/progress"
3231
"github.com/spf13/cobra"
3332

3433
"github.com/docker/compose/v2/pkg/api"
@@ -37,14 +36,13 @@ import (
3736
type buildOptions struct {
3837
*ProjectOptions
3938
composeOptions
40-
quiet bool
41-
pull bool
42-
push bool
43-
progress string
44-
args []string
45-
noCache bool
46-
memory cliopts.MemBytes
47-
ssh string
39+
quiet bool
40+
pull bool
41+
push bool
42+
args []string
43+
noCache bool
44+
memory cliopts.MemBytes
45+
ssh string
4846
}
4947

5048
func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) {
@@ -60,7 +58,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
6058
return api.BuildOptions{
6159
Pull: opts.pull,
6260
Push: opts.push,
63-
Progress: opts.progress,
61+
Progress: ui.Mode,
6462
Args: types.NewMappingWithEquals(opts.args),
6563
NoCache: opts.noCache,
6664
Quiet: opts.quiet,
@@ -69,14 +67,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
6967
}, nil
7068
}
7169

72-
var printerModes = []string{
73-
buildx.PrinterModeAuto,
74-
buildx.PrinterModeTty,
75-
buildx.PrinterModePlain,
76-
buildx.PrinterModeQuiet,
77-
}
78-
79-
func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
70+
func buildCommand(p *ProjectOptions, progress *string, backend api.Service) *cobra.Command {
8071
opts := buildOptions{
8172
ProjectOptions: p,
8273
}
@@ -85,24 +76,21 @@ func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
8576
Short: "Build or rebuild services",
8677
PreRunE: Adapt(func(ctx context.Context, args []string) error {
8778
if opts.quiet {
88-
opts.progress = buildx.PrinterModeQuiet
79+
ui.Mode = ui.ModeQuiet
8980
devnull, err := os.Open(os.DevNull)
9081
if err != nil {
9182
return err
9283
}
9384
os.Stdout = devnull
9485
}
95-
if !utils.StringContains(printerModes, opts.progress) {
96-
return fmt.Errorf("unsupported --progress value %q", opts.progress)
97-
}
9886
return nil
9987
}),
10088
RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
10189
if cmd.Flags().Changed("ssh") && opts.ssh == "" {
10290
opts.ssh = "default"
10391
}
104-
if progress.Mode == progress.ModePlain && !cmd.Flags().Changed("progress") {
105-
opts.progress = buildx.PrinterModePlain
92+
if cmd.Flags().Changed("progress") && opts.ssh == "" {
93+
fmt.Fprint(os.Stderr, "--progress is a global compose flag, better use `docker compose --progress xx build ...")
10694
}
10795
return runBuild(ctx, backend, opts, args)
10896
}),
@@ -111,7 +99,6 @@ func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
11199
cmd.Flags().BoolVar(&opts.push, "push", false, "Push service images.")
112100
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
113101
cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
114-
cmd.Flags().StringVar(&opts.progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of progress output (%s)`, strings.Join(printerModes, ", ")))
115102
cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
116103
cmd.Flags().StringVar(&opts.ssh, "ssh", "", "Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent)")
117104
cmd.Flags().Bool("parallel", true, "Build images in parallel. DEPRECATED")
@@ -124,6 +111,8 @@ func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
124111
cmd.Flags().Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
125112
cmd.Flags().MarkHidden("no-rm") //nolint:errcheck
126113
cmd.Flags().VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.")
114+
cmd.Flags().StringVar(progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of ui output (%s)`, strings.Join(printerModes, ", ")))
115+
cmd.Flags().MarkHidden("progress") //nolint:errcheck
127116

128117
return cmd
129118
}

cmd/compose/compose.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727
"syscall"
2828

29+
buildx "github.com/docker/buildx/util/progress"
2930
"github.com/docker/cli/cli/command"
3031

3132
"github.com/compose-spec/compose-go/cli"
@@ -43,7 +44,7 @@ import (
4344
"github.com/docker/compose/v2/cmd/formatter"
4445
"github.com/docker/compose/v2/pkg/api"
4546
"github.com/docker/compose/v2/pkg/compose"
46-
"github.com/docker/compose/v2/pkg/progress"
47+
ui "github.com/docker/compose/v2/pkg/progress"
4748
"github.com/docker/compose/v2/pkg/utils"
4849
)
4950

@@ -273,6 +274,7 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
273274
version bool
274275
parallel int
275276
dryRun bool
277+
progress string
276278
)
277279
c := &cobra.Command{
278280
Short: "Docker Compose",
@@ -326,16 +328,36 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
326328
formatter.SetANSIMode(streams, ansi)
327329

328330
if noColor, ok := os.LookupEnv("NO_COLOR"); ok && noColor != "" {
329-
progress.NoColor()
331+
ui.NoColor()
330332
formatter.SetANSIMode(streams, formatter.Never)
331333
}
332334

333335
switch ansi {
334336
case "never":
335-
progress.Mode = progress.ModePlain
337+
ui.Mode = ui.ModePlain
336338
case "always":
337-
progress.Mode = progress.ModeTTY
339+
ui.Mode = ui.ModeTTY
338340
}
341+
342+
switch progress {
343+
case ui.ModeAuto:
344+
ui.Mode = ui.ModeAuto
345+
case ui.ModeTTY:
346+
if ansi == "never" {
347+
return fmt.Errorf("can't use --progress tty while ANSI support is disabled")
348+
}
349+
ui.Mode = ui.ModeTTY
350+
case ui.ModePlain:
351+
if ansi == "always" {
352+
return fmt.Errorf("can't use --progress plain while ANSI support is forced")
353+
}
354+
ui.Mode = ui.ModePlain
355+
case ui.ModeQuiet, "none":
356+
ui.Mode = ui.ModeQuiet
357+
default:
358+
return fmt.Errorf("unsupported --progress value %q", progress)
359+
}
360+
339361
if opts.WorkDir != "" {
340362
if opts.ProjectDir != "" {
341363
return errors.New(`cannot specify DEPRECATED "--workdir" and "--project-directory". Please use only "--project-directory" instead`)
@@ -404,7 +426,7 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
404426
portCommand(&opts, streams, backend),
405427
imagesCommand(&opts, streams, backend),
406428
versionCommand(streams),
407-
buildCommand(&opts, backend),
429+
buildCommand(&opts, &progress, backend),
408430
pushCommand(&opts, backend),
409431
pullCommand(&opts, backend),
410432
createCommand(&opts, backend),
@@ -425,6 +447,8 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
425447
},
426448
)
427449

450+
c.Flags().StringVar(&progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of progress output (%s)`, strings.Join(printerModes, ", ")))
451+
428452
c.Flags().StringVar(&ansi, "ansi", "auto", `Control when to print ANSI control characters ("never"|"always"|"auto")`)
429453
c.Flags().IntVar(&parallel, "parallel", -1, `Control max parallelism, -1 for unlimited`)
430454
c.Flags().BoolVarP(&version, "version", "v", false, "Show the Docker Compose version information")
@@ -460,3 +484,10 @@ func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
460484
}
461485
return nil
462486
}
487+
488+
var printerModes = []string{
489+
ui.ModeAuto,
490+
ui.ModeTTY,
491+
ui.ModePlain,
492+
ui.ModeQuiet,
493+
}

docs/reference/compose.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Define and run multi-container applications with Docker.
4646
| `-f`, `--file` | `stringArray` | | Compose configuration files |
4747
| `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited |
4848
| `--profile` | `stringArray` | | Specify a profile to enable |
49+
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
4950
| `--project-directory` | `string` | | Specify an alternate working directory<br>(default: the path of the, first specified, Compose file) |
5051
| `-p`, `--project-name` | `string` | | Project name |
5152

docs/reference/compose_build.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Build or rebuild services
1111
| `--dry-run` | | | Execute command in dry run mode |
1212
| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. |
1313
| `--no-cache` | | | Do not use cache when building the image |
14-
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
1514
| `--pull` | | | Always attempt to pull a newer version of the image. |
1615
| `--push` | | | Push service images. |
1716
| `-q`, `--quiet` | | | Don't print anything to STDOUT |

docs/reference/docker_compose.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ options:
280280
experimentalcli: false
281281
kubernetes: false
282282
swarm: false
283+
- option: progress
284+
value_type: string
285+
default_value: auto
286+
description: Set type of progress output (auto, tty, plain, quiet)
287+
deprecated: false
288+
hidden: false
289+
experimental: false
290+
experimentalcli: false
291+
kubernetes: false
292+
swarm: false
283293
- option: project-directory
284294
value_type: string
285295
description: |-

docs/reference/docker_compose_build.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ options:
9090
- option: progress
9191
value_type: string
9292
default_value: auto
93-
description: Set type of progress output (auto, tty, plain, quiet)
93+
description: Set type of ui output (auto, tty, plain, quiet)
9494
deprecated: false
95-
hidden: false
95+
hidden: true
9696
experimental: false
9797
experimentalcli: false
9898
kubernetes: false

pkg/progress/quiet.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package progress
18+
19+
import "context"
20+
21+
type quiet struct{}
22+
23+
func (q quiet) Start(_ context.Context) error {
24+
return nil
25+
}
26+
27+
func (q quiet) Stop() {
28+
}
29+
30+
func (q quiet) Event(_ Event) {
31+
}
32+
33+
func (q quiet) Events(_ []Event) {
34+
}
35+
36+
func (q quiet) TailMsgf(_ string, _ ...interface{}) {
37+
}

pkg/progress/writer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ const (
107107
ModeTTY = "tty"
108108
// ModePlain dump raw events to output
109109
ModePlain = "plain"
110+
// ModeQuiet don't display events
111+
ModeQuiet = "quiet"
110112
)
111113

112114
// Mode define how progress should be rendered, either as ModePlain or ModeTTY
@@ -119,6 +121,9 @@ func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer
119121
if !ok {
120122
dryRun = false
121123
}
124+
if Mode == ModeQuiet {
125+
return quiet{}, nil
126+
}
122127
f, isConsole := out.(console.File) // see https://github.com/docker/compose/issues/10560
123128
if Mode == ModeAuto && isTerminal && isConsole {
124129
return newTTYWriter(f, dryRun, progressTitle)

0 commit comments

Comments
 (0)