2018-12-10 10:30:19 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-17 10:55:38 -05:00
|
|
|
"context"
|
2018-12-10 10:30:19 -05:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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!")
|
|
|
|
},
|
|
|
|
}
|
2018-12-17 10:55:38 -05:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
2018-12-10 10:30:19 -05:00
|
|
|
|
2019-01-17 08:38:38 -05:00
|
|
|
var who string
|
2018-12-10 10:30:19 -05:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "helloworld",
|
|
|
|
Short: "A basic Hello World plugin for tests",
|
2018-12-17 10:55:38 -05:00
|
|
|
// This is redundant but included to exercise
|
|
|
|
// the path where a plugin overrides this
|
|
|
|
// hook.
|
|
|
|
PersistentPreRunE: plugin.PersistentPreRunE,
|
2019-02-18 06:49:41 -05:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if who == "" {
|
|
|
|
who, _ = dockerCli.ConfigFile().PluginConfig("helloworld", "who")
|
|
|
|
}
|
|
|
|
if who == "" {
|
|
|
|
who = "World"
|
|
|
|
}
|
|
|
|
|
2019-01-17 08:38:38 -05:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Hello %s!\n", who)
|
2019-02-18 06:49:41 -05:00
|
|
|
dockerCli.ConfigFile().SetPluginConfig("helloworld", "lastwho", who)
|
|
|
|
return dockerCli.ConfigFile().Save()
|
2018-12-10 10:30:19 -05:00
|
|
|
},
|
|
|
|
}
|
2019-01-17 08:38:38 -05:00
|
|
|
flags := cmd.Flags()
|
2019-02-18 06:49:41 -05:00
|
|
|
flags.StringVar(&who, "who", "", "Who are we addressing?")
|
2018-12-10 10:30:19 -05:00
|
|
|
|
2018-12-17 10:55:38 -05:00
|
|
|
cmd.AddCommand(goodbye, apiversion)
|
2018-12-10 10:30:19 -05:00
|
|
|
return cmd
|
|
|
|
},
|
|
|
|
manager.Metadata{
|
|
|
|
SchemaVersion: "0.1.0",
|
|
|
|
Vendor: "Docker Inc.",
|
2018-12-11 09:03:47 -05:00
|
|
|
Version: "testing",
|
2018-12-10 10:30:19 -05:00
|
|
|
})
|
|
|
|
}
|