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 { type BuildOptions struct {
// Pull always attempt to pull a newer version of the image // Pull always attempt to pull a newer version of the image
Pull bool Pull bool
// Progress set type of progress output ("auto", "plain", "tty")
Progress string
} }
// CreateOptions group options of the Create API // CreateOptions group options of the Create API

View File

@ -18,12 +18,12 @@ package compose
import ( import (
"context" "context"
"github.com/docker/compose-cli/api/compose"
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/api/progress"
) )
@ -32,6 +32,7 @@ type buildOptions struct {
composeOptions composeOptions
quiet bool quiet bool
pull bool pull bool
progress string
} }
func buildCommand(p *projectOptions) *cobra.Command { 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().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().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 return cmd
} }
@ -71,6 +73,7 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error {
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{ return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{
Pull: opts.pull, Pull: opts.pull,
Progress: opts.progress,
}) })
}) })
return err return err

View File

@ -19,11 +19,12 @@ package compose
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/docker/compose-cli/api/compose"
"os" "os"
"path" "path"
"strings" "strings"
"github.com/docker/compose-cli/api/compose"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/docker/buildx/build" "github.com/docker/buildx/build"
"github.com/docker/buildx/driver" "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 { if err == nil {
displayScanSuggestMsg(ctx, imagesToBuild) 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 { if err == nil {
displayScanSuggestMsg(ctx, imagesToBuild) displayScanSuggestMsg(ctx, imagesToBuild)
} }
@ -114,7 +115,7 @@ func (s *composeService) localImagePresent(ctx context.Context, imageName string
return true, nil 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 { if len(opts) == 0 {
return nil return nil
} }
@ -135,7 +136,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
// build and will lock // build and will lock
progressCtx, cancel := context.WithCancel(context.Background()) progressCtx, cancel := context.WithCancel(context.Background())
defer cancel() 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 // 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) _, err = build.Build(ctx, driverInfo, opts, nil, nil, w)