github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/plugin/plugin_examples/call_cli_cmd/main/call_cli_cmd.go (about)

     1  /**
     2  * This plugin is an example plugin that allows a user to call a cli-command
     3  * by typing `cf cli-command name-of-command args.....`. This plugin also prints
     4  * the output returned by the CLI when a cli-command is invoked.
     5   */
     6  package main
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"code.cloudfoundry.org/cli/plugin"
    12  )
    13  
    14  type CliCmd struct{}
    15  
    16  func (c *CliCmd) GetMetadata() plugin.PluginMetadata {
    17  	return plugin.PluginMetadata{
    18  		Name: "CliCmd",
    19  		Version: plugin.VersionType{
    20  			Major: 1,
    21  			Minor: 1,
    22  			Build: 0,
    23  		},
    24  		Commands: []plugin.Command{
    25  			{
    26  				Name:     "cli-command",
    27  				HelpText: "Command to call cli command. It passes all arguments through to the command",
    28  				UsageDetails: plugin.Usage{
    29  					Usage: "cli-command\n   cf cli-command CORE-COMMAND",
    30  				},
    31  			},
    32  		},
    33  	}
    34  }
    35  
    36  func main() {
    37  	plugin.Start(new(CliCmd))
    38  }
    39  
    40  func (c *CliCmd) Run(cliConnection plugin.CliConnection, args []string) {
    41  	// Invoke the cf command passed as the set of arguments
    42  	// after the first argument.
    43  	//
    44  	// Calls to plugin.CliCommand([]string) must be done after the invocation
    45  	// of plugin.Start() to ensure the environment is bootstrapped.
    46  	output, err := cliConnection.CliCommand(args[1:]...)
    47  
    48  	// The call to plugin.CliCommand() returns an error if the cli command
    49  	// returns a non-zero return code. The output written by the CLI
    50  	// is returned in any case.
    51  	if err != nil {
    52  		fmt.Println("PLUGIN ERROR: Error from CliCommand: ", err)
    53  	}
    54  
    55  	// Print the output returned from the CLI command.
    56  	fmt.Println("")
    57  	fmt.Println("---------- Command output from the plugin ----------")
    58  	for index, val := range output {
    59  		fmt.Println("#", index, " value: ", val)
    60  	}
    61  	fmt.Println("----------              FIN               -----------")
    62  }