github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/plugin/plugin_examples/README.md (about)

     1  If you have any questions about developing a CLI plugin, ask away on the [cf-dev mailing list](https://lists.cloudfoundry.org/archives/list/cf-dev@lists.cloudfoundry.org/) (many plugin developers there!) or the #cli channel in our Slack community.
     2  
     3  # Changes in v6.25.0
     4  - `GetApp` now returns `Path` and `Port` information.
     5  
     6  # Changes in v6.24.0
     7  - API `LoggregatorEndpoint()` is deprecated and now always returns the empty string. Use `DopplerEndpoint()` instead to obtain logs.
     8  
     9  # Changes in v6.17.0
    10  - `-v` is now a global flag to enable verbose logging of API calls, equivalent to `CF_TRACE=true`. This means that the `-v` flag will no longer be passed to plugins.
    11  
    12  # Changes in v6.14.0
    13  - API `AccessToken()` now provides a refreshed o-auth token.
    14  - [Examples](https://github.com/cloudfoundry/cli/tree/master/plugin/plugin_examples#test-driven-development-tdd) on how to use fake `CliConnection` and test RPC server for TDD development.
    15  - Fix Plugin API file descriptors leakage.
    16  - Fix bug where some CLI versions does not respect `PluginMetadata.MinCliVersion`.
    17  - The field `PackageUpdatedAt` returned by `GetApp()` API is now populated.
    18  
    19  [Complete change log ...](https://github.com/cloudfoundry/cli/blob/master/plugin/plugin_examples/CHANGELOG.md)
    20  
    21  # Developing a Plugin
    22  [Go here for documentation of the plugin API](https://github.com/cloudfoundry/cli/blob/master/plugin/plugin_examples/DOC.md)
    23  
    24  This README discusses how to develop a cf CLI plugin.
    25  For user-focused documentation, see [Using the cf CLI](http://docs.cloudfoundry.org/cf-cli/use-cli-plugins.html).
    26  
    27  *If you wish to share your plugin with the community, see [here](http://github.com/cloudfoundry-incubator/cli-plugin-repo) for plugin submission.
    28  
    29  
    30  ## Development Requirements
    31  
    32  - GoLang installed
    33  - Tagged version of CLI release source code that supports plugins; cf CLI v.6.7.0 and above
    34  
    35  ## Architecture Overview
    36  
    37  The cf CLI plugin architecture model follows the remote procedure call (RPC) model.
    38  The cf CLI invokes each plugin, runs it as an independent executable, and handles all start, stop, and clean up tasks for plugin executable resources.
    39  
    40  Here is an illustration of the work flow when a plugin command is being invoked.
    41  
    42  1: CLI launches 2 processes, the rpc server and the independent plugin executable
    43  <p align="center">
    44  <img src="https://raw.githubusercontent.com/cloudfoundry/cli/master/plugin/plugin_examples/images/rpc_flow1.png" alt="workflow 1" width="400px">
    45  </p>
    46  
    47  2: Plugin establishes a connection to the RPC server, the connection is used to invoke core cli commands.
    48  <p align="center">
    49  <img src="https://raw.githubusercontent.com/cloudfoundry/cli/master/plugin/plugin_examples/images/rpc_flow2.png" alt="workflow 1" width="400px">
    50  </p>
    51  
    52  3: When a plugin invokes a cli command, it talks to the rpc server, and the rpc server interacts with cf cli to perform the command. The result is passed back to the plugin through the rpc server.
    53  <p align="center">
    54  <img src="https://raw.githubusercontent.com/cloudfoundry/cli/master/plugin/plugin_examples/images/rpc_flow3.png" alt="workflow 1" width="400px">
    55  </p>
    56  
    57  - Plugins that you develop for the cf CLI must conform to a predefined plugin interface that we discuss below.
    58  
    59  ## Writing a Plugin
    60  
    61  [Go here for documentation of the plugin API](https://github.com/cloudfoundry/cli/blob/master/plugin/plugin_examples/DOC.md)
    62  
    63  To write a plugin for the cf CLI, implement the [predefined plugin interface](https://github.com/cloudfoundry/cli/blob/master/plugin/plugin.go).
    64  
    65  The interface uses a `Run(...)` method as the main entry point between the CLI and a plugin. This method receives the following arguments:
    66  
    67    - A struct `plugin.CliConnection` that contains methods for invoking cf CLI commands
    68    - A string array that contains the arguments passed from the `cf` process
    69  
    70  The `GetMetadata()` function informs the CLI of the name of a plugin, plugin version (optional), minimum CLI version required (optional), the commands it implements, and help text for each command that users can display with `cf help`.
    71  
    72  Plugin names with spaces must be enclosed in quotes when installed and uninstalled (e.g.: `cf install-plugin "my plugin"`). We recommend that plugin names not contain spaces to prevent the command shell from interpreting the name as multiple words.
    73  
    74    To initialize a plugin, call `plugin.Start(new(MyPluginStruct))` from within the `main()` method of your plugin. The `plugin.Start(...)` function requires a new reference to the struct that implements the defined interface.
    75  
    76  This repo contains a basic plugin example [here](https://github.com/cloudfoundry/cli/blob/master/plugin/plugin_examples/basic_plugin.go).<br>
    77  To see more examples, go [here](https://github.com/cloudfoundry/cli/blob/master/plugin/plugin_examples/).
    78  
    79  ### Uninstalling A Plugin
    80  Uninstall of the plugin needs to be explicitly handled. When a user calls the `cf uninstall-plugin` command, CLI notifies the plugin via a call with `CLI-MESSAGE-UNINSTALL` as the first item in `[]args` from within the plugin's `Run(...)` method.
    81  
    82  ### Test Driven Development (TDD)
    83  An example which was developed using TDD is available:
    84  - `Test RPC server`: an RPC server to be used as a back-end for the plugin. It allows the plugin to be tested as a stand alone binary without replying on CLI as a back-end. [See example](https://github.com/cloudfoundry/cli/tree/master/plugin/plugin_examples/test_rpc_server_example)
    85  
    86  ### Using Command Line Arguments
    87  
    88  The `Run(...)` method accepts the command line arguments and flags that you define for a plugin.
    89  
    90    See the [command line arguments example] (https://github.com/cloudfoundry/cli/blob/master/plugin/plugin_examples/echo.go) included in this repo.
    91  
    92  #### Global Flags
    93  There are several global flags that will not be passed to the plugin. These are:
    94  - `-v`: equivalent to `CF_TRACE=true`, will display any API calls/responses to the user
    95  - `-h`: will process the return from the plugin's `GetMetadata` function to produce a help display
    96  
    97  ### Calling CLI Commands
    98  
    99  You can invoke CLI commands with `cliConnection.CliCommand([]args)` from within a plugin's `Run(...)` method. The `Run(...)` method receives the `cliConnection` as its first argument.
   100  
   101  The `cliConnection.CliCommand([]args)` returns the output printed by the command and an error. The output is returned as a slice of strings. The error will be present if the call to the CLI command fails.
   102  
   103  See the [test plugin example](https://github.com/cloudfoundry/cli/blob/master/integration/assets/test_plugin/test_plugin.go) included in this repo.
   104  
   105  ### Creating Interactive Plugins
   106  
   107  Because a plugin has access to stdin during a call to the `Run(...)` method, you can create interactive plugins. See the [interactive plugin example](https://github.com/cloudfoundry/cli/blob/master/plugin/plugin_examples/interactive.go) included in this repo.
   108  
   109  ### Creating Plugins with multiple commands
   110  
   111  A single plugin binary can have more than one command, and each command can have it's own help text defined. For an example of multi-command plugins, see the [multiple commands example](https://github.com/cloudfoundry/cli/blob/master/plugin/plugin_examples/multiple_commands.go)
   112  
   113  ### Enforcing a minimum CLI version required for the plugin.
   114  
   115  ```go
   116  func (c *cmd) GetMetadata() plugin.PluginMetadata {
   117  	return plugin.PluginMetadata{
   118  		Name: "Test1",
   119  		MinCliVersion: plugin.VersionType{
   120  			Major: 6,
   121  			Minor: 12,
   122  			Build: 0,
   123  		},
   124  	}
   125  }
   126  ```
   127  
   128  ## Compiling Plugin Source Code
   129  
   130  The cf CLI requires an executable file to install the plugin. You must compile the source code with the `go build` command before distributing the plugin, or instruct your users to compile the plugin source code before installing the plugin. For information about compiling Go source code, see [Compile packages and dependencies](https://golang.org/cmd/go/).
   131  
   132  ## Using Plugins
   133  
   134  After you compile a plugin, use the following commands to install and manage the plugin.
   135  
   136  ### Installing Plugins
   137  
   138  To install a plugin, run:
   139  
   140  `cf install-plugin PATH_TO_PLUGIN_BINARY`
   141  
   142  ### Listing Plugins
   143  
   144  To display a list of installed plugins and the commands available from each plugin, run:
   145  
   146  `cf plugins`
   147  
   148  ### Uninstalling Plugins
   149  
   150  To remove a plugin, run:
   151  
   152  `cf uninstall-plugin PLUGIN_NAME`
   153  
   154  ## Known Issues
   155  
   156  - When invoking a CLI command using `cliConnection.CliCommand([]args)` a plugin will not receive output generated by the cli package. This includes usage failures when executing a cli command, `cf help`, or `cli SOME-COMMAND -h`.
   157  - When invoking a CLI command using `cliConnection.CliCommand([]args)` and `CF_TRACE=true/cf -v` a plugin will receive all the output, including the trace in the returned string array. This may cause problem while trying to debug output with `CF_TRACE`. As work around, if a plugin is running `cf curl` via `CliCommand`, the following can be used to help with debugging (when the `CF_DEBUG_CURL=true`):
   158  ```go
   159    func RunCurl(cliConnection plugin.CliConnection, args []string) ([]string, error) {
   160      output, err := cliConnection.CliCommand("curl", args...)
   161      if os.Getenv("CF_DEBUG_CURL") == "true" {
   162        fmt.Println(strings.Join(output, "\n"))
   163      }
   164      return output, err
   165    }
   166  ```
   167  - Due to architectural limitations, calling CLI core commands is not concurrency-safe. The correct execution of concurrent commands is not guaranteed. An architecture restructuring is in the works to fix this in the near future.