install/uninstall: allow aliasing buildx to docker builder

Currently the user can type `docker buildx` to use this tool.
This patch allows the user to install buildx as a `docker builder` alias.

As an additional benefit, this allows the regular `docker build` to hook
into `buildx build`.

Note that the install and uninstall commands are currently hidden.

Signed-off-by: Tibor Vass <tibor@docker.com>
This commit is contained in:
Tibor Vass
2019-04-19 03:02:57 +00:00
parent 7f474ed28e
commit 416b7bb23f
17 changed files with 241 additions and 67 deletions

52
commands/install.go Normal file
View File

@ -0,0 +1,52 @@
package commands
import (
"os"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type installOptions struct {
}
func runInstall(dockerCli command.Cli, in installOptions) error {
dir := config.Dir()
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Wrap(err, "could not create docker config")
}
cfg, err := config.Load(dir)
if err != nil {
return err
}
if cfg.Aliases == nil {
cfg.Aliases = map[string]string{}
}
cfg.Aliases["builder"] = "buildx"
if err := cfg.Save(); err != nil {
return errors.Wrap(err, "could not write docker config")
}
return nil
}
func installCmd(dockerCli command.Cli) *cobra.Command {
var options installOptions
cmd := &cobra.Command{
Use: "install",
Short: "Install buildx as a 'docker builder' alias",
Args: cli.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return runInstall(dockerCli, options)
},
Hidden: true,
}
return cmd
}

View File

@ -32,6 +32,8 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
useCmd(dockerCli),
inspectCmd(dockerCli),
stopCmd(dockerCli),
installCmd(dockerCli),
uninstallCmd(dockerCli),
imagetoolscmd.RootCmd(dockerCli),
)
}

58
commands/uninstall.go Normal file
View File

@ -0,0 +1,58 @@
package commands
import (
"os"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type uninstallOptions struct {
}
func runUninstall(dockerCli command.Cli, in uninstallOptions) error {
dir := config.Dir()
cfg, err := config.Load(dir)
if err != nil {
return errors.Wrap(err, "could not load docker config to uninstall 'docker builder' alias")
}
// config.Load does not return an error if config file does not exist
// so let's detect that case, to avoid writing an empty config to disk.
if _, err := os.Stat(cfg.Filename); err != nil {
if !os.IsNotExist(err) {
// should never happen, already handled in config.Load
return errors.Wrap(err, "unexpected error loading docker config")
}
// no-op
return nil
}
delete(cfg.Aliases, "builder")
if len(cfg.Aliases) == 0 {
cfg.Aliases = nil
}
if err := cfg.Save(); err != nil {
return errors.Wrap(err, "could not write docker config")
}
return nil
}
func uninstallCmd(dockerCli command.Cli) *cobra.Command {
var options uninstallOptions
cmd := &cobra.Command{
Use: "uninstall",
Short: "Uninstall the 'docker builder' alias",
Args: cli.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return runUninstall(dockerCli, options)
},
Hidden: true,
}
return cmd
}