func (cmd *PluginUninstall) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { cmd.ui = deps.UI cmd.config = deps.PluginConfig //reset rpc registration in case there is other running instance, //each service can only be registered once server := rpc.NewServer() RPCService, err := rpcService.NewRpcService(deps.TeePrinter, deps.TeePrinter, deps.Config, deps.RepoLocator, rpcService.NewCommandRunner(), deps.Logger, cmd.ui.Writer(), server) if err != nil { cmd.ui.Failed("Error initializing RPC service: " + err.Error()) } cmd.rpcService = RPCService return cmd }
It("set the rpc command's Return Data", func() { var success bool err = client.Call("CliRpcCmd.SetPluginMetadata", metadata, &success) Expect(err).ToNot(HaveOccurred()) Expect(success).To(BeTrue()) Expect(rpcService.RpcCmd.PluginMetadata).To(Equal(metadata)) }) }) Describe(".GetOutputAndReset", func() { Context("success", func() { BeforeEach(func() { outputCapture := terminal.NewTeePrinter(os.Stdout) rpcService, err = NewRpcService(outputCapture, nil, nil, api.RepositoryLocator{}, cmdRunner.NewCommandRunner(), nil, nil, rpc.DefaultServer) Expect(err).ToNot(HaveOccurred()) err := rpcService.Start() Expect(err).ToNot(HaveOccurred()) pingCli(rpcService.Port()) }) AfterEach(func() { rpcService.Stop() //give time for server to stop time.Sleep(50 * time.Millisecond) })
func Main(traceEnv string, args []string) { //handle `cf -v` for cf version if len(args) == 2 && (args[1] == "-v" || args[1] == "--version") { args[1] = "version" } //handles `cf` if len(args) == 1 { args = []string{args[0], "help"} } //handles `cf [COMMAND] -h ...` //rearrange args to `cf help COMMAND` and let `command help` to print out usage args = append([]string{args[0]}, handleHelp(args[1:])...) newArgs, isVerbose := handleVerbose(args) args = newArgs errFunc := func(err error) { if err != nil { ui := terminal.NewUI( os.Stdin, Writer, terminal.NewTeePrinter(Writer), trace.NewLogger(Writer, isVerbose, traceEnv, ""), ) ui.Failed(fmt.Sprintf("Config error: %s", err)) } } // Only used to get Trace, so our errorHandler doesn't matter, since it's not used configPath, err := confighelpers.DefaultFilePath() if err != nil { errFunc(err) } config := coreconfig.NewRepositoryFromFilepath(configPath, errFunc) defer config.Close() traceConfigVal := config.Trace() // Writer is assigned in writer_unix.go/writer_windows.go traceLogger := trace.NewLogger(Writer, isVerbose, traceEnv, traceConfigVal) deps := commandregistry.NewDependency(Writer, traceLogger, os.Getenv("CF_DIAL_TIMEOUT")) defer handlePanics(args, deps.TeePrinter, deps.Logger) defer deps.Config.Close() //handle `cf --build` if len(args) == 2 && (args[1] == "--build" || args[1] == "-b") { deps.UI.Say(T("{{.CFName}} was built with Go version: {{.GoVersion}}", map[string]interface{}{ "CFName": args[0], "GoVersion": runtime.Version(), })) os.Exit(0) } warningProducers := []net.WarningProducer{} for _, warningProducer := range deps.Gateways { warningProducers = append(warningProducers, warningProducer) } warningsCollector := net.NewWarningsCollector(deps.UI, warningProducers...) commandsloader.Load() //run core command cmdName := args[1] cmd := cmdRegistry.FindCommand(cmdName) if cmd != nil { meta := cmd.MetaData() flagContext := flags.NewFlagContext(meta.Flags) flagContext.SkipFlagParsing(meta.SkipFlagParsing) cmdArgs := args[2:] err = flagContext.Parse(cmdArgs...) if err != nil { usage := cmdRegistry.CommandUsage(cmdName) deps.UI.Failed(T("Incorrect Usage") + "\n\n" + err.Error() + "\n\n" + usage) } cmd = cmd.SetDependency(deps, false) cmdRegistry.SetCommand(cmd) requirementsFactory := requirements.NewFactory(deps.Config, deps.RepoLocator) reqs := cmd.Requirements(requirementsFactory, flagContext) for _, req := range reqs { err = req.Execute() if err != nil { deps.UI.Failed(err.Error()) } } err = cmd.Execute(flagContext) if err != nil { ui := terminal.NewUI(os.Stdin, Writer, terminal.NewTeePrinter(Writer), traceLogger) ui.Failed(err.Error()) } warningsCollector.PrintWarnings() os.Exit(0) } //non core command, try plugin command server := netrpc.NewServer() rpcService, err := rpc.NewRpcService(deps.TeePrinter, deps.TeePrinter, deps.Config, deps.RepoLocator, rpc.NewCommandRunner(), deps.Logger, Writer, server) if err != nil { deps.UI.Say(T("Error initializing RPC service: ") + err.Error()) os.Exit(1) } pluginPath := filepath.Join(confighelpers.PluginRepoDir(), ".cf", "plugins") pluginConfig := pluginconfig.NewPluginConfig( func(err error) { deps.UI.Failed(fmt.Sprintf("Error read/writing plugin config: %s, ", err.Error())) }, configuration.NewDiskPersistor(filepath.Join(pluginPath, "config.json")), pluginPath, ) pluginList := pluginConfig.Plugins() ran := rpc.RunMethodIfExists(rpcService, args[1:], pluginList) if !ran { deps.UI.Say("'" + args[1] + T("' is not a registered command. See 'cf help'")) suggestCommands(cmdName, deps.UI, append(cmdRegistry.ListCommands(), pluginConfig.ListCommands()...)) os.Exit(1) } }
func (cmd *PluginInstall) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command { cmd.ui = deps.Ui cmd.config = deps.Config cmd.pluginConfig = deps.PluginConfig cmd.pluginRepo = deps.PluginRepo cmd.checksum = deps.ChecksumUtil //reset rpc registration in case there is other running instance, //each service can only be registered once rpc.DefaultServer = rpc.NewServer() rpcService, err := rpcService.NewRpcService(deps.TeePrinter, deps.TeePrinter, deps.Config, deps.RepoLocator, rpcService.NewCommandRunner(), deps.Logger) if err != nil { cmd.ui.Failed("Error initializing RPC service: " + err.Error()) } cmd.rpcService = rpcService return cmd }