github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/plugin/rpc/run_plugin.go (about)

     1  package rpc
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  
     7  	"github.com/liamawhite/cli-with-i18n/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  				cmd.Stderr = os.Stderr
    25  
    26  				defer stopPlugin(cmd)
    27  				err := cmd.Run()
    28  				if err != nil {
    29  					os.Exit(1)
    30  				}
    31  				return true
    32  			}
    33  		}
    34  	}
    35  	return false
    36  }
    37  
    38  func stopPlugin(plugin *exec.Cmd) {
    39  	plugin.Process.Kill()
    40  	plugin.Wait()
    41  }