forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rpc_server_example.go
123 lines (101 loc) · 2.75 KB
/
test_rpc_server_example.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* This plugin demonstrate the use of Test driven development using the test rpc server
* This allows the plugin to be tested independently without relying on CF CLI
*/
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/plugin"
)
type DemoCmd struct{}
type AppsModel struct {
NextURL string `json:"next_url,omitempty"`
Resources []AppModel `json:"resources"`
}
type EntityModel struct {
Name string `json:"name"`
State string `json:"state"`
}
type AppModel struct {
Entity EntityModel `json:"entity"`
}
func (c *DemoCmd) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "App-Lister",
Commands: []plugin.Command{
{
Name: "list-apps",
HelpText: "curl /v2/apps to get a list of apps",
UsageDetails: plugin.Usage{
Usage: "cf demo-curl [--started | --stopped]",
Options: map[string]string{
"--started": "Shows only apps that are started",
"--stopped": "Shows only apps that are stopped",
},
},
},
},
}
}
func main() {
plugin.Start(new(DemoCmd))
}
func (c *DemoCmd) Run(cliConnection plugin.CliConnection, args []string) {
switch args[0] {
case "list-apps":
fc, err := parseArguments(args)
if err != nil {
exit1(err.Error())
}
endpoint, err := cliConnection.ApiEndpoint()
if err != nil {
exit1("Error getting targeted endpoint: " + err.Error())
}
fmt.Printf("Listing apps @ endpoint %s/v2/apps...\n\n", endpoint)
allApps, err := getAllApps(cliConnection)
if err != nil {
exit1("Error curling v2/apps: " + err.Error())
}
for _, app := range allApps.Resources {
if (fc.IsSet("started") && app.Entity.State == "STARTED") ||
(fc.IsSet("stopped") && app.Entity.State == "STOPPED") ||
(!fc.IsSet("stopped") && !fc.IsSet("started")) {
fmt.Println(app.Entity.Name)
}
}
case "CLI-MESSAGE-UNINSTALL":
fmt.Println("Thanks for using this demo")
}
}
func getAllApps(cliConnection plugin.CliConnection) (AppsModel, error) {
nextURL := "v2/apps"
allApps := AppsModel{}
for nextURL != "" {
output, err := cliConnection.CliCommandWithoutTerminalOutput("curl", nextURL)
if err != nil {
return AppsModel{}, err
}
apps := AppsModel{}
err = json.Unmarshal([]byte(output[0]), &apps)
if err != nil {
return AppsModel{}, err
}
allApps.Resources = append(allApps.Resources, apps.Resources...)
nextURL = apps.NextURL
}
return allApps, nil
}
func parseArguments(args []string) (flags.FlagContext, error) {
fc := flags.New()
fc.NewBoolFlag("started", "s", "Shows only apps that are started")
fc.NewBoolFlag("stopped", "o", "Shows only apps that are stopped")
err := fc.Parse(args...)
return fc, err
}
func exit1(err string) {
fmt.Println("FAILED\n" + err)
os.Exit(1)
}