2019-03-24 12:30:29 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-04-28 05:37:17 +08:00
|
|
|
"os"
|
|
|
|
|
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"
|
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"
|
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 {
|
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: `"`,
|
|
|
|
},
|
2019-04-12 00:09:03 +08:00
|
|
|
}
|
|
|
|
if isPlugin {
|
|
|
|
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
2019-03-26 10:01:31 +08:00
|
|
|
return plugin.PersistentPreRunE(cmd, args)
|
2019-04-12 00:09:03 +08:00
|
|
|
}
|
2022-04-27 23:33:12 +08:00
|
|
|
} else {
|
|
|
|
// 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",
|
|
|
|
))
|
|
|
|
|
2022-01-21 07:36:06 +08:00
|
|
|
// filter out useless commandConn.CloseWrite warning message that can occur
|
|
|
|
// when listing builder instances with "buildx ls" for those that are
|
|
|
|
// unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
|
|
|
|
// https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/connhelper/commandconn/commandconn.go#L203-L214
|
|
|
|
logrus.AddHook(logutil.NewFilter([]logrus.Level{
|
|
|
|
logrus.WarnLevel,
|
|
|
|
},
|
|
|
|
"commandConn.CloseWrite:",
|
2022-04-29 23:24:13 +08:00
|
|
|
"commandConn.CloseRead:",
|
2022-01-21 07:36:06 +08:00
|
|
|
))
|
|
|
|
|
2019-03-24 12:30:29 +08:00
|
|
|
addCommands(cmd, dockerCli)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
type rootOptions struct {
|
|
|
|
builder string
|
|
|
|
}
|
|
|
|
|
2019-03-24 12:30:29 +08:00
|
|
|
func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
|
2020-04-28 05:37:17 +08:00
|
|
|
opts := &rootOptions{}
|
|
|
|
rootFlags(opts, cmd.PersistentFlags())
|
|
|
|
|
2019-03-24 12:30:29 +08:00
|
|
|
cmd.AddCommand(
|
2020-04-28 05:37:17 +08:00
|
|
|
buildCmd(dockerCli, opts),
|
|
|
|
bakeCmd(dockerCli, opts),
|
2019-04-12 08:24:24 +08:00
|
|
|
createCmd(dockerCli),
|
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),
|
2022-04-13 21:36:48 +08:00
|
|
|
imagetoolscmd.RootCmd(dockerCli, imagetoolscmd.RootOptions{Builder: &opts.builder}),
|
2019-03-24 12:30:29 +08:00
|
|
|
)
|
2022-08-29 13:14:14 +08:00
|
|
|
if isExperimental() {
|
2023-02-01 00:39:57 +08:00
|
|
|
remote.AddControllerCommands(cmd, dockerCli)
|
2022-08-29 13:14:14 +08:00
|
|
|
}
|
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")
|
|
|
|
}
|