github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/cli-plugins/examples/helloworld/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/docker/cli/cli-plugins/manager"
     9  	"github.com/docker/cli/cli-plugins/plugin"
    10  	"github.com/docker/cli/cli/command"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  func main() {
    15  	plugin.Run(func(dockerCli command.Cli) *cobra.Command {
    16  		goodbye := &cobra.Command{
    17  			Use:   "goodbye",
    18  			Short: "Say Goodbye instead of Hello",
    19  			Run: func(cmd *cobra.Command, _ []string) {
    20  				fmt.Fprintln(dockerCli.Out(), "Goodbye World!")
    21  			},
    22  		}
    23  		apiversion := &cobra.Command{
    24  			Use:   "apiversion",
    25  			Short: "Print the API version of the server",
    26  			RunE: func(_ *cobra.Command, _ []string) error {
    27  				cli := dockerCli.Client()
    28  				ping, err := cli.Ping(context.Background())
    29  				if err != nil {
    30  					return err
    31  				}
    32  				fmt.Println(ping.APIVersion)
    33  				return nil
    34  			},
    35  		}
    36  
    37  		exitStatus2 := &cobra.Command{
    38  			Use:   "exitstatus2",
    39  			Short: "Exit with status 2",
    40  			RunE: func(_ *cobra.Command, _ []string) error {
    41  				fmt.Fprintln(dockerCli.Err(), "Exiting with error status 2")
    42  				os.Exit(2)
    43  				return nil
    44  			},
    45  		}
    46  
    47  		var (
    48  			who, context  string
    49  			preRun, debug bool
    50  		)
    51  		cmd := &cobra.Command{
    52  			Use:   "helloworld",
    53  			Short: "A basic Hello World plugin for tests",
    54  			PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    55  				if err := plugin.PersistentPreRunE(cmd, args); err != nil {
    56  					return err
    57  				}
    58  				if preRun {
    59  					fmt.Fprintf(dockerCli.Err(), "Plugin PersistentPreRunE called")
    60  				}
    61  				return nil
    62  			},
    63  			RunE: func(cmd *cobra.Command, args []string) error {
    64  				if debug {
    65  					fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled")
    66  				}
    67  
    68  				switch context {
    69  				case "Christmas":
    70  					fmt.Fprintf(dockerCli.Out(), "Merry Christmas!\n")
    71  					return nil
    72  				case "":
    73  					// nothing
    74  				}
    75  
    76  				if who == "" {
    77  					who, _ = dockerCli.ConfigFile().PluginConfig("helloworld", "who")
    78  				}
    79  				if who == "" {
    80  					who = "World"
    81  				}
    82  
    83  				fmt.Fprintf(dockerCli.Out(), "Hello %s!\n", who)
    84  				dockerCli.ConfigFile().SetPluginConfig("helloworld", "lastwho", who)
    85  				return dockerCli.ConfigFile().Save()
    86  			},
    87  		}
    88  
    89  		flags := cmd.Flags()
    90  		flags.StringVar(&who, "who", "", "Who are we addressing?")
    91  		flags.BoolVar(&preRun, "pre-run", false, "Log from prerun hook")
    92  		// These are intended to deliberately clash with the CLIs own top
    93  		// level arguments.
    94  		flags.BoolVarP(&debug, "debug", "D", false, "Enable debug")
    95  		flags.StringVarP(&context, "context", "c", "", "Is it Christmas?")
    96  
    97  		cmd.AddCommand(goodbye, apiversion, exitStatus2)
    98  		return cmd
    99  	},
   100  		manager.Metadata{
   101  			SchemaVersion: "0.1.0",
   102  			Vendor:        "Docker Inc.",
   103  			Version:       "testing",
   104  		})
   105  }