forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_connection.go
383 lines (280 loc) · 8.78 KB
/
cli_connection.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package plugin
import (
"errors"
"fmt"
"net"
"net/rpc"
"os"
"time"
"github.com/cloudfoundry/cli/plugin/models"
)
type cliConnection struct {
cliServerPort string
}
func NewCliConnection(cliServerPort string) *cliConnection {
return &cliConnection{
cliServerPort: cliServerPort,
}
}
func (c *cliConnection) withClientDo(f func(client *rpc.Client) error) error {
client, err := rpc.Dial("tcp", "127.0.0.1:"+c.cliServerPort)
if err != nil {
return err
}
defer client.Close()
return f(client)
}
func (c *cliConnection) sendPluginMetadataToCliServer(metadata PluginMetadata) {
var success bool
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.SetPluginMetadata", metadata, &success)
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !success {
os.Exit(1)
}
os.Exit(0)
}
func (c *cliConnection) isMinCliVersion(version string) bool {
var result bool
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.IsMinCliVersion", version, &result)
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return result
}
func (c *cliConnection) CliCommandWithoutTerminalOutput(args ...string) ([]string, error) {
return c.callCliCommand(true, args...)
}
func (c *cliConnection) CliCommand(args ...string) ([]string, error) {
return c.callCliCommand(false, args...)
}
func (c *cliConnection) callCliCommand(silently bool, args ...string) ([]string, error) {
var (
success bool
cmdOutput []string
callCoreCommandErr error
getOutputAndResetErr error
disableTerminalOutputErr error
)
c.withClientDo(func(client *rpc.Client) error {
disableTerminalOutputErr = client.Call("CliRpcCmd.DisableTerminalOutput", silently, &success)
callCoreCommandErr = client.Call("CliRpcCmd.CallCoreCommand", args, &success)
getOutputAndResetErr = client.Call("CliRpcCmd.GetOutputAndReset", success, &cmdOutput)
return nil
})
if disableTerminalOutputErr != nil {
return []string{}, disableTerminalOutputErr
}
if callCoreCommandErr != nil {
return []string{}, callCoreCommandErr
}
if !success {
return []string{}, errors.New("Error executing cli core command")
}
if getOutputAndResetErr != nil {
return []string{}, errors.New("something completely unexpected happened")
}
return cmdOutput, nil
}
func (c *cliConnection) pingCLI() {
//call back to cf saying we have been setup
var connErr error
var conn net.Conn
for i := 0; i < 5; i++ {
conn, connErr = net.Dial("tcp", "127.0.0.1:"+c.cliServerPort)
if connErr != nil {
time.Sleep(200 * time.Millisecond)
} else {
conn.Close()
break
}
}
if connErr != nil {
fmt.Println(connErr)
os.Exit(1)
}
}
func (c *cliConnection) GetCurrentOrg() (plugin_models.Organization, error) {
var result plugin_models.Organization
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetCurrentOrg", "", &result)
})
return result, err
}
func (c *cliConnection) GetCurrentSpace() (plugin_models.Space, error) {
var result plugin_models.Space
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetCurrentSpace", "", &result)
})
return result, err
}
func (c *cliConnection) Username() (string, error) {
var result string
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.Username", "", &result)
})
return result, err
}
func (c *cliConnection) UserGuid() (string, error) {
var result string
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.UserGuid", "", &result)
})
return result, err
}
func (c *cliConnection) UserEmail() (string, error) {
var result string
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.UserEmail", "", &result)
})
return result, err
}
func (c *cliConnection) IsSSLDisabled() (bool, error) {
var result bool
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.IsSSLDisabled", "", &result)
})
return result, err
}
func (c *cliConnection) IsLoggedIn() (bool, error) {
var result bool
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.IsLoggedIn", "", &result)
})
return result, err
}
func (c *cliConnection) HasOrganization() (bool, error) {
var result bool
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.HasOrganization", "", &result)
})
return result, err
}
func (c *cliConnection) HasSpace() (bool, error) {
var result bool
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.HasSpace", "", &result)
})
return result, err
}
func (c *cliConnection) ApiEndpoint() (string, error) {
var result string
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.ApiEndpoint", "", &result)
})
return result, err
}
func (c *cliConnection) HasAPIEndpoint() (bool, error) {
var result bool
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.HasAPIEndpoint", "", &result)
})
return result, err
}
func (c *cliConnection) ApiVersion() (string, error) {
var result string
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.ApiVersion", "", &result)
})
return result, err
}
func (c *cliConnection) LoggregatorEndpoint() (string, error) {
var result string
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.LoggregatorEndpoint", "", &result)
})
return result, err
}
func (c *cliConnection) DopplerEndpoint() (string, error) {
var result string
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.DopplerEndpoint", "", &result)
})
return result, err
}
func (c *cliConnection) AccessToken() (string, error) {
var result string
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.AccessToken", "", &result)
})
return result, err
}
func (c *cliConnection) GetApp(appName string) (plugin_models.GetAppModel, error) {
var result plugin_models.GetAppModel
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetApp", appName, &result)
})
return result, err
}
func (c *cliConnection) GetApps() ([]plugin_models.GetAppsModel, error) {
var result []plugin_models.GetAppsModel
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetApps", "", &result)
})
return result, err
}
func (c *cliConnection) GetOrgs() ([]plugin_models.GetOrgs_Model, error) {
var result []plugin_models.GetOrgs_Model
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetOrgs", "", &result)
})
return result, err
}
func (c *cliConnection) GetSpaces() ([]plugin_models.GetSpaces_Model, error) {
var result []plugin_models.GetSpaces_Model
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetSpaces", "", &result)
})
return result, err
}
func (c *cliConnection) GetServices() ([]plugin_models.GetServices_Model, error) {
var result []plugin_models.GetServices_Model
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetServices", "", &result)
})
return result, err
}
func (c *cliConnection) GetOrgUsers(orgName string, args ...string) ([]plugin_models.GetOrgUsers_Model, error) {
var result []plugin_models.GetOrgUsers_Model
cmdArgs := append([]string{orgName}, args...)
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetOrgUsers", cmdArgs, &result)
})
return result, err
}
func (c *cliConnection) GetSpaceUsers(orgName string, spaceName string) ([]plugin_models.GetSpaceUsers_Model, error) {
var result []plugin_models.GetSpaceUsers_Model
cmdArgs := []string{orgName, spaceName}
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetSpaceUsers", cmdArgs, &result)
})
return result, err
}
func (c *cliConnection) GetOrg(orgName string) (plugin_models.GetOrg_Model, error) {
var result plugin_models.GetOrg_Model
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetOrg", orgName, &result)
})
return result, err
}
func (c *cliConnection) GetSpace(spaceName string) (plugin_models.GetSpace_Model, error) {
var result plugin_models.GetSpace_Model
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetSpace", spaceName, &result)
})
return result, err
}
func (c *cliConnection) GetService(serviceInstance string) (plugin_models.GetService_Model, error) {
var result plugin_models.GetService_Model
err := c.withClientDo(func(client *rpc.Client) error {
return client.Call("CliRpcCmd.GetService", serviceInstance, &result)
})
return result, err
}