github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/help.go (about)

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