forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_connection.go
95 lines (78 loc) · 2.06 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
package plugin
import (
"errors"
"fmt"
"net"
"net/rpc"
"os"
"time"
)
type cliConnection struct {
cliServerPort string
}
func NewCliConnection(cliServerPort string) *cliConnection {
return &cliConnection{
cliServerPort: cliServerPort,
}
}
func (cliConnection *cliConnection) sendPluginMetadataToCliServer(metadata PluginMetadata) {
cliServerConn, err := rpc.Dial("tcp", "127.0.0.1:"+cliConnection.cliServerPort)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var success bool
err = cliServerConn.Call("CliRpcCmd.SetPluginMetadata", metadata, &success)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !success {
os.Exit(1)
}
os.Exit(0)
}
func (cliConnection *cliConnection) CliCommandWithoutTerminalOutput(args ...string) ([]string, error) {
return cliConnection.callCliCommand(true, args...)
}
func (cliConnection *cliConnection) CliCommand(args ...string) ([]string, error) {
return cliConnection.callCliCommand(false, args...)
}
func (cliConnection *cliConnection) callCliCommand(silently bool, args ...string) ([]string, error) {
client, err := rpc.Dial("tcp", "127.0.0.1:"+cliConnection.cliServerPort)
if err != nil {
return []string{}, err
}
var success bool
client.Call("CliRpcCmd.DisableTerminalOutput", silently, &success)
err = client.Call("CliRpcCmd.CallCoreCommand", args, &success)
var cmdOutput []string
outputErr := client.Call("CliRpcCmd.GetOutputAndReset", success, &cmdOutput)
if err != nil {
return cmdOutput, err
} else if !success {
return cmdOutput, errors.New("Error executing cli core command")
}
if outputErr != nil {
return cmdOutput, errors.New("something completely unexpected happened")
}
return cmdOutput, nil
}
func (cliConnection *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:"+cliConnection.cliServerPort)
if connErr != nil {
time.Sleep(200 * time.Millisecond)
} else {
conn.Close()
break
}
}
if connErr != nil {
fmt.Println(connErr)
os.Exit(1)
}
}