Discover help routes through intermediate aliases
This commit is contained in:
62
help_view.go
62
help_view.go
@@ -16,13 +16,12 @@ func (a *App) helpView(route routeResult, snapshot *routingSnapshot) commandmeta
|
||||
if snapshot == nil {
|
||||
return view
|
||||
}
|
||||
view.CommandAliases = routeHelpAliases(snapshot, route)
|
||||
if view.Discovery {
|
||||
view.CommandAliases = discoveryHelpAliases(snapshot, route)
|
||||
view.FlagAliases = snapshot.discoveryFlags[route.discoveryPath.Key()].neutralRecords()
|
||||
return view
|
||||
}
|
||||
view.FlagAliases = snapshot.flagAliases[route.canonicalPath.Key()].neutralRecords()
|
||||
view.CommandAliases = routeHelpAliases(snapshot, route)
|
||||
return view
|
||||
}
|
||||
|
||||
@@ -30,9 +29,12 @@ func clonePath(path commandpath.Path) []string {
|
||||
return append([]string(nil), path...)
|
||||
}
|
||||
|
||||
func discoveryHelpAliases(snapshot *routingSnapshot, route routeResult) []commandmeta.CommandAlias {
|
||||
func routeHelpAliases(snapshot *routingSnapshot, route routeResult) []commandmeta.CommandAlias {
|
||||
if route.kind != routeDiscovery && len(route.canonicalPath) == 0 {
|
||||
return append([]commandmeta.CommandAlias(nil), snapshot.commandAliases...)
|
||||
}
|
||||
bySource := make(map[string]commandmeta.CommandAlias)
|
||||
for _, prefix := range []commandpath.Path{route.enteredPath, route.discoveryPath} {
|
||||
for _, prefix := range snapshot.aliases.ResolutionPaths(route.enteredPath) {
|
||||
for _, alias := range snapshot.commandAliases {
|
||||
source := commandpath.FromTokens(alias.Source)
|
||||
if len(source) <= len(prefix) || !source.HasPrefix(prefix) {
|
||||
@@ -58,55 +60,3 @@ func discoveryHelpAliases(snapshot *routingSnapshot, route routeResult) []comman
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func routeHelpAliases(snapshot *routingSnapshot, route routeResult) []commandmeta.CommandAlias {
|
||||
if len(route.canonicalPath) == 0 {
|
||||
return append([]commandmeta.CommandAlias(nil), snapshot.commandAliases...)
|
||||
}
|
||||
node := traceCanonicalPath(snapshot.canonical, route.canonicalPath).exact
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
entered := route.enteredPath
|
||||
canonical := route.canonicalPath
|
||||
bySource := make(map[string]commandmeta.CommandAlias)
|
||||
add := func(base commandpath.Path, includeOnlyWhenReached bool) {
|
||||
for _, alias := range snapshot.commandAliases {
|
||||
source := commandpath.FromTokens(alias.Source)
|
||||
if len(source) <= len(base) || !source.HasPrefix(base) {
|
||||
continue
|
||||
}
|
||||
suffix := source[len(base):]
|
||||
if includeOnlyWhenReached {
|
||||
enteredSource := append(entered.Clone(), suffix...)
|
||||
if !snapshot.aliases.Resolve(enteredSource).Equal(commandpath.FromTokens(alias.Target)) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
key := commandpath.FromTokens(suffix).Key()
|
||||
if _, exists := bySource[key]; exists {
|
||||
continue
|
||||
}
|
||||
bySource[key] = commandmeta.CommandAlias{
|
||||
Source: clonePath(suffix),
|
||||
Target: append([]string(nil), alias.Target...),
|
||||
}
|
||||
}
|
||||
}
|
||||
add(entered, false)
|
||||
if !entered.Equal(canonical) {
|
||||
add(canonical, true)
|
||||
}
|
||||
|
||||
keys := make([]string, 0, len(bySource))
|
||||
for key := range bySource {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
result := make([]commandmeta.CommandAlias, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
result = append(result, bySource[key])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user