Skip to content

Commit

Permalink
use --progress to configure progress UI style
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Jun 12, 2023
1 parent 508d71c commit 1f20407
Show file tree
Hide file tree
Showing 61 changed files with 481 additions and 119 deletions.
37 changes: 9 additions & 28 deletions cmd/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ package compose

import (
"context"
"fmt"
"os"
"strings"

"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
buildx "github.com/docker/buildx/util/progress"
cliopts "github.com/docker/cli/opts"
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/compose/v2/pkg/utils"
"github.com/spf13/cobra"

"github.com/docker/compose/v2/pkg/api"
Expand All @@ -37,14 +33,13 @@ import (
type buildOptions struct {
*ProjectOptions
composeOptions
quiet bool
pull bool
push bool
progress string
args []string
noCache bool
memory cliopts.MemBytes
ssh string
quiet bool
pull bool
push bool
args []string
noCache bool
memory cliopts.MemBytes
ssh string
}

func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) {
Expand All @@ -60,7 +55,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
return api.BuildOptions{
Pull: opts.pull,
Push: opts.push,
Progress: opts.progress,
Progress: progress.Mode,
Args: types.NewMappingWithEquals(opts.args),
NoCache: opts.noCache,
Quiet: opts.quiet,
Expand All @@ -69,13 +64,6 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
}, nil
}

var printerModes = []string{
buildx.PrinterModeAuto,
buildx.PrinterModeTty,
buildx.PrinterModePlain,
buildx.PrinterModeQuiet,
}

func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
opts := buildOptions{
ProjectOptions: p,
Expand All @@ -85,33 +73,26 @@ func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
Short: "Build or rebuild services",
PreRunE: Adapt(func(ctx context.Context, args []string) error {
if opts.quiet {
opts.progress = buildx.PrinterModeQuiet
progress.Mode = progress.ModeQuiet
devnull, err := os.Open(os.DevNull)
if err != nil {
return err
}
os.Stdout = devnull
}
if !utils.StringContains(printerModes, opts.progress) {
return fmt.Errorf("unsupported --progress value %q", opts.progress)
}
return nil
}),
RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("ssh") && opts.ssh == "" {
opts.ssh = "default"
}
if progress.Mode == progress.ModePlain && !cmd.Flags().Changed("progress") {
opts.progress = buildx.PrinterModePlain
}
return runBuild(ctx, backend, opts, args)
}),
ValidArgsFunction: completeServiceNames(p),
}
cmd.Flags().BoolVar(&opts.push, "push", false, "Push service images.")
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
cmd.Flags().StringVar(&opts.progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of progress output (%s)`, strings.Join(printerModes, ", ")))
cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
cmd.Flags().StringVar(&opts.ssh, "ssh", "", "Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent)")
cmd.Flags().Bool("parallel", true, "Build images in parallel. DEPRECATED")
Expand Down
39 changes: 35 additions & 4 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"syscall"

buildx "github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"

"github.com/compose-spec/compose-go/cli"
Expand All @@ -43,7 +44,7 @@ import (
"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
"github.com/docker/compose/v2/pkg/progress"
ui "github.com/docker/compose/v2/pkg/progress"
"github.com/docker/compose/v2/pkg/utils"
)

Expand Down Expand Up @@ -273,6 +274,7 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
version bool
parallel int
dryRun bool
progress string
)
c := &cobra.Command{
Short: "Docker Compose",
Expand Down Expand Up @@ -326,16 +328,36 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
formatter.SetANSIMode(streams, ansi)

if noColor, ok := os.LookupEnv("NO_COLOR"); ok && noColor != "" {
progress.NoColor()
ui.NoColor()
formatter.SetANSIMode(streams, formatter.Never)
}

switch ansi {
case "never":
progress.Mode = progress.ModePlain
ui.Mode = ui.ModePlain
case "always":
progress.Mode = progress.ModeTTY
ui.Mode = ui.ModeTTY
}

switch progress {
case ui.ModeAuto:
ui.Mode = ui.ModeAuto
case ui.ModeTTY:
if ansi == "never" {
return fmt.Errorf("can't use --progress tty while ANSI support is disabled")
}
ui.Mode = ui.ModeTTY
case ui.ModePlain:
if ansi == "always" {
return fmt.Errorf("can't use --progress plain while ANSI support is forced")
}
ui.Mode = ui.ModePlain
case ui.ModeQuiet, "none":
ui.Mode = ui.ModeQuiet
default:
return fmt.Errorf("unsupported --progress value %q", progress)
}

if opts.WorkDir != "" {
if opts.ProjectDir != "" {
return errors.New(`cannot specify DEPRECATED "--workdir" and "--project-directory". Please use only "--project-directory" instead`)
Expand Down Expand Up @@ -425,6 +447,8 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
},
)

c.PersistentFlags().StringVar(&progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of progress output (%s)`, strings.Join(printerModes, ", ")))

c.Flags().StringVar(&ansi, "ansi", "auto", `Control when to print ANSI control characters ("never"|"always"|"auto")`)
c.Flags().IntVar(&parallel, "parallel", -1, `Control max parallelism, -1 for unlimited`)
c.Flags().BoolVarP(&version, "version", "v", false, "Show the Docker Compose version information")
Expand Down Expand Up @@ -460,3 +484,10 @@ func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
}
return nil
}

var printerModes = []string{
ui.ModeAuto,
ui.ModeTTY,
ui.ModePlain,
ui.ModeQuiet,
}
1 change: 1 addition & 0 deletions docs/reference/compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Define and run multi-container applications with Docker.
| `-f`, `--file` | `stringArray` | | Compose configuration files |
| `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited |
| `--profile` | `stringArray` | | Specify a profile to enable |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `--project-directory` | `string` | | Specify an alternate working directory<br>(default: the path of the, first specified, Compose file) |
| `-p`, `--project-name` | `string` | | Project name |

Expand Down
7 changes: 4 additions & 3 deletions docs/reference/compose_alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ Experimental commands

### Options

| Name | Type | Default | Description |
|:------------|:-----|:--------|:--------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| Name | Type | Default | Description |
|:-------------|:---------|:--------|:------------------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |


<!---MARKER_GEN_END-->
Expand Down
17 changes: 9 additions & 8 deletions docs/reference/compose_alpha_viz.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ EXPERIMENTAL - Generate a graphviz graph from your compose file

### Options

| Name | Type | Default | Description |
|:---------------------|:------|:--------|:---------------------------------------------------------------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--image` | | | Include service's image name in output graph |
| `--indentation-size` | `int` | `1` | Number of tabs or spaces to use for indentation |
| `--networks` | | | Include service's attached networks in output graph |
| `--ports` | | | Include service's exposed ports in output graph |
| `--spaces` | | | If given, space character ' ' will be used to indent,<br>otherwise tab character '\t' will be used |
| Name | Type | Default | Description |
|:---------------------|:---------|:--------|:---------------------------------------------------------------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--image` | | | Include service's image name in output graph |
| `--indentation-size` | `int` | `1` | Number of tabs or spaces to use for indentation |
| `--networks` | | | Include service's attached networks in output graph |
| `--ports` | | | Include service's exposed ports in output graph |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `--spaces` | | | If given, space character ' ' will be used to indent,<br>otherwise tab character '\t' will be used |


<!---MARKER_GEN_END-->
Expand Down
9 changes: 5 additions & 4 deletions docs/reference/compose_alpha_watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ EXPERIMENTAL - Watch build context for service and rebuild/refresh containers wh

### Options

| Name | Type | Default | Description |
|:------------|:-----|:--------|:--------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--quiet` | | | hide build output |
| Name | Type | Default | Description |
|:-------------|:---------|:--------|:------------------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `--quiet` | | | hide build output |


<!---MARKER_GEN_END-->
Expand Down
1 change: 1 addition & 0 deletions docs/reference/compose_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Parse, resolve and render compose file in canonical format
| `--no-path-resolution` | | | Don't resolve file paths. |
| `-o`, `--output` | `string` | | Save to file (default to stdout) |
| `--profiles` | | | Print the profile names, one per line. |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `-q`, `--quiet` | | | Only validate the configuration, don't print anything. |
| `--resolve-image-digests` | | | Pin image tags to digests. |
| `--services` | | | Print the service names, one per line. |
Expand Down
13 changes: 7 additions & 6 deletions docs/reference/compose_cp.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ Copy files/folders between a service container and the local filesystem

### Options

| Name | Type | Default | Description |
|:----------------------|:------|:--------|:----------------------------------------------------------------------|
| `-a`, `--archive` | | | Archive mode (copy all uid/gid information) |
| `--dry-run` | | | Execute command in dry run mode |
| `-L`, `--follow-link` | | | Always follow symbol link in SRC_PATH |
| `--index` | `int` | `0` | Index of the container if there are multiple instances of a service . |
| Name | Type | Default | Description |
|:----------------------|:---------|:--------|:----------------------------------------------------------------------|
| `-a`, `--archive` | | | Archive mode (copy all uid/gid information) |
| `--dry-run` | | | Execute command in dry run mode |
| `-L`, `--follow-link` | | | Always follow symbol link in SRC_PATH |
| `--index` | `int` | `0` | Index of the container if there are multiple instances of a service . |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |


<!---MARKER_GEN_END-->
Expand Down
1 change: 1 addition & 0 deletions docs/reference/compose_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Creates containers for a service.
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
| `--no-build` | | | Don't build an image, even if it's missing. |
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `--pull` | `string` | `missing` | Pull image before running ("always"\|"missing"\|"never") |
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
Expand Down
1 change: 1 addition & 0 deletions docs/reference/compose_down.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Stop and remove containers, networks
| Name | Type | Default | Description |
|:-------------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
| `--rmi` | `string` | | Remove images used by services. "local" remove only images that don't have a custom tag ("local"\|"all") |
| `-t`, `--timeout` | `int` | `10` | Specify a shutdown timeout in seconds |
Expand Down
9 changes: 5 additions & 4 deletions docs/reference/compose_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ Receive real time events from containers.

### Options

| Name | Type | Default | Description |
|:------------|:-----|:--------|:------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--json` | | | Output events as a stream of json objects |
| Name | Type | Default | Description |
|:-------------|:---------|:--------|:------------------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--json` | | | Output events as a stream of json objects |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |


<!---MARKER_GEN_END-->
Expand Down
1 change: 1 addition & 0 deletions docs/reference/compose_exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Execute a command in a running container.
| `--index` | `int` | `1` | index of the container if there are multiple instances of a service [default: 1]. |
| `-T`, `--no-TTY` | | | Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY. |
| `--privileged` | | | Give extended privileges to the process. |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `-u`, `--user` | `string` | | Run the command as this user. |
| `-w`, `--workdir` | `string` | | Path to workdir directory for this command. |

Expand Down
11 changes: 6 additions & 5 deletions docs/reference/compose_images.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ List images used by the created containers

### Options

| Name | Type | Default | Description |
|:----------------|:---------|:--------|:--------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--format` | `string` | `table` | Format the output. Values: [table \| json]. |
| `-q`, `--quiet` | | | Only display IDs |
| Name | Type | Default | Description |
|:----------------|:---------|:--------|:------------------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--format` | `string` | `table` | Format the output. Values: [table \| json]. |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `-q`, `--quiet` | | | Only display IDs |


<!---MARKER_GEN_END-->
Expand Down
1 change: 1 addition & 0 deletions docs/reference/compose_kill.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Force stop service containers.
| Name | Type | Default | Description |
|:-------------------|:---------|:----------|:----------------------------------------------------------------|
| `--dry-run` | | | Execute command in dry run mode |
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
| `-s`, `--signal` | `string` | `SIGKILL` | SIGNAL to send to the container. |

Expand Down
Loading

0 comments on commit 1f20407

Please sign in to comment.