MCPcopy Index your code
hub / github.com/cloudfoundry/cli / CommandInfoByName

Method CommandInfoByName

actor/sharedaction/help.go:90–181  ·  view source on GitHub ↗

CommandInfoByName returns the help information for a particular commandName in the commandList.

(commandList interface{}, commandName string)

Source from the content-addressed store, hash-verified

88// CommandInfoByName returns the help information for a particular commandName in
89// the commandList.
90func (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

Callers

nothing calls this directly

Implementers 1

FakeActorcommand/v7/v7fakes/fake_actor.go

Calls 5

SortAlphabeticFuncFunction · 0.92
GetMethod · 0.65
UsageMethod · 0.65
ExamplesMethod · 0.65
ResourcesMethod · 0.65

Tested by

no test coverage detected