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

     1  /**
     2  * This is an example plugin where we use both arguments and flags. The plugin
     3  * will echo all arguments passed to it. The flag -uppercase will upcase the
     4  * arguments passed to the command.
     5  **/
     6  package main
     7  
     8  import (
     9  	"flag"
    10  	"fmt"
    11  	"os"
    12  	"strings"
    13  
    14  	"code.cloudfoundry.org/cli/plugin"
    15  )
    16  
    17  type PluginDemonstratingParams struct {
    18  	uppercase *bool
    19  }
    20  
    21  func main() {
    22  	plugin.Start(new(PluginDemonstratingParams))
    23  }
    24  
    25  func (pluginDemo *PluginDemonstratingParams) Run(cliConnection plugin.CliConnection, args []string) {
    26  	// Initialize flags
    27  	echoFlagSet := flag.NewFlagSet("echo", flag.ExitOnError)
    28  	uppercase := echoFlagSet.Bool("uppercase", false, "displayes all provided text in uppercase")
    29  
    30  	// Parse starting from [1] because the [0]th element is the
    31  	// name of the command
    32  	err := echoFlagSet.Parse(args[1:])
    33  	if err != nil {
    34  		fmt.Println(err)
    35  		os.Exit(1)
    36  	}
    37  
    38  	var itemToEcho string
    39  	for _, value := range echoFlagSet.Args() {
    40  		if *uppercase {
    41  			itemToEcho += strings.ToUpper(value) + " "
    42  		} else {
    43  			itemToEcho += value + " "
    44  		}
    45  	}
    46  
    47  	fmt.Println(itemToEcho)
    48  }
    49  
    50  func (pluginDemo *PluginDemonstratingParams) GetMetadata() plugin.PluginMetadata {
    51  	return plugin.PluginMetadata{
    52  		Name: "EchoDemo",
    53  		Version: plugin.VersionType{
    54  			Major: 0,
    55  			Minor: 1,
    56  			Build: 4,
    57  		},
    58  		Commands: []plugin.Command{
    59  			{
    60  				Name:     "echo",
    61  				Alias:    "repeat",
    62  				HelpText: "Echo text passed into the command. To obtain more information use --help",
    63  				UsageDetails: plugin.Usage{
    64  					Usage: "echo - print input arguments to screen\n   cf echo [-uppercase] text",
    65  					Options: map[string]string{
    66  						"uppercase": "If this param is passed, which ever word is passed to echo will be all capitals.",
    67  					},
    68  				},
    69  			},
    70  		},
    71  	}
    72  }