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