github.com/cloudfoundry/cli@v7.1.0+incompatible/plugin/cli_connection.go (about)

     1  package plugin
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net"
     7  	"net/rpc"
     8  	"os"
     9  	"time"
    10  
    11  	plugin_models "code.cloudfoundry.org/cli/plugin/models"
    12  )
    13  
    14  type cliConnection struct {
    15  	cliServerPort string
    16  }
    17  
    18  func NewCliConnection(cliServerPort string) *cliConnection {
    19  	return &cliConnection{
    20  		cliServerPort: cliServerPort,
    21  	}
    22  }
    23  
    24  func (c *cliConnection) withClientDo(f func(client *rpc.Client) error) error {
    25  	client, err := rpc.Dial("tcp", "127.0.0.1:"+c.cliServerPort)
    26  	if err != nil {
    27  		return err
    28  	}
    29  	defer client.Close()
    30  
    31  	return f(client)
    32  }
    33  
    34  func (c *cliConnection) sendPluginMetadataToCliServer(metadata PluginMetadata) {
    35  	var success bool
    36  
    37  	err := c.withClientDo(func(client *rpc.Client) error {
    38  		return client.Call("CliRpcCmd.SetPluginMetadata", metadata, &success)
    39  	})
    40  
    41  	if err != nil {
    42  		fmt.Println(err)
    43  		os.Exit(1)
    44  	}
    45  
    46  	if !success {
    47  		os.Exit(1)
    48  	}
    49  
    50  	os.Exit(0)
    51  }
    52  
    53  func (c *cliConnection) isMinCliVersion(version string) bool {
    54  	var result bool
    55  
    56  	err := c.withClientDo(func(client *rpc.Client) error {
    57  		return client.Call("CliRpcCmd.IsMinCliVersion", version, &result)
    58  	})
    59  
    60  	if err != nil {
    61  		fmt.Println(err)
    62  		os.Exit(1)
    63  	}
    64  
    65  	return result
    66  }
    67  
    68  func (c *cliConnection) CliCommandWithoutTerminalOutput(args ...string) ([]string, error) {
    69  	return c.callCliCommand(true, args...)
    70  }
    71  
    72  func (c *cliConnection) CliCommand(args ...string) ([]string, error) {
    73  	return c.callCliCommand(false, args...)
    74  }
    75  
    76  func (c *cliConnection) callCliCommand(silently bool, args ...string) ([]string, error) {
    77  	var (
    78  		success                  bool
    79  		cmdOutput                []string
    80  		callCoreCommandErr       error
    81  		getOutputAndResetErr     error
    82  		disableTerminalOutputErr error
    83  	)
    84  
    85  	_ = c.withClientDo(func(client *rpc.Client) error {
    86  		disableTerminalOutputErr = client.Call("CliRpcCmd.DisableTerminalOutput", silently, &success)
    87  		callCoreCommandErr = client.Call("CliRpcCmd.CallCoreCommand", args, &success)
    88  		getOutputAndResetErr = client.Call("CliRpcCmd.GetOutputAndReset", success, &cmdOutput)
    89  
    90  		return nil
    91  	})
    92  
    93  	if disableTerminalOutputErr != nil {
    94  		return []string{}, disableTerminalOutputErr
    95  	}
    96  
    97  	if callCoreCommandErr != nil {
    98  		return []string{}, callCoreCommandErr
    99  	}
   100  
   101  	if !success {
   102  		return []string{}, errors.New("Error executing cli core command")
   103  	}
   104  
   105  	if getOutputAndResetErr != nil {
   106  		return []string{}, errors.New("something completely unexpected happened")
   107  	}
   108  
   109  	return cmdOutput, nil
   110  }
   111  
   112  func (c *cliConnection) pingCLI() {
   113  	//call back to cf saying we have been setup
   114  	var connErr error
   115  	var conn net.Conn
   116  	for i := 0; i < 5; i++ {
   117  		conn, connErr = net.Dial("tcp", "127.0.0.1:"+c.cliServerPort)
   118  		if connErr != nil {
   119  			time.Sleep(200 * time.Millisecond)
   120  		} else {
   121  			conn.Close()
   122  			break
   123  		}
   124  	}
   125  	if connErr != nil {
   126  		fmt.Println(connErr)
   127  		os.Exit(1)
   128  	}
   129  }
   130  
   131  func (c *cliConnection) GetCurrentOrg() (plugin_models.Organization, error) {
   132  	var result plugin_models.Organization
   133  
   134  	err := c.withClientDo(func(client *rpc.Client) error {
   135  		return client.Call("CliRpcCmd.GetCurrentOrg", "", &result)
   136  	})
   137  
   138  	return result, err
   139  }
   140  
   141  func (c *cliConnection) GetCurrentSpace() (plugin_models.Space, error) {
   142  	var result plugin_models.Space
   143  
   144  	err := c.withClientDo(func(client *rpc.Client) error {
   145  		return client.Call("CliRpcCmd.GetCurrentSpace", "", &result)
   146  	})
   147  
   148  	return result, err
   149  }
   150  
   151  func (c *cliConnection) Username() (string, error) {
   152  	var result string
   153  
   154  	err := c.withClientDo(func(client *rpc.Client) error {
   155  		return client.Call("CliRpcCmd.Username", "", &result)
   156  	})
   157  
   158  	return result, err
   159  }
   160  
   161  func (c *cliConnection) UserGuid() (string, error) {
   162  	var result string
   163  
   164  	err := c.withClientDo(func(client *rpc.Client) error {
   165  		return client.Call("CliRpcCmd.UserGuid", "", &result)
   166  	})
   167  
   168  	return result, err
   169  }
   170  
   171  func (c *cliConnection) UserEmail() (string, error) {
   172  	var result string
   173  
   174  	err := c.withClientDo(func(client *rpc.Client) error {
   175  		return client.Call("CliRpcCmd.UserEmail", "", &result)
   176  	})
   177  
   178  	return result, err
   179  }
   180  
   181  func (c *cliConnection) IsSSLDisabled() (bool, error) {
   182  	var result bool
   183  
   184  	err := c.withClientDo(func(client *rpc.Client) error {
   185  		return client.Call("CliRpcCmd.IsSSLDisabled", "", &result)
   186  	})
   187  
   188  	return result, err
   189  }
   190  
   191  func (c *cliConnection) IsLoggedIn() (bool, error) {
   192  	var result bool
   193  
   194  	err := c.withClientDo(func(client *rpc.Client) error {
   195  		return client.Call("CliRpcCmd.IsLoggedIn", "", &result)
   196  	})
   197  
   198  	return result, err
   199  }
   200  
   201  func (c *cliConnection) HasOrganization() (bool, error) {
   202  	var result bool
   203  
   204  	err := c.withClientDo(func(client *rpc.Client) error {
   205  		return client.Call("CliRpcCmd.HasOrganization", "", &result)
   206  	})
   207  
   208  	return result, err
   209  }
   210  
   211  func (c *cliConnection) HasSpace() (bool, error) {
   212  	var result bool
   213  
   214  	err := c.withClientDo(func(client *rpc.Client) error {
   215  		return client.Call("CliRpcCmd.HasSpace", "", &result)
   216  	})
   217  
   218  	return result, err
   219  }
   220  
   221  func (c *cliConnection) ApiEndpoint() (string, error) {
   222  	var result string
   223  
   224  	err := c.withClientDo(func(client *rpc.Client) error {
   225  		return client.Call("CliRpcCmd.ApiEndpoint", "", &result)
   226  	})
   227  
   228  	return result, err
   229  }
   230  
   231  func (c *cliConnection) HasAPIEndpoint() (bool, error) {
   232  	var result bool
   233  
   234  	err := c.withClientDo(func(client *rpc.Client) error {
   235  		return client.Call("CliRpcCmd.HasAPIEndpoint", "", &result)
   236  	})
   237  
   238  	return result, err
   239  }
   240  
   241  func (c *cliConnection) ApiVersion() (string, error) {
   242  	var result string
   243  
   244  	err := c.withClientDo(func(client *rpc.Client) error {
   245  		return client.Call("CliRpcCmd.ApiVersion", "", &result)
   246  	})
   247  
   248  	return result, err
   249  }
   250  
   251  func (c *cliConnection) LoggregatorEndpoint() (string, error) {
   252  	var result string
   253  
   254  	err := c.withClientDo(func(client *rpc.Client) error {
   255  		return client.Call("CliRpcCmd.LoggregatorEndpoint", "", &result)
   256  	})
   257  
   258  	return result, err
   259  }
   260  
   261  func (c *cliConnection) DopplerEndpoint() (string, error) {
   262  	var result string
   263  
   264  	err := c.withClientDo(func(client *rpc.Client) error {
   265  		return client.Call("CliRpcCmd.DopplerEndpoint", "", &result)
   266  	})
   267  
   268  	return result, err
   269  }
   270  
   271  func (c *cliConnection) AccessToken() (string, error) {
   272  	var result string
   273  
   274  	err := c.withClientDo(func(client *rpc.Client) error {
   275  		return client.Call("CliRpcCmd.AccessToken", "", &result)
   276  	})
   277  
   278  	return result, err
   279  }
   280  
   281  func (c *cliConnection) GetApp(appName string) (plugin_models.GetAppModel, error) {
   282  	var result plugin_models.GetAppModel
   283  
   284  	err := c.withClientDo(func(client *rpc.Client) error {
   285  		return client.Call("CliRpcCmd.GetApp", appName, &result)
   286  	})
   287  
   288  	return result, err
   289  }
   290  
   291  func (c *cliConnection) GetApps() ([]plugin_models.GetAppsModel, error) {
   292  	var result []plugin_models.GetAppsModel
   293  
   294  	err := c.withClientDo(func(client *rpc.Client) error {
   295  		return client.Call("CliRpcCmd.GetApps", "", &result)
   296  	})
   297  
   298  	return result, err
   299  }
   300  
   301  func (c *cliConnection) GetOrgs() ([]plugin_models.GetOrgs_Model, error) {
   302  	var result []plugin_models.GetOrgs_Model
   303  
   304  	err := c.withClientDo(func(client *rpc.Client) error {
   305  		return client.Call("CliRpcCmd.GetOrgs", "", &result)
   306  	})
   307  
   308  	return result, err
   309  }
   310  
   311  func (c *cliConnection) GetSpaces() ([]plugin_models.GetSpaces_Model, error) {
   312  	var result []plugin_models.GetSpaces_Model
   313  
   314  	err := c.withClientDo(func(client *rpc.Client) error {
   315  		return client.Call("CliRpcCmd.GetSpaces", "", &result)
   316  	})
   317  
   318  	return result, err
   319  }
   320  
   321  func (c *cliConnection) GetServices() ([]plugin_models.GetServices_Model, error) {
   322  	var result []plugin_models.GetServices_Model
   323  
   324  	err := c.withClientDo(func(client *rpc.Client) error {
   325  		return client.Call("CliRpcCmd.GetServices", "", &result)
   326  	})
   327  
   328  	return result, err
   329  }
   330  
   331  func (c *cliConnection) GetOrgUsers(orgName string, args ...string) ([]plugin_models.GetOrgUsers_Model, error) {
   332  	var result []plugin_models.GetOrgUsers_Model
   333  
   334  	cmdArgs := append([]string{orgName}, args...)
   335  
   336  	err := c.withClientDo(func(client *rpc.Client) error {
   337  		return client.Call("CliRpcCmd.GetOrgUsers", cmdArgs, &result)
   338  	})
   339  
   340  	return result, err
   341  }
   342  
   343  func (c *cliConnection) GetSpaceUsers(orgName string, spaceName string) ([]plugin_models.GetSpaceUsers_Model, error) {
   344  	var result []plugin_models.GetSpaceUsers_Model
   345  
   346  	cmdArgs := []string{orgName, spaceName}
   347  
   348  	err := c.withClientDo(func(client *rpc.Client) error {
   349  		return client.Call("CliRpcCmd.GetSpaceUsers", cmdArgs, &result)
   350  	})
   351  
   352  	return result, err
   353  }
   354  
   355  func (c *cliConnection) GetOrg(orgName string) (plugin_models.GetOrg_Model, error) {
   356  	var result plugin_models.GetOrg_Model
   357  
   358  	err := c.withClientDo(func(client *rpc.Client) error {
   359  		return client.Call("CliRpcCmd.GetOrg", orgName, &result)
   360  	})
   361  
   362  	return result, err
   363  }
   364  
   365  func (c *cliConnection) GetSpace(spaceName string) (plugin_models.GetSpace_Model, error) {
   366  	var result plugin_models.GetSpace_Model
   367  
   368  	err := c.withClientDo(func(client *rpc.Client) error {
   369  		return client.Call("CliRpcCmd.GetSpace", spaceName, &result)
   370  	})
   371  
   372  	return result, err
   373  }
   374  
   375  func (c *cliConnection) GetService(serviceInstance string) (plugin_models.GetService_Model, error) {
   376  	var result plugin_models.GetService_Model
   377  
   378  	err := c.withClientDo(func(client *rpc.Client) error {
   379  		return client.Call("CliRpcCmd.GetService", serviceInstance, &result)
   380  	})
   381  
   382  	return result, err
   383  }