github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/plugin/rpc/call_command_registry.go (about)

     1  package rpc
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	"code.cloudfoundry.org/cli/cf/requirements"
     9  )
    10  
    11  //go:generate counterfeiter . CommandRunner
    12  
    13  type CommandRunner interface {
    14  	Command([]string, commandregistry.Dependency, bool) error
    15  }
    16  
    17  type commandRunner struct{}
    18  
    19  func NewCommandRunner() CommandRunner {
    20  	return &commandRunner{}
    21  }
    22  
    23  func (c *commandRunner) Command(args []string, deps commandregistry.Dependency, pluginApiCall bool) (err error) {
    24  	cmdRegistry := commandregistry.Commands
    25  
    26  	if cmdRegistry.CommandExists(args[0]) {
    27  		fc := flags.NewFlagContext(cmdRegistry.FindCommand(args[0]).MetaData().Flags)
    28  		err = fc.Parse(args[1:]...)
    29  		if err != nil {
    30  			return err
    31  		}
    32  
    33  		cfCmd := cmdRegistry.FindCommand(args[0])
    34  		cfCmd = cfCmd.SetDependency(deps, pluginApiCall)
    35  
    36  		reqs, reqErr := cfCmd.Requirements(requirements.NewFactory(deps.Config, deps.RepoLocator), fc)
    37  		if reqErr != nil {
    38  			return reqErr
    39  		}
    40  
    41  		for _, r := range reqs {
    42  			if err = r.Execute(); err != nil {
    43  				return err
    44  			}
    45  		}
    46  
    47  		defer func() {
    48  			if r := recover(); r != nil {
    49  				err = fmt.Errorf("command panic: %v", r)
    50  			}
    51  		}()
    52  
    53  		return cfCmd.Execute(fc)
    54  	}
    55  
    56  	return nil
    57  }