github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/util/plugin/plugin.go (about)

     1  package plugin
     2  
     3  import (
     4  	"os"
     5  
     6  	"errors"
     7  	"fmt"
     8  
     9  	plugin_transition "code.cloudfoundry.org/cli/plugin/transition"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  
    13  	"code.cloudfoundry.org/cli/command/common"
    14  	"code.cloudfoundry.org/cli/command/translatableerror"
    15  )
    16  
    17  var ErrFailed = errors.New("command failed")
    18  var ParseErr = errors.New("incorrect type for arg")
    19  
    20  type DisplayUsage interface {
    21  	DisplayUsage()
    22  }
    23  
    24  type UI interface {
    25  	DisplayError(err error)
    26  	DisplayWarning(template string, templateValues ...map[string]interface{})
    27  	DisplayText(template string, templateValues ...map[string]interface{})
    28  	FlushDeferred()
    29  }
    30  
    31  func IsPluginCommand(osArgs []string) (configv3.Plugin, bool) {
    32  	if len(osArgs) < 1 {
    33  		return configv3.Plugin{}, false
    34  	}
    35  	command := osArgs[0]
    36  	config, configErr := configv3.LoadConfig()
    37  	if configErr != nil {
    38  		fmt.Fprintf(os.Stderr, "Empty Config, failed to load plugins")
    39  		return configv3.Plugin{}, false
    40  	}
    41  	for _, plugin := range config.Plugins() {
    42  		for _, pluginCommand := range plugin.Commands {
    43  			if command == pluginCommand.Name || command == pluginCommand.Alias {
    44  				return plugin, true
    45  			}
    46  		}
    47  	}
    48  	return configv3.Plugin{}, false
    49  }
    50  
    51  func PluginCommandNames() []string {
    52  	var names []string
    53  
    54  	config, configErr := configv3.LoadConfig()
    55  	if configErr != nil {
    56  		return names
    57  	}
    58  
    59  	for _, plugin := range config.Plugins() {
    60  		for _, pluginCommand := range plugin.Commands {
    61  			names = append(names, pluginCommand.Name)
    62  		}
    63  	}
    64  
    65  	return names
    66  }
    67  
    68  func RunPlugin(plugin configv3.Plugin) error {
    69  	_, commandUI, err := getCFConfigAndCommandUIObjects()
    70  	if err != nil {
    71  		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
    72  		return err
    73  	}
    74  	defer commandUI.FlushDeferred()
    75  	pluginErr := plugin_transition.RunPlugin(plugin, commandUI)
    76  	if pluginErr != nil {
    77  		return handleError(pluginErr, commandUI)
    78  	}
    79  	return nil
    80  }
    81  
    82  func getCFConfigAndCommandUIObjects() (*configv3.Config, *ui.UI, error) {
    83  	cfConfig, configErr := configv3.LoadConfig(configv3.FlagOverride{
    84  		Verbose: common.Commands.VerboseOrVersion,
    85  	})
    86  	if configErr != nil {
    87  		if _, ok := configErr.(translatableerror.EmptyConfigError); !ok {
    88  			return nil, nil, configErr
    89  		}
    90  	}
    91  	commandUI, err := ui.NewUI(cfConfig)
    92  	return cfConfig, commandUI, err
    93  }
    94  
    95  func handleError(passedErr error, commandUI UI) error {
    96  	if passedErr == nil {
    97  		return nil
    98  	}
    99  
   100  	translatedErr := translatableerror.ConvertToTranslatableError(passedErr)
   101  	commandUI.DisplayError(translatedErr)
   102  
   103  	if _, ok := translatedErr.(DisplayUsage); ok {
   104  		return ParseErr
   105  	}
   106  
   107  	return ErrFailed
   108  }