2019-03-24 12:30:29 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-10-16 22:15:26 +08:00
|
|
|
"fmt"
|
2020-04-28 05:37:17 +08:00
|
|
|
"os"
|
|
|
|
|
2023-08-15 15:37:42 +08:00
|
|
|
debugcmd "github.com/docker/buildx/commands/debug"
|
2019-04-25 10:29:56 +08:00
|
|
|
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
|
2023-02-01 00:39:57 +08:00
|
|
|
"github.com/docker/buildx/controller/remote"
|
2023-04-11 17:45:59 +08:00
|
|
|
"github.com/docker/buildx/util/cobrautil/completion"
|
2024-03-15 18:52:11 +08:00
|
|
|
"github.com/docker/buildx/util/confutil"
|
2021-12-20 14:31:31 +08:00
|
|
|
"github.com/docker/buildx/util/logutil"
|
2021-11-22 17:51:54 +08:00
|
|
|
"github.com/docker/cli-docs-tool/annotation"
|
2022-04-27 23:33:12 +08:00
|
|
|
"github.com/docker/cli/cli"
|
2019-03-26 10:01:31 +08:00
|
|
|
"github.com/docker/cli/cli-plugins/plugin"
|
2019-03-24 12:30:29 +08:00
|
|
|
"github.com/docker/cli/cli/command"
|
2023-05-22 16:52:23 +08:00
|
|
|
"github.com/docker/cli/cli/debug"
|
2024-01-20 08:44:30 +08:00
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
2021-12-20 14:31:31 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-03-24 12:30:29 +08:00
|
|
|
"github.com/spf13/cobra"
|
2020-04-28 05:37:17 +08:00
|
|
|
"github.com/spf13/pflag"
|
2019-03-24 12:30:29 +08:00
|
|
|
)
|
|
|
|
|
2019-04-12 00:09:03 +08:00
|
|
|
func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command {
|
2024-08-16 18:20:28 +08:00
|
|
|
var opt rootOptions
|
2019-03-24 12:30:29 +08:00
|
|
|
cmd := &cobra.Command{
|
2021-10-21 04:16:10 +08:00
|
|
|
Short: "Docker Buildx",
|
|
|
|
Long: `Extended build capabilities with BuildKit`,
|
2019-04-12 00:09:03 +08:00
|
|
|
Use: name,
|
2021-11-22 17:51:54 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
annotation.CodeDelimiter: `"`,
|
|
|
|
},
|
2023-04-10 03:20:18 +08:00
|
|
|
CompletionOptions: cobra.CompletionOptions{
|
|
|
|
HiddenDefaultCmd: true,
|
|
|
|
},
|
2024-01-20 08:44:30 +08:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2024-08-16 18:20:28 +08:00
|
|
|
if opt.debug {
|
|
|
|
debug.Enable()
|
|
|
|
}
|
2024-01-20 08:44:30 +08:00
|
|
|
cmd.SetContext(appcontext.Context())
|
|
|
|
if !isPlugin {
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-26 10:01:31 +08:00
|
|
|
return plugin.PersistentPreRunE(cmd, args)
|
2024-01-20 08:44:30 +08:00
|
|
|
},
|
2024-10-16 22:15:26 +08:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return cmd.Help()
|
|
|
|
}
|
|
|
|
_ = cmd.Help()
|
|
|
|
return cli.StatusError{
|
|
|
|
StatusCode: 1,
|
|
|
|
Status: fmt.Sprintf("ERROR: unknown command: %q", args[0]),
|
|
|
|
}
|
|
|
|
},
|
2024-01-20 08:44:30 +08:00
|
|
|
}
|
|
|
|
if !isPlugin {
|
2022-04-27 23:33:12 +08:00
|
|
|
// match plugin behavior for standalone mode
|
|
|
|
// https://github.com/docker/cli/blob/6c9eb708fa6d17765d71965f90e1c59cea686ee9/cli-plugins/plugin/plugin.go#L117-L127
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
cmd.SilenceErrors = true
|
|
|
|
cmd.TraverseChildren = true
|
|
|
|
cmd.DisableFlagsInUseLine = true
|
|
|
|
cli.DisableFlagsInUseLine(cmd)
|
2019-03-24 12:30:29 +08:00
|
|
|
}
|
2019-04-12 00:09:03 +08:00
|
|
|
|
2022-01-25 15:54:05 +08:00
|
|
|
logrus.SetFormatter(&logutil.Formatter{})
|
|
|
|
|
2022-01-21 07:36:06 +08:00
|
|
|
logrus.AddHook(logutil.NewFilter([]logrus.Level{
|
|
|
|
logrus.DebugLevel,
|
|
|
|
},
|
2021-12-20 14:31:31 +08:00
|
|
|
"serving grpc connection",
|
|
|
|
"stopping session",
|
|
|
|
"using default config store",
|
|
|
|
))
|
|
|
|
|
2024-03-15 18:52:11 +08:00
|
|
|
if !confutil.IsExperimental() {
|
2024-01-12 10:49:16 +08:00
|
|
|
cmd.SetHelpTemplate(cmd.HelpTemplate() + "\nExperimental commands and flags are hidden. Set BUILDX_EXPERIMENTAL=1 to show them.\n")
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:20:28 +08:00
|
|
|
addCommands(cmd, &opt, dockerCli)
|
2019-03-24 12:30:29 +08:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
type rootOptions struct {
|
|
|
|
builder string
|
2024-08-16 18:20:28 +08:00
|
|
|
debug bool
|
2020-04-28 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
2024-08-16 18:20:28 +08:00
|
|
|
func addCommands(cmd *cobra.Command, opts *rootOptions, dockerCli command.Cli) {
|
2020-04-28 05:37:17 +08:00
|
|
|
rootFlags(opts, cmd.PersistentFlags())
|
|
|
|
|
2019-03-24 12:30:29 +08:00
|
|
|
cmd.AddCommand(
|
2023-08-15 15:37:42 +08:00
|
|
|
buildCmd(dockerCli, opts, nil),
|
2020-04-28 05:37:17 +08:00
|
|
|
bakeCmd(dockerCli, opts),
|
2019-04-12 08:24:24 +08:00
|
|
|
createCmd(dockerCli),
|
2023-11-14 07:57:12 +08:00
|
|
|
dialStdioCmd(dockerCli, opts),
|
2020-04-28 05:37:17 +08:00
|
|
|
rmCmd(dockerCli, opts),
|
2019-04-12 08:24:24 +08:00
|
|
|
lsCmd(dockerCli),
|
2020-04-28 05:37:17 +08:00
|
|
|
useCmd(dockerCli, opts),
|
|
|
|
inspectCmd(dockerCli, opts),
|
|
|
|
stopCmd(dockerCli, opts),
|
2019-04-19 11:02:57 +08:00
|
|
|
installCmd(dockerCli),
|
|
|
|
uninstallCmd(dockerCli),
|
2019-04-25 07:16:30 +08:00
|
|
|
versionCmd(dockerCli),
|
2020-04-28 05:37:17 +08:00
|
|
|
pruneCmd(dockerCli, opts),
|
|
|
|
duCmd(dockerCli, opts),
|
2024-10-16 22:15:26 +08:00
|
|
|
imagetoolscmd.RootCmd(cmd, dockerCli, imagetoolscmd.RootOptions{Builder: &opts.builder}),
|
2019-03-24 12:30:29 +08:00
|
|
|
)
|
2024-03-15 18:52:11 +08:00
|
|
|
if confutil.IsExperimental() {
|
2023-08-15 15:37:42 +08:00
|
|
|
cmd.AddCommand(debugcmd.RootCmd(dockerCli,
|
|
|
|
newDebuggableBuild(dockerCli, opts),
|
|
|
|
))
|
2023-02-01 00:39:57 +08:00
|
|
|
remote.AddControllerCommands(cmd, dockerCli)
|
2022-08-29 13:14:14 +08:00
|
|
|
}
|
2023-04-11 17:45:59 +08:00
|
|
|
|
|
|
|
cmd.RegisterFlagCompletionFunc( //nolint:errcheck
|
|
|
|
"builder",
|
|
|
|
completion.BuilderNames(dockerCli),
|
|
|
|
)
|
2019-03-24 12:30:29 +08:00
|
|
|
}
|
2020-04-28 05:37:17 +08:00
|
|
|
|
|
|
|
func rootFlags(options *rootOptions, flags *pflag.FlagSet) {
|
|
|
|
flags.StringVar(&options.builder, "builder", os.Getenv("BUILDX_BUILDER"), "Override the configured builder instance")
|
2024-08-16 18:20:28 +08:00
|
|
|
flags.BoolVarP(&options.debug, "debug", "D", debug.IsEnabled(), "Enable debug logging")
|
2020-04-28 05:37:17 +08:00
|
|
|
}
|