use --progress to configure progress UI stylet push

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-06-12 09:27:32 +02:00 committed by Nicolas De loof
parent 9384e5f4d7
commit cfe91becc7
8 changed files with 106 additions and 34 deletions

View file

@ -27,8 +27,7 @@ import (
"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"
ui "github.com/docker/compose/v2/pkg/progress"
"github.com/spf13/cobra"
"github.com/docker/compose/v2/pkg/api"
@ -37,14 +36,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) {
@ -60,7 +58,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
return api.BuildOptions{
Pull: opts.pull,
Push: opts.push,
Progress: opts.progress,
Progress: ui.Mode,
Args: types.NewMappingWithEquals(opts.args),
NoCache: opts.noCache,
Quiet: opts.quiet,
@ -69,14 +67,7 @@ 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 {
func buildCommand(p *ProjectOptions, progress *string, backend api.Service) *cobra.Command {
opts := buildOptions{
ProjectOptions: p,
}
@ -85,24 +76,21 @@ 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
ui.Mode = ui.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
if cmd.Flags().Changed("progress") && opts.ssh == "" {
fmt.Fprint(os.Stderr, "--progress is a global compose flag, better use `docker compose --progress xx build ...")
}
return runBuild(ctx, backend, opts, args)
}),
@ -111,7 +99,6 @@ func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
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")
@ -124,6 +111,8 @@ func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
cmd.Flags().Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
cmd.Flags().MarkHidden("no-rm") //nolint:errcheck
cmd.Flags().VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.")
cmd.Flags().StringVar(progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of ui output (%s)`, strings.Join(printerModes, ", ")))
cmd.Flags().MarkHidden("progress") //nolint:errcheck
return cmd
}

View file

@ -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"
@ -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"
)
@ -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",
@ -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`)
@ -404,7 +426,7 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
portCommand(&opts, streams, backend),
imagesCommand(&opts, streams, backend),
versionCommand(streams),
buildCommand(&opts, backend),
buildCommand(&opts, &progress, backend),
pushCommand(&opts, backend),
pullCommand(&opts, backend),
createCommand(&opts, backend),
@ -425,6 +447,8 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
},
)
c.Flags().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")
@ -460,3 +484,10 @@ func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
}
return nil
}
var printerModes = []string{
ui.ModeAuto,
ui.ModeTTY,
ui.ModePlain,
ui.ModeQuiet,
}