I got a bit carried away in d4ced2ef77fcc ("allow plugins to have argument which match a top-level flag.") and broke the ability of a plugin to use the `PersistentPreRun(E)` hook on its top-level command (by unconditionally overwriting it) and also broke the plugin framework if a plugin's subcommand used those hooks (because they would shadow the root one). This could result in either `dockerCli.Client()` returning `nil` or whatever initialisation the plugin hoped to do not occuring. This change revert the relevant bits and reinstates the requirement that a plugin calls `plugin.PersistentPreRunE` if it uses that hook itself. It is at least a bit nicer now since we avoid the need for the global struct since the interesting state is now encapsulated in `tcmd` (and the closure). In principal this could be done even more simply (by calling `tcmd.Initialize` statically between `tcmd.HandleGlobalFlags` and `cmd.Execute`) however this has the downside of _always_ initialising the cli (and therefore dialing the daemon) even for the `docker-cli-plugin-metadata` command but also for the `help foo` and `foo --help` commands (Cobra short-circuits the hooks in this case). Signed-off-by: Ian Campbell <ijc@docker.com>
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/docker/cli/cli-plugins/manager"
|
|
"github.com/docker/cli/cli-plugins/plugin"
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func main() {
|
|
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
|
|
goodbye := &cobra.Command{
|
|
Use: "goodbye",
|
|
Short: "Say Goodbye instead of Hello",
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
|
fmt.Fprintln(dockerCli.Out(), "Goodbye World!")
|
|
},
|
|
}
|
|
apiversion := &cobra.Command{
|
|
Use: "apiversion",
|
|
Short: "Print the API version of the server",
|
|
RunE: func(_ *cobra.Command, _ []string) error {
|
|
cli := dockerCli.Client()
|
|
ping, err := cli.Ping(context.Background())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(ping.APIVersion)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
exitStatus2 := &cobra.Command{
|
|
Use: "exitstatus2",
|
|
Short: "Exit with status 2",
|
|
RunE: func(_ *cobra.Command, _ []string) error {
|
|
fmt.Fprintln(dockerCli.Err(), "Exiting with error status 2")
|
|
os.Exit(2)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var (
|
|
who, context string
|
|
preRun, debug bool
|
|
)
|
|
cmd := &cobra.Command{
|
|
Use: "helloworld",
|
|
Short: "A basic Hello World plugin for tests",
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
|
|
return err
|
|
}
|
|
if preRun {
|
|
fmt.Fprintf(dockerCli.Err(), "Plugin PersistentPreRunE called")
|
|
}
|
|
return nil
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if debug {
|
|
fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled")
|
|
}
|
|
|
|
switch context {
|
|
case "Christmas":
|
|
fmt.Fprintf(dockerCli.Out(), "Merry Christmas!\n")
|
|
return nil
|
|
case "":
|
|
// nothing
|
|
}
|
|
|
|
if who == "" {
|
|
who, _ = dockerCli.ConfigFile().PluginConfig("helloworld", "who")
|
|
}
|
|
if who == "" {
|
|
who = "World"
|
|
}
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "Hello %s!\n", who)
|
|
dockerCli.ConfigFile().SetPluginConfig("helloworld", "lastwho", who)
|
|
return dockerCli.ConfigFile().Save()
|
|
},
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
flags.StringVar(&who, "who", "", "Who are we addressing?")
|
|
flags.BoolVar(&preRun, "pre-run", false, "Log from prerun hook")
|
|
// These are intended to deliberately clash with the CLIs own top
|
|
// level arguments.
|
|
flags.BoolVarP(&debug, "debug", "D", false, "Enable debug")
|
|
flags.StringVarP(&context, "context", "c", "", "Is it Christmas?")
|
|
|
|
cmd.AddCommand(goodbye, apiversion, exitStatus2)
|
|
return cmd
|
|
},
|
|
manager.Metadata{
|
|
SchemaVersion: "0.1.0",
|
|
Vendor: "Docker Inc.",
|
|
Version: "testing",
|
|
})
|
|
}
|