github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/help.go (about)

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/pluginconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/help"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/i18n"
    15  )
    16  
    17  type Help struct {
    18  	ui     terminal.UI
    19  	config pluginconfig.PluginConfiguration
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&Help{})
    24  }
    25  
    26  func (cmd *Help) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "help",
    29  		ShortName:   "h",
    30  		Description: T("Show help"),
    31  		Usage: []string{
    32  			T("CF_NAME help [COMMAND]"),
    33  		},
    34  	}
    35  }
    36  
    37  func (cmd *Help) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    38  	reqs := []requirements.Requirement{}
    39  	return reqs, nil
    40  }
    41  
    42  func (cmd *Help) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    43  	cmd.ui = deps.UI
    44  	cmd.config = deps.PluginConfig
    45  	return cmd
    46  }
    47  
    48  func (cmd *Help) Execute(c flags.FlagContext) error {
    49  	if len(c.Args()) == 0 {
    50  		help.ShowHelp(cmd.ui.Writer(), help.GetHelpTemplate())
    51  	} else {
    52  		cmdName := c.Args()[0]
    53  		if commandregistry.Commands.CommandExists(cmdName) {
    54  			cmd.ui.Say(commandregistry.Commands.CommandUsage(cmdName))
    55  		} else {
    56  			//check plugin commands
    57  			found := false
    58  			for _, meta := range cmd.config.Plugins() {
    59  				for _, c := range meta.Commands {
    60  					if c.Name == cmdName || c.Alias == cmdName {
    61  						output := T("NAME:") + "\n"
    62  						output += "   " + c.Name + " - " + c.HelpText + "\n"
    63  
    64  						if c.Alias != "" {
    65  							output += "\n" + T("ALIAS:") + "\n"
    66  							output += "   " + c.Alias + "\n"
    67  						}
    68  
    69  						output += "\n" + T("USAGE:") + "\n"
    70  						output += "   " + c.UsageDetails.Usage + "\n"
    71  
    72  						if len(c.UsageDetails.Options) > 0 {
    73  							output += "\n" + T("OPTIONS:") + "\n"
    74  
    75  							//find longest name length
    76  							l := 0
    77  							for n := range c.UsageDetails.Options {
    78  								if len(n) > l {
    79  									l = len(n)
    80  								}
    81  							}
    82  
    83  							for n, f := range c.UsageDetails.Options {
    84  								output += "   -" + n + strings.Repeat(" ", 7+(l-len(n))) + f + "\n"
    85  							}
    86  						}
    87  
    88  						cmd.ui.Say(output)
    89  
    90  						found = true
    91  					}
    92  				}
    93  			}
    94  
    95  			if !found {
    96  				return errors.New("'" + cmdName + "' is not a registered command. See 'cf help -a'")
    97  			}
    98  		}
    99  	}
   100  	return nil
   101  }