github.com/arunkumar7540/cli@v6.45.0+incompatible/plugin/plugin_examples/echo.go (about)

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