2018-12-10 10:30:19 -05:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-03-04 12:29:06 -05:00
|
|
|
"runtime"
|
2018-12-17 10:55:38 -05:00
|
|
|
"sync"
|
2018-12-10 10:30:19 -05:00
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli-plugins/manager"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2019-01-31 12:50:58 -05:00
|
|
|
"github.com/docker/cli/cli/connhelper"
|
2018-12-10 10:30:19 -05:00
|
|
|
cliflags "github.com/docker/cli/cli/flags"
|
2019-01-31 12:50:58 -05:00
|
|
|
"github.com/docker/docker/client"
|
2018-12-10 10:30:19 -05:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run is the top-level entry point to the CLI plugin framework. It should be called from your plugin's `main()` function.
|
|
|
|
func Run(makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) {
|
|
|
|
dockerCli, err := command.NewDockerCli()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := makeCmd(dockerCli)
|
|
|
|
|
|
|
|
cmd := newPluginCommand(dockerCli, plugin, meta)
|
|
|
|
|
|
|
|
if err := cmd.Execute(); err != nil {
|
|
|
|
if sterr, ok := err.(cli.StatusError); ok {
|
|
|
|
if sterr.Status != "" {
|
|
|
|
fmt.Fprintln(dockerCli.Err(), sterr.Status)
|
|
|
|
}
|
|
|
|
// StatusError should only be used for errors, and all errors should
|
|
|
|
// have a non-zero exit status, so never exit with 0
|
|
|
|
if sterr.StatusCode == 0 {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(sterr.StatusCode)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(dockerCli.Err(), err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 10:55:38 -05:00
|
|
|
// options encapsulates the ClientOptions and FlagSet constructed by
|
|
|
|
// `newPluginCommand` such that they can be finalized by our
|
|
|
|
// `PersistentPreRunE`. This is necessary because otherwise a plugin's
|
|
|
|
// own use of that hook will shadow anything we add to the top-level
|
|
|
|
// command meaning the CLI is never Initialized.
|
|
|
|
var options struct {
|
2019-01-31 12:50:58 -05:00
|
|
|
name string
|
2018-12-17 10:55:38 -05:00
|
|
|
init, prerun sync.Once
|
|
|
|
opts *cliflags.ClientOptions
|
|
|
|
flags *pflag.FlagSet
|
|
|
|
dockerCli *command.DockerCli
|
|
|
|
}
|
|
|
|
|
|
|
|
// PersistentPreRunE must be called by any plugin command (or
|
|
|
|
// subcommand) which uses the cobra `PersistentPreRun*` hook. Plugins
|
|
|
|
// which do not make use of `PersistentPreRun*` do not need to call
|
|
|
|
// this (although it remains safe to do so). Plugins are recommended
|
|
|
|
// to use `PersistenPreRunE` to enable the error to be
|
|
|
|
// returned. Should not be called outside of a commands
|
|
|
|
// PersistentPreRunE hook and must not be run unless Run has been
|
|
|
|
// called.
|
|
|
|
func PersistentPreRunE(cmd *cobra.Command, args []string) error {
|
|
|
|
var err error
|
|
|
|
options.prerun.Do(func() {
|
|
|
|
if options.opts == nil || options.flags == nil || options.dockerCli == nil {
|
|
|
|
panic("PersistentPreRunE called without Run successfully called first")
|
|
|
|
}
|
|
|
|
// flags must be the original top-level command flags, not cmd.Flags()
|
|
|
|
options.opts.Common.SetDefaultOptions(options.flags)
|
2019-03-04 12:29:06 -05:00
|
|
|
var initopts []command.InitializeOpt
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
initopts = append(initopts, withPluginClientConn(options.name))
|
|
|
|
}
|
|
|
|
err = options.dockerCli.Initialize(options.opts, initopts...)
|
2018-12-17 10:55:38 -05:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2018-12-10 10:30:19 -05:00
|
|
|
|
2019-01-31 12:50:58 -05:00
|
|
|
func withPluginClientConn(name string) command.InitializeOpt {
|
|
|
|
return command.WithInitializeClient(func(dockerCli *command.DockerCli) (client.APIClient, error) {
|
|
|
|
cmd := "docker"
|
|
|
|
if x := os.Getenv(manager.ReexecEnvvar); x != "" {
|
|
|
|
cmd = x
|
|
|
|
}
|
|
|
|
var flags []string
|
|
|
|
|
|
|
|
// Accumulate all the global arguments, that is those
|
|
|
|
// up to (but not including) the plugin's name. This
|
|
|
|
// ensures that `docker system dial-stdio` is
|
|
|
|
// evaluating the same set of `--config`, `--tls*` etc
|
|
|
|
// global options as the plugin was called with, which
|
|
|
|
// in turn is the same as what the original docker
|
|
|
|
// invocation was passed.
|
|
|
|
for _, a := range os.Args[1:] {
|
|
|
|
if a == name {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
flags = append(flags, a)
|
|
|
|
}
|
|
|
|
flags = append(flags, "system", "dial-stdio")
|
|
|
|
|
|
|
|
helper, err := connhelper.GetCommandConnectionHelper(cmd, flags...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewClientWithOpts(client.WithDialContext(helper.Dialer))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-17 10:55:38 -05:00
|
|
|
func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) *cobra.Command {
|
2019-02-01 05:34:44 -05:00
|
|
|
name := plugin.Name()
|
2018-12-10 10:30:19 -05:00
|
|
|
fullname := manager.NamePrefix + name
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2018-12-17 10:55:38 -05:00
|
|
|
Use: fmt.Sprintf("docker [OPTIONS] %s [ARG...]", name),
|
|
|
|
Short: fullname + " is a Docker CLI plugin",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
TraverseChildren: true,
|
|
|
|
PersistentPreRunE: PersistentPreRunE,
|
2018-12-10 10:30:19 -05:00
|
|
|
DisableFlagsInUseLine: true,
|
|
|
|
}
|
2018-12-17 10:55:38 -05:00
|
|
|
opts, flags := cli.SetupPluginRootCommand(cmd)
|
2018-12-10 10:30:19 -05:00
|
|
|
|
|
|
|
cmd.SetOutput(dockerCli.Out())
|
|
|
|
|
|
|
|
cmd.AddCommand(
|
|
|
|
plugin,
|
|
|
|
newMetadataSubcommand(plugin, meta),
|
|
|
|
)
|
|
|
|
|
|
|
|
cli.DisableFlagsInUseLine(cmd)
|
|
|
|
|
2018-12-17 10:55:38 -05:00
|
|
|
options.init.Do(func() {
|
2019-01-31 12:50:58 -05:00
|
|
|
options.name = name
|
2018-12-17 10:55:38 -05:00
|
|
|
options.opts = opts
|
|
|
|
options.flags = flags
|
|
|
|
options.dockerCli = dockerCli
|
|
|
|
})
|
2018-12-10 10:30:19 -05:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMetadataSubcommand(plugin *cobra.Command, meta manager.Metadata) *cobra.Command {
|
|
|
|
if meta.ShortDescription == "" {
|
|
|
|
meta.ShortDescription = plugin.Short
|
|
|
|
}
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: manager.MetadataSubcommandName,
|
|
|
|
Hidden: true,
|
2019-01-31 12:50:58 -05:00
|
|
|
// Suppress the global/parent PersistentPreRunE, which needlessly initializes the client and tries to connect to the daemon.
|
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
|
2018-12-10 10:30:19 -05:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
enc.SetEscapeHTML(false)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
return enc.Encode(meta)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|