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