github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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.CurrentSpace, error) { 76 var result plugin_models.CurrentSpace 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) GetSpace(spaceName string) (plugin_models.Space, error) { 96 var result plugin_models.Space 97 98 err := c.withClientDo(func(client *rpc.Client) error { 99 return client.Call("CliRpcCmd.GetSpace", spaceName, &result) 100 }) 101 102 return result, err 103 } 104 105 func (c *cliConnection) GetSpaces() ([]plugin_models.Space, error) { 106 var result []plugin_models.Space 107 108 err := c.withClientDo(func(client *rpc.Client) error { 109 return client.Call("CliRpcCmd.GetSpaces", "", &result) 110 }) 111 return result, err 112 } 113 114 func (c *cliConnection) IsLoggedIn() (bool, error) { 115 var result bool 116 err := c.withClientDo(func(client *rpc.Client) error { 117 return client.Call("CliRpcCmd.IsLoggedIn", "", &result) 118 }) 119 return result, err 120 } 121 122 func (c *cliConnection) Username() (string, error) { 123 var result string 124 125 err := c.withClientDo(func(client *rpc.Client) error { 126 return client.Call("CliRpcCmd.Username", "", &result) 127 }) 128 129 return result, err 130 } 131 132 func (c *cliConnection) IsSkipSSLValidation() (bool, error) { 133 var result bool 134 135 err := c.withClientDo(func(client *rpc.Client) error { 136 return client.Call("CliRpcCmd.IsSkipSSLValidation", "", &result) 137 }) 138 139 return result, err 140 } 141 142 func (c *cliConnection) isMinCliVersion(version string) bool { 143 var result bool 144 145 err := c.withClientDo(func(client *rpc.Client) error { 146 return client.Call("CliRpcCmd.IsMinCliVersion", version, &result) 147 }) 148 149 if err != nil { 150 fmt.Println(err) 151 os.Exit(1) 152 } 153 154 return result 155 } 156 157 func (c *cliConnection) pingCLI() { 158 //call back to cf saying we have been setup 159 var connErr error 160 var conn net.Conn 161 for i := 0; i < 5; i++ { 162 conn, connErr = net.Dial("tcp", "127.0.0.1:"+c.cliServerPort) 163 if connErr != nil { 164 time.Sleep(200 * time.Millisecond) 165 } else { 166 conn.Close() 167 break 168 } 169 } 170 if connErr != nil { 171 fmt.Println(connErr) 172 os.Exit(1) 173 } 174 } 175 176 func (c *cliConnection) sendPluginMetadataToCliServer(metadata PluginMetadata) { 177 var success bool 178 179 err := c.withClientDo(func(client *rpc.Client) error { 180 return client.Call("CliRpcCmd.SetPluginMetadata", metadata, &success) 181 }) 182 183 if err != nil { 184 fmt.Println(err) 185 os.Exit(1) 186 } 187 188 if !success { 189 os.Exit(1) 190 } 191 192 os.Exit(0) 193 } 194 195 func (c *cliConnection) withClientDo(f func(client *rpc.Client) error) error { 196 client, err := rpc.Dial("tcp", "127.0.0.1:"+c.cliServerPort) 197 if err != nil { 198 return err 199 } 200 defer client.Close() 201 202 return f(client) 203 }