github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/plugin/v7/cli_connection.go (about) 1 // +build V7 2 3 package v7 4 5 import ( 6 "fmt" 7 "net" 8 "net/rpc" 9 "os" 10 "time" 11 12 plugin_models "code.cloudfoundry.org/cli/plugin/v7/models" 13 ) 14 15 type cliConnection struct { 16 cliServerPort string 17 } 18 19 func NewCliConnection(cliServerPort string) *cliConnection { 20 return &cliConnection{ 21 cliServerPort: cliServerPort, 22 } 23 } 24 25 func (c *cliConnection) AccessToken() (string, error) { 26 var result string 27 28 err := c.withClientDo(func(client *rpc.Client) error { 29 return client.Call("CliRpcCmd.AccessToken", "", &result) 30 }) 31 32 return result, err 33 } 34 35 func (c *cliConnection) ApiEndpoint() (string, error) { 36 var result string 37 38 err := c.withClientDo(func(client *rpc.Client) error { 39 return client.Call("CliRpcCmd.ApiEndpoint", "", &result) 40 }) 41 42 return result, err 43 } 44 45 func (c *cliConnection) GetApp(appName string) (plugin_models.DetailedApplicationSummary, error) { 46 var result plugin_models.DetailedApplicationSummary 47 48 err := c.withClientDo(func(client *rpc.Client) error { 49 return client.Call("CliRpcCmd.GetApp", appName, &result) 50 }) 51 52 return result, err 53 } 54 55 func (c *cliConnection) GetApps() ([]plugin_models.Application, error) { 56 var result []plugin_models.Application 57 58 err := c.withClientDo(func(client *rpc.Client) error { 59 return client.Call("CliRpcCmd.GetApps", "", &result) 60 }) 61 62 return result, err 63 } 64 65 func (c *cliConnection) GetCurrentOrg() (plugin_models.Org, error) { 66 var result plugin_models.Org 67 68 err := c.withClientDo(func(client *rpc.Client) error { 69 return client.Call("CliRpcCmd.GetCurrentOrg", "", &result) 70 }) 71 72 return result, err 73 } 74 75 func (c *cliConnection) GetCurrentSpace() (plugin_models.Space, error) { 76 var result plugin_models.Space 77 78 err := c.withClientDo(func(client *rpc.Client) error { 79 return client.Call("CliRpcCmd.GetCurrentSpace", "", &result) 80 }) 81 82 return result, err 83 } 84 85 func (c *cliConnection) GetOrg(orgName string) (plugin_models.OrgSummary, error) { 86 var result plugin_models.OrgSummary 87 88 err := c.withClientDo(func(client *rpc.Client) error { 89 return client.Call("CliRpcCmd.GetOrg", orgName, &result) 90 }) 91 92 return result, err 93 } 94 95 func (c *cliConnection) IsLoggedIn() (bool, error) { 96 var result bool 97 err := c.withClientDo(func(client *rpc.Client) error { 98 return client.Call("CliRpcCmd.IsLoggedIn", "", &result) 99 }) 100 return result, err 101 } 102 103 func (c *cliConnection) Username() (string, error) { 104 var result string 105 106 err := c.withClientDo(func(client *rpc.Client) error { 107 return client.Call("CliRpcCmd.Username", "", &result) 108 }) 109 110 return result, err 111 } 112 113 func (c *cliConnection) IsSkipSSLValidation() (bool, error) { 114 var result bool 115 116 err := c.withClientDo(func(client *rpc.Client) error { 117 return client.Call("CliRpcCmd.IsSkipSSLValidation", "", &result) 118 }) 119 120 return result, err 121 } 122 123 func (c *cliConnection) isMinCliVersion(version string) bool { 124 var result bool 125 126 err := c.withClientDo(func(client *rpc.Client) error { 127 return client.Call("CliRpcCmd.IsMinCliVersion", version, &result) 128 }) 129 130 if err != nil { 131 fmt.Println(err) 132 os.Exit(1) 133 } 134 135 return result 136 } 137 138 func (c *cliConnection) pingCLI() { 139 //call back to cf saying we have been setup 140 var connErr error 141 var conn net.Conn 142 for i := 0; i < 5; i++ { 143 conn, connErr = net.Dial("tcp", "127.0.0.1:"+c.cliServerPort) 144 if connErr != nil { 145 time.Sleep(200 * time.Millisecond) 146 } else { 147 conn.Close() 148 break 149 } 150 } 151 if connErr != nil { 152 fmt.Println(connErr) 153 os.Exit(1) 154 } 155 } 156 157 func (c *cliConnection) sendPluginMetadataToCliServer(metadata PluginMetadata) { 158 var success bool 159 160 err := c.withClientDo(func(client *rpc.Client) error { 161 return client.Call("CliRpcCmd.SetPluginMetadata", metadata, &success) 162 }) 163 164 if err != nil { 165 fmt.Println(err) 166 os.Exit(1) 167 } 168 169 if !success { 170 os.Exit(1) 171 } 172 173 os.Exit(0) 174 } 175 176 func (c *cliConnection) withClientDo(f func(client *rpc.Client) error) error { 177 client, err := rpc.Dial("tcp", "127.0.0.1:"+c.cliServerPort) 178 if err != nil { 179 return err 180 } 181 defer client.Close() 182 183 return f(client) 184 }