github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/actors/v2actions/help.go (about) 1 package v2actions 2 3 import ( 4 "reflect" 5 "sort" 6 "strings" 7 8 "code.cloudfoundry.org/cli/utils/sortutils" 9 ) 10 11 // CommandInfo contains the help details of a command 12 type CommandInfo struct { 13 // Name is the command name 14 Name string 15 16 // Description is the command description 17 Description string 18 19 // Alias is the command alias 20 Alias string 21 22 // Usage is the command usage string, may contain examples and flavor text 23 Usage string 24 25 // RelatedCommands is a list of commands related to the command 26 RelatedCommands []string 27 28 // Flags contains the list of flags for this command 29 Flags []CommandFlag 30 } 31 32 // CommandFlag contains the help details of a command's flag 33 type CommandFlag struct { 34 // Short is the short form of the flag 35 Short string 36 37 // Long is the long form of the flag 38 Long string 39 40 // Description is the description of the flag 41 Description string 42 } 43 44 // CommandInfoByName returns the help information for a particular commandName in 45 // the commandList. 46 func (_ Actor) CommandInfoByName(commandList interface{}, commandName string) (CommandInfo, error) { 47 field, found := reflect.TypeOf(commandList).FieldByNameFunc( 48 func(fieldName string) bool { 49 field, _ := reflect.TypeOf(commandList).FieldByName(fieldName) 50 return field.Tag.Get("command") == commandName || field.Tag.Get("alias") == commandName 51 }, 52 ) 53 54 if !found { 55 return CommandInfo{}, ErrorInvalidCommand{CommandName: commandName} 56 } 57 58 tag := field.Tag 59 cmd := CommandInfo{ 60 Name: tag.Get("command"), 61 Description: tag.Get("description"), 62 Alias: tag.Get("alias"), 63 Flags: []CommandFlag{}, 64 } 65 66 command := field.Type 67 for i := 0; i < command.NumField(); i++ { 68 fieldTag := command.Field(i).Tag 69 70 if fieldTag.Get("hidden") != "" { 71 continue 72 } 73 74 if fieldTag.Get("usage") != "" { 75 cmd.Usage = fieldTag.Get("usage") 76 continue 77 } 78 79 if fieldTag.Get("related_commands") != "" { 80 relatedCommands := sortutils.Alphabetic(strings.Split(fieldTag.Get("related_commands"), ", ")) 81 sort.Sort(relatedCommands) 82 cmd.RelatedCommands = relatedCommands 83 continue 84 } 85 86 if fieldTag.Get("description") != "" { 87 cmd.Flags = append(cmd.Flags, CommandFlag{ 88 Short: fieldTag.Get("short"), 89 Long: fieldTag.Get("long"), 90 Description: fieldTag.Get("description"), 91 }) 92 } 93 } 94 95 return cmd, nil 96 } 97 98 // CommandInfos returns a slice of CommandInfo that only fills in 99 // the Name and Description for all the commands in commandList 100 func (_ Actor) CommandInfos(commandList interface{}) map[string]CommandInfo { 101 handler := reflect.TypeOf(commandList) 102 103 infos := make(map[string]CommandInfo, handler.NumField()) 104 for i := 0; i < handler.NumField(); i++ { 105 fieldTag := handler.Field(i).Tag 106 commandName := fieldTag.Get("command") 107 infos[commandName] = CommandInfo{ 108 Name: commandName, 109 Description: fieldTag.Get("description"), 110 Alias: fieldTag.Get("alias"), 111 } 112 } 113 114 return infos 115 }