github.com/arunkumar7540/cli@v6.45.0+incompatible/actor/sharedaction/help.go (about)

     1  package sharedaction
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/util/sorting"
    10  )
    11  
    12  // CommandInfo contains the help details of a command
    13  type CommandInfo struct {
    14  	// Name is the command name
    15  	Name string
    16  
    17  	// Description is the command description
    18  	Description string
    19  
    20  	// Alias is the command alias
    21  	Alias string
    22  
    23  	// Usage is the command usage string, may contain examples and flavor text
    24  	Usage string
    25  
    26  	// RelatedCommands is a list of commands related to the command
    27  	RelatedCommands []string
    28  
    29  	// Flags contains the list of flags for this command
    30  	Flags []CommandFlag
    31  
    32  	// Environment is a list of environment variables specific for this command
    33  	Environment []EnvironmentVariable
    34  }
    35  
    36  // CommandFlag contains the help details of a command's flag
    37  type CommandFlag struct {
    38  	// Short is the short form of the flag
    39  	Short string
    40  
    41  	// Long is the long form of the flag
    42  	Long string
    43  
    44  	// Description is the description of the flag
    45  	Description string
    46  
    47  	// Default is the flag's default value
    48  	Default string
    49  }
    50  
    51  // EnvironmentVariable contains env vars specific for this command
    52  type EnvironmentVariable struct {
    53  	Name         string
    54  	Description  string
    55  	DefaultValue string
    56  }
    57  
    58  // CommandInfoByName returns the help information for a particular commandName in
    59  // the commandList.
    60  func (Actor) CommandInfoByName(commandList interface{}, commandName string) (CommandInfo, error) {
    61  	field, found := reflect.TypeOf(commandList).FieldByNameFunc(
    62  		func(fieldName string) bool {
    63  			field, _ := reflect.TypeOf(commandList).FieldByName(fieldName)
    64  			return field.Tag.Get("command") == commandName || field.Tag.Get("alias") == commandName
    65  		},
    66  	)
    67  
    68  	if !found {
    69  		return CommandInfo{}, actionerror.InvalidCommandError{CommandName: commandName}
    70  	}
    71  
    72  	tag := field.Tag
    73  	cmd := CommandInfo{
    74  		Name:        tag.Get("command"),
    75  		Description: tag.Get("description"),
    76  		Alias:       tag.Get("alias"),
    77  		Flags:       []CommandFlag{},
    78  		Environment: []EnvironmentVariable{},
    79  	}
    80  
    81  	command := field.Type
    82  	for i := 0; i < command.NumField(); i++ {
    83  		fieldTag := command.Field(i).Tag
    84  
    85  		if fieldTag.Get("hidden") != "" {
    86  			continue
    87  		}
    88  
    89  		if fieldTag.Get("usage") != "" {
    90  			cmd.Usage = fieldTag.Get("usage")
    91  			continue
    92  		}
    93  
    94  		if fieldTag.Get("related_commands") != "" {
    95  			relatedCommands := strings.Split(fieldTag.Get("related_commands"), ", ")
    96  			sort.Slice(relatedCommands, sorting.SortAlphabeticFunc(relatedCommands))
    97  			cmd.RelatedCommands = relatedCommands
    98  			continue
    99  		}
   100  
   101  		if fieldTag.Get("description") != "" {
   102  			cmd.Flags = append(cmd.Flags, CommandFlag{
   103  				Short:       fieldTag.Get("short"),
   104  				Long:        fieldTag.Get("long"),
   105  				Description: fieldTag.Get("description"),
   106  				Default:     fieldTag.Get("default"),
   107  			})
   108  		}
   109  
   110  		if fieldTag.Get("environmentName") != "" {
   111  			cmd.Environment = append(cmd.Environment, EnvironmentVariable{
   112  				Name:         fieldTag.Get("environmentName"),
   113  				DefaultValue: fieldTag.Get("environmentDefault"),
   114  				Description:  fieldTag.Get("environmentDescription"),
   115  			})
   116  		}
   117  	}
   118  
   119  	return cmd, nil
   120  }
   121  
   122  // CommandInfos returns a slice of CommandInfo that only fills in
   123  // the Name and Description for all the commands in commandList
   124  func (Actor) CommandInfos(commandList interface{}) map[string]CommandInfo {
   125  	handler := reflect.TypeOf(commandList)
   126  
   127  	infos := make(map[string]CommandInfo, handler.NumField())
   128  	for i := 0; i < handler.NumField(); i++ {
   129  		fieldTag := handler.Field(i).Tag
   130  		commandName := fieldTag.Get("command")
   131  		infos[commandName] = CommandInfo{
   132  			Name:        commandName,
   133  			Description: fieldTag.Get("description"),
   134  			Alias:       fieldTag.Get("alias"),
   135  		}
   136  	}
   137  
   138  	return infos
   139  }