forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_plugin.go
41 lines (33 loc) · 862 Bytes
/
run_plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package rpc
import (
"os"
"os/exec"
"code.cloudfoundry.org/cli/cf/configuration/pluginconfig"
)
func RunMethodIfExists(rpcService *CliRpcService, args []string, pluginList map[string]pluginconfig.PluginMetadata) bool {
for _, metadata := range pluginList {
for _, command := range metadata.Commands {
if command.Name == args[0] || command.Alias == args[0] {
args[0] = command.Name
rpcService.Start()
defer rpcService.Stop()
pluginArgs := append([]string{rpcService.Port()}, args...)
cmd := exec.Command(metadata.Location, pluginArgs...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
defer stopPlugin(cmd)
err := cmd.Run()
if err != nil {
os.Exit(1)
}
return true
}
}
}
return false
}
func stopPlugin(plugin *exec.Cmd) {
plugin.Process.Kill()
plugin.Wait()
}