1
0
mirror of https://github.com/docker/cli.git synced 2025-08-29 00:47:54 +03:00

Allow plugins to make use of the cobra PersistentPreRun hooks.

Previously a plugin which used these hooks would overwrite the top-level plugin
command's use of this hook, resulting in the dockerCli object not being fully
initialised.

Provide a function which plugins can use to chain to the required behaviour.
This required some fairly ugly arrangements to preserve state (which was
previously in-scope in `newPluginCOmmand`) to be used by the new function.

Signed-off-by: Ian Campbell <ijc@docker.com>
This commit is contained in:
Ian Campbell
2018-12-17 15:55:38 +00:00
parent 53f018120a
commit e5e578abc7
4 changed files with 78 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"github.com/docker/cli/cli-plugins/manager"
@@ -18,16 +19,33 @@ func main() {
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
},
}
cmd := &cobra.Command{
Use: "helloworld",
Short: "A basic Hello World plugin for tests",
// This is redundant but included to exercise
// the path where a plugin overrides this
// hook.
PersistentPreRunE: plugin.PersistentPreRunE,
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintln(dockerCli.Out(), "Hello World!")
},
}
cmd.AddCommand(goodbye)
cmd.AddCommand(goodbye, apiversion)
return cmd
},
manager.Metadata{