CommandInfoByName returns the help information for a particular commandName in the commandList.
(commandList interface{}, commandName string)
| 88 | // CommandInfoByName returns the help information for a particular commandName in |
| 89 | // the commandList. |
| 90 | func (Actor) CommandInfoByName(commandList interface{}, commandName string) (CommandInfo, error) { |
| 91 | field, found := reflect.TypeOf(commandList).FieldByNameFunc( |
| 92 | func(fieldName string) bool { |
| 93 | field, _ := reflect.TypeOf(commandList).FieldByName(fieldName) |
| 94 | return field.Tag.Get("command") == commandName || field.Tag.Get("alias") == commandName |
| 95 | }, |
| 96 | ) |
| 97 | |
| 98 | if !found { |
| 99 | return CommandInfo{}, actionerror.InvalidCommandError{CommandName: commandName} |
| 100 | } |
| 101 | |
| 102 | tag := field.Tag |
| 103 | cmd := CommandInfo{ |
| 104 | Name: tag.Get("command"), |
| 105 | Description: tag.Get("description"), |
| 106 | Alias: tag.Get("alias"), |
| 107 | Flags: []CommandFlag{}, |
| 108 | Environment: []EnvironmentVariable{}, |
| 109 | } |
| 110 | |
| 111 | fieldValue := reflect.ValueOf(commandList).FieldByIndex(field.Index) |
| 112 | |
| 113 | if commandWithUsage, hasUsage := fieldValue.Interface().(HasUsage); hasUsage { |
| 114 | cmd.Usage = strings.ReplaceAll( |
| 115 | strings.TrimSpace(commandWithUsage.Usage()), |
| 116 | "\n", |
| 117 | "\n"+CommandIndent, |
| 118 | ) |
| 119 | } |
| 120 | |
| 121 | if commandWithExamples, hasExamples := fieldValue.Interface().(HasExamples); hasExamples { |
| 122 | cmd.Examples = strings.ReplaceAll( |
| 123 | strings.TrimSpace(commandWithExamples.Examples()), |
| 124 | "\n", |
| 125 | "\n"+CommandIndent, |
| 126 | ) |
| 127 | } |
| 128 | |
| 129 | if commandWithResources, hasResources := fieldValue.Interface().(HasResources); hasResources { |
| 130 | cmd.Resources = strings.ReplaceAll( |
| 131 | strings.TrimSpace(commandWithResources.Resources()), |
| 132 | "\n", |
| 133 | "\n"+CommandIndent, |
| 134 | ) |
| 135 | } |
| 136 | |
| 137 | command := field.Type |
| 138 | for i := 0; i < command.NumField(); i++ { |
| 139 | fieldTag := command.Field(i).Tag |
| 140 | |
| 141 | if fieldTag.Get("hidden") != "" { |
| 142 | continue |
| 143 | } |
| 144 | |
| 145 | if cmd.Usage == "" && fieldTag.Get("usage") != "" { |
| 146 | cmd.Usage = fieldTag.Get("usage") |
| 147 | continue |