github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/plugin/plugin_examples/basic_plugin.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/plugin"
     7  )
     8  
     9  // BasicPlugin is the struct implementing the interface defined by the core CLI. It can
    10  // be found at  "code.cloudfoundry.org/cli/plugin/plugin.go"
    11  type BasicPlugin struct{}
    12  
    13  // Run must be implemented by any plugin because it is part of the
    14  // plugin interface defined by the core CLI.
    15  //
    16  // Run(....) is the entry point when the core CLI is invoking a command defined
    17  // by a plugin. The first parameter, plugin.CliConnection, is a struct that can
    18  // be used to invoke cli commands. The second paramter, args, is a slice of
    19  // strings. args[0] will be the name of the command, and will be followed by
    20  // any additional arguments a cli user typed in.
    21  //
    22  // Any error handling should be handled with the plugin itself (this means printing
    23  // user facing errors). The CLI will exit 0 if the plugin exits 0 and will exit
    24  // 1 should the plugin exits nonzero.
    25  func (c *BasicPlugin) Run(cliConnection plugin.CliConnection, args []string) {
    26  	// Ensure that we called the command basic-plugin-command
    27  	if args[0] == "basic-plugin-command" {
    28  		fmt.Println("Running the basic-plugin-command")
    29  	}
    30  }
    31  
    32  // GetMetadata must be implemented as part of the plugin interface
    33  // defined by the core CLI.
    34  //
    35  // GetMetadata() returns a PluginMetadata struct. The first field, Name,
    36  // determines the name of the plugin which should generally be without spaces.
    37  // If there are spaces in the name a user will need to properly quote the name
    38  // during uninstall otherwise the name will be treated as seperate arguments.
    39  // The second value is a slice of Command structs. Our slice only contains one
    40  // Command Struct, but could contain any number of them. The first field Name
    41  // defines the command `cf basic-plugin-command` once installed into the CLI. The
    42  // second field, HelpText, is used by the core CLI to display help information
    43  // to the user in the core commands `cf help`, `cf`, or `cf -h`.
    44  func (c *BasicPlugin) GetMetadata() plugin.PluginMetadata {
    45  	return plugin.PluginMetadata{
    46  		Name: "MyBasicPlugin",
    47  		Version: plugin.VersionType{
    48  			Major: 1,
    49  			Minor: 0,
    50  			Build: 0,
    51  		},
    52  		MinCliVersion: plugin.VersionType{
    53  			Major: 6,
    54  			Minor: 7,
    55  			Build: 0,
    56  		},
    57  		Commands: []plugin.Command{
    58  			{
    59  				Name:     "basic-plugin-command",
    60  				HelpText: "Basic plugin command's help text",
    61  
    62  				// UsageDetails is optional
    63  				// It is used to show help of usage of each command
    64  				UsageDetails: plugin.Usage{
    65  					Usage: "basic-plugin-command\n   cf basic-plugin-command",
    66  				},
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  // Unlike most Go programs, the `Main()` function will not be used to run all of the
    73  // commands provided in your plugin. Main will be used to initialize the plugin
    74  // process, as well as any dependencies you might require for your
    75  // plugin.
    76  func main() {
    77  	// Any initialization for your plugin can be handled here
    78  	//
    79  	// Note: to run the plugin.Start method, we pass in a pointer to the struct
    80  	// implementing the interface defined at "code.cloudfoundry.org/cli/plugin/plugin.go"
    81  	//
    82  	// Note: The plugin's main() method is invoked at install time to collect
    83  	// metadata. The plugin will exit 0 and the Run([]string) method will not be
    84  	// invoked.
    85  	plugin.Start(new(BasicPlugin))
    86  	// Plugin code should be written in the Run([]string) method,
    87  	// ensuring the plugin environment is bootstrapped.
    88  }