introduce build --progress

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-03-02 09:14:09 +01:00
parent 6f73007265
commit 6412d88703
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
3 changed files with 15 additions and 9 deletions

View File

@ -69,6 +69,8 @@ type Service interface {
type BuildOptions struct {
// Pull always attempt to pull a newer version of the image
Pull bool
// Progress set type of progress output ("auto", "plain", "tty")
Progress string
}
// CreateOptions group options of the Create API

View File

@ -18,20 +18,21 @@ package compose
import (
"context"
"github.com/docker/compose-cli/api/compose"
"os"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/progress"
)
type buildOptions struct {
*projectOptions
composeOptions
quiet bool
pull bool
quiet bool
pull bool
progress string
}
func buildCommand(p *projectOptions) *cobra.Command {
@ -54,6 +55,7 @@ func buildCommand(p *projectOptions) *cobra.Command {
}
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", "auto", `Set type of progress output ("auto", "plain", "tty")`)
return cmd
}
@ -70,7 +72,8 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error {
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{
Pull: opts.pull,
Pull: opts.pull,
Progress: opts.progress,
})
})
return err

View File

@ -19,11 +19,12 @@ package compose
import (
"context"
"fmt"
"github.com/docker/compose-cli/api/compose"
"os"
"path"
"strings"
"github.com/docker/compose-cli/api/compose"
"github.com/compose-spec/compose-go/types"
"github.com/docker/buildx/build"
"github.com/docker/buildx/driver"
@ -46,7 +47,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
}
}
err := s.build(ctx, project, opts)
err := s.build(ctx, project, opts, options.Progress)
if err == nil {
displayScanSuggestMsg(ctx, imagesToBuild)
}
@ -96,7 +97,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
}
err := s.build(ctx, project, opts)
err := s.build(ctx, project, opts, "auto")
if err == nil {
displayScanSuggestMsg(ctx, imagesToBuild)
}
@ -114,7 +115,7 @@ func (s *composeService) localImagePresent(ctx context.Context, imageName string
return true, nil
}
func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options) error {
func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) error {
if len(opts) == 0 {
return nil
}
@ -135,7 +136,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
// build and will lock
progressCtx, cancel := context.WithCancel(context.Background())
defer cancel()
w := progress.NewPrinter(progressCtx, os.Stdout, "auto")
w := progress.NewPrinter(progressCtx, os.Stdout, mode)
// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
_, err = build.Build(ctx, driverInfo, opts, nil, nil, w)