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