forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_plugin.go
59 lines (50 loc) · 1.52 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package rpc
import (
"os"
"os/exec"
"github.com/cloudfoundry/cli/cf/configuration/plugin_config"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
func RunMethodIfExists(coreCommandRunner *cli.App, args []string, outputCapture terminal.OutputCapture, terminalOutputSwitch terminal.TerminalOutputSwitch) bool {
pluginsConfig := plugin_config.NewPluginConfig(func(err error) { panic(err) })
pluginList := pluginsConfig.Plugins()
for _, metadata := range pluginList {
for _, command := range metadata.Commands {
if command.Name == args[0] || command.Alias == args[0] {
args[0] = command.Name
cliServer, err := startCliServer(coreCommandRunner, outputCapture, terminalOutputSwitch)
if err != nil {
os.Exit(1)
}
defer cliServer.Stop()
pluginArgs := append([]string{cliServer.Port()}, args...)
cmd := exec.Command(metadata.Location, pluginArgs...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
defer stopPlugin(cmd)
err = cmd.Run()
if err != nil {
os.Exit(1)
}
return true
}
}
}
return false
}
func startCliServer(coreCommandRunner *cli.App, outputCapture terminal.OutputCapture, terminalOutputSwitch terminal.TerminalOutputSwitch) (*CliRpcService, error) {
cliServer, err := NewRpcService(coreCommandRunner, outputCapture, terminalOutputSwitch)
if err != nil {
return nil, err
}
err = cliServer.Start()
if err != nil {
return nil, err
}
return cliServer, nil
}
func stopPlugin(plugin *exec.Cmd) {
plugin.Process.Kill()
plugin.Wait()
}