github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/plugin_examples/test_rpc_server_example/test_rpc_server_example.go (about) 1 /** 2 * This plugin demonstrate the use of Test driven development using the test rpc server 3 * This allows the plugin to be tested independently without relying on CF CLI 4 */ 5 package main 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "os" 11 12 "github.com/cloudfoundry/cli/cf/flags" 13 "github.com/cloudfoundry/cli/plugin" 14 ) 15 16 type DemoCmd struct{} 17 18 type AppsModel struct { 19 NextURL string `json:"next_url,omitempty"` 20 Resources []AppModel `json:"resources"` 21 } 22 23 type EntityModel struct { 24 Name string `json:"name"` 25 State string `json:"state"` 26 } 27 28 type AppModel struct { 29 Entity EntityModel `json:"entity"` 30 } 31 32 func (c *DemoCmd) GetMetadata() plugin.PluginMetadata { 33 return plugin.PluginMetadata{ 34 Name: "App-Lister", 35 Commands: []plugin.Command{ 36 { 37 Name: "list-apps", 38 HelpText: "curl /v2/apps to get a list of apps", 39 UsageDetails: plugin.Usage{ 40 Usage: "cf demo-curl [--started | --stopped]", 41 Options: map[string]string{ 42 "--started": "Shows only apps that are started", 43 "--stopped": "Shows only apps that are stopped", 44 }, 45 }, 46 }, 47 }, 48 } 49 } 50 51 func main() { 52 plugin.Start(new(DemoCmd)) 53 } 54 55 func (c *DemoCmd) Run(cliConnection plugin.CliConnection, args []string) { 56 switch args[0] { 57 case "list-apps": 58 fc, err := parseArguments(args) 59 if err != nil { 60 exit1(err.Error()) 61 } 62 63 endpoint, err := cliConnection.ApiEndpoint() 64 if err != nil { 65 exit1("Error getting targeted endpoint: " + err.Error()) 66 } 67 fmt.Printf("Listing apps @ endpoint %s/v2/apps...\n\n", endpoint) 68 69 allApps, err := getAllApps(cliConnection) 70 if err != nil { 71 exit1("Error curling v2/apps: " + err.Error()) 72 } 73 74 for _, app := range allApps.Resources { 75 if (fc.IsSet("started") && app.Entity.State == "STARTED") || 76 (fc.IsSet("stopped") && app.Entity.State == "STOPPED") || 77 (!fc.IsSet("stopped") && !fc.IsSet("started")) { 78 fmt.Println(app.Entity.Name) 79 } 80 } 81 82 case "CLI-MESSAGE-UNINSTALL": 83 fmt.Println("Thanks for using this demo") 84 } 85 } 86 87 func getAllApps(cliConnection plugin.CliConnection) (AppsModel, error) { 88 nextURL := "v2/apps" 89 allApps := AppsModel{} 90 91 for nextURL != "" { 92 output, err := cliConnection.CliCommandWithoutTerminalOutput("curl", nextURL) 93 if err != nil { 94 return AppsModel{}, err 95 } 96 97 apps := AppsModel{} 98 err = json.Unmarshal([]byte(output[0]), &apps) 99 if err != nil { 100 return AppsModel{}, err 101 } 102 103 allApps.Resources = append(allApps.Resources, apps.Resources...) 104 105 nextURL = apps.NextURL 106 } 107 108 return allApps, nil 109 } 110 111 func parseArguments(args []string) (flags.FlagContext, error) { 112 fc := flags.New() 113 fc.NewBoolFlag("started", "s", "Shows only apps that are started") 114 fc.NewBoolFlag("stopped", "o", "Shows only apps that are stopped") 115 err := fc.Parse(args...) 116 117 return fc, err 118 } 119 120 func exit1(err string) { 121 fmt.Println("FAILED\n" + err) 122 os.Exit(1) 123 }