github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/plugin/rpc/run_plugin.go (about)

     1  package rpc
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  
     7  	"github.com/cloudfoundry/cli/cf/configuration/pluginconfig"
     8  )
     9  
    10  func RunMethodIfExists(rpcService *CliRpcService, args []string, pluginList map[string]pluginconfig.PluginMetadata) bool {
    11  	for _, metadata := range pluginList {
    12  		for _, command := range metadata.Commands {
    13  			if command.Name == args[0] || command.Alias == args[0] {
    14  				args[0] = command.Name
    15  
    16  				rpcService.Start()
    17  				defer rpcService.Stop()
    18  
    19  				pluginArgs := append([]string{rpcService.Port()}, args...)
    20  
    21  				cmd := exec.Command(metadata.Location, pluginArgs...)
    22  				cmd.Stdout = os.Stdout
    23  				cmd.Stdin = os.Stdin
    24  
    25  				defer stopPlugin(cmd)
    26  				err := cmd.Run()
    27  				if err != nil {
    28  					os.Exit(1)
    29  				}
    30  				return true
    31  			}
    32  		}
    33  	}
    34  	return false
    35  }
    36  
    37  func stopPlugin(plugin *exec.Cmd) {
    38  	plugin.Process.Kill()
    39  	plugin.Wait()
    40  }