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"
|
2019-02-22 12:49:44 -05:00
|
|
|
"os"
|
2018-12-10 10:30:19 -05:00
|
|
|
|
|
|
|
"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-02-22 12:49:44 -05:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-01 11:03:14 -05:00
|
|
|
var (
|
2024-01-11 16:25:15 -05:00
|
|
|
who, optContext string
|
|
|
|
preRun, debug bool
|
2019-03-01 11:03:14 -05:00
|
|
|
)
|
2018-12-10 10:30:19 -05:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "helloworld",
|
|
|
|
Short: "A basic Hello World plugin for tests",
|
2019-03-14 10:02:49 -04:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-13 13:50:51 -04:00
|
|
|
if preRun {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Plugin PersistentPreRunE called")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2019-02-18 06:49:41 -05:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2019-03-01 11:03:14 -05:00
|
|
|
if debug {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled")
|
|
|
|
}
|
|
|
|
|
2024-01-11 16:25:15 -05:00
|
|
|
switch optContext {
|
2019-03-01 11:03:14 -05:00
|
|
|
case "Christmas":
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "Merry Christmas!\n")
|
|
|
|
return nil
|
|
|
|
case "":
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
|
2019-02-18 06:49:41 -05:00
|
|
|
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-02-22 12:49:44 -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?")
|
2019-03-13 13:50:51 -04:00
|
|
|
flags.BoolVar(&preRun, "pre-run", false, "Log from prerun hook")
|
2019-03-01 11:03:14 -05:00
|
|
|
// These are intended to deliberately clash with the CLIs own top
|
|
|
|
// level arguments.
|
|
|
|
flags.BoolVarP(&debug, "debug", "D", false, "Enable debug")
|
2024-01-11 16:25:15 -05:00
|
|
|
flags.StringVarP(&optContext, "context", "c", "", "Is it Christmas?")
|
2018-12-10 10:30:19 -05:00
|
|
|
|
2019-02-22 12:49:44 -05:00
|
|
|
cmd.AddCommand(goodbye, apiversion, exitStatus2)
|
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
|
|
|
})
|
|
|
|
}
|