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"
|
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"
|
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
|
|
|
}
|
2019-03-24 12:30:29 +08:00
|
|
|
}
|
2019-04-12 00:09:03 +08:00
|
|
|
|
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:",
|
|
|
|
))
|
|
|
|
|
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),
|
2021-11-04 12:17:27 +08:00
|
|
|
imagetoolscmd.RootCmd(dockerCli, imagetoolscmd.RootOptions{Builder: opts.builder}),
|
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")
|
|
|
|
}
|