From 18095ee87b563ab89c5cf05f31088f72b3a2967c Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 30 Apr 2020 13:01:45 -0700 Subject: [PATCH] bake: reset no-cache and pull if not set Signed-off-by: Tonis Tiigi --- commands/bake.go | 15 ++++++++++----- commands/build.go | 39 +++++---------------------------------- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/commands/bake.go b/commands/bake.go index c64eefd6..7a876722 100644 --- a/commands/bake.go +++ b/commands/bake.go @@ -106,15 +106,20 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command { Use: "bake [OPTIONS] [TARGET...]", Aliases: []string{"f"}, Short: "Build from a file", + RunE: func(cmd *cobra.Command, args []string) error { + // reset to nil to avoid override is unset + if !cmd.Flags().Lookup("no-cache").Changed { + options.noCache = nil + } + if !cmd.Flags().Lookup("pull").Changed { + options.pull = nil + } + return runBake(dockerCli, args, options) + }, } flags := cmd.Flags() - cmd.RunE = func(cmd *cobra.Command, args []string) error { - handleUnsetFlags(flags, &options) - return runBake(dockerCli, args, options) - } - flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Build definition file") flags.BoolVar(&options.printOnly, "print", false, "Print the options without building") flags.StringArrayVar(&options.overrides, "set", nil, "Override target value (eg: targetpattern.key=value)") diff --git a/commands/build.go b/commands/build.go index 5cdf251c..24b4702f 100644 --- a/commands/build.go +++ b/commands/build.go @@ -70,18 +70,6 @@ type commonOptions struct { exportLoad bool } -func (o *commonOptions) Unset(s string) error { - switch s { - case "pull": - o.noCache = nil - case "no-cache": - o.pull = nil - default: - return errors.Errorf("cannot unset flag %q", s) - } - return nil -} - func runBuild(dockerCli command.Cli, in buildOptions) error { if in.squash { return errors.Errorf("squash currently not implemented") @@ -220,21 +208,6 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu return err } -type unsetter interface { - Unset(flagName string) error -} - -func handleUnsetFlags(flags *pflag.FlagSet, options unsetter) error { - for _, name := range []string{"pull", "no-cache"} { - if !flags.Lookup(name).Changed { - if err := options.Unset(name); err != nil { - return err - } - } - } - return nil -} - func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command { var options buildOptions @@ -243,17 +216,15 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command { Aliases: []string{"b"}, Short: "Start a build", Args: cli.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + options.contextPath = args[0] + options.builder = rootOpts.builder + return runBuild(dockerCli, options) + }, } flags := cmd.Flags() - cmd.RunE = func(cmd *cobra.Command, args []string) error { - options.contextPath = args[0] - options.builder = rootOpts.builder - handleUnsetFlags(flags, &options) - return runBuild(dockerCli, options) - } - flags.BoolVar(&options.exportPush, "push", false, "Shorthand for --output=type=registry") flags.BoolVar(&options.exportLoad, "load", false, "Shorthand for --output=type=docker")