Share invocation flag lookup across installation contexts
This commit is contained in:
@@ -24,9 +24,10 @@ type flagAliasMapping struct {
|
||||
}
|
||||
|
||||
type flagAliasContext struct {
|
||||
path commandpath.Path
|
||||
canonical map[string]capturedFlagSpelling
|
||||
discovery bool
|
||||
path commandpath.Path
|
||||
canonical map[string]capturedFlagSpelling
|
||||
invocation map[string]capturedFlagSpelling
|
||||
discovery bool
|
||||
}
|
||||
|
||||
func (aliases resolvedFlagAliases) neutralRecords() []commandmeta.FlagAlias {
|
||||
@@ -64,6 +65,24 @@ func (spelling capturedFlagSpelling) token() string {
|
||||
return "--" + spelling.name
|
||||
}
|
||||
|
||||
func (context flagAliasContext) canonicalSpelling(key string) (capturedFlagSpelling, bool) {
|
||||
if value, ok := context.canonical[key]; ok {
|
||||
return value, true
|
||||
}
|
||||
value, ok := context.invocation[key]
|
||||
return value, ok
|
||||
}
|
||||
|
||||
func invocationFlagSpellings(flags []commandmeta.Flag) map[string]capturedFlagSpelling {
|
||||
result := make(map[string]capturedFlagSpelling, len(flags))
|
||||
for _, flag := range flags {
|
||||
for _, spelling := range featureSpellings(flag) {
|
||||
result[flagSpellingKey(spelling)] = spelling
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func compileFlagAliases(rules []RoutingRule, roots map[string]*canonicalNode, aliases interface {
|
||||
Resolve(commandpath.Path) commandpath.Path
|
||||
ProperSourcePrefixes() []commandpath.Path
|
||||
@@ -97,13 +116,11 @@ func compileFlagAliases(rules []RoutingRule, roots map[string]*canonicalNode, al
|
||||
canonicalContexts[key] = flagAliasContext{path: node.path.Clone(), canonical: canonicalFlagSpellings(node.command)}
|
||||
}
|
||||
discoveryContexts := discoverFlagContexts(rules, roots, aliases, canonicalNodes)
|
||||
shared := invocationFlagSpellings(features)
|
||||
for _, contexts := range []map[string]flagAliasContext{canonicalContexts, discoveryContexts} {
|
||||
for _, context := range contexts {
|
||||
for _, flag := range features {
|
||||
for _, spelling := range featureSpellings(flag) {
|
||||
context.canonical[flagSpellingKey(spelling)] = spelling
|
||||
}
|
||||
}
|
||||
for key, context := range contexts {
|
||||
context.invocation = shared
|
||||
contexts[key] = context
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,7 +262,7 @@ func resolveFlagAliases(context flagAliasContext, mappings map[string]flagAliasM
|
||||
var resolve func(capturedFlagSpelling) (terminal, error)
|
||||
resolve = func(current capturedFlagSpelling) (terminal, error) {
|
||||
key := flagSpellingKey(current)
|
||||
if endpoint, exists := context.canonical[key]; exists {
|
||||
if endpoint, exists := context.canonicalSpelling(key); exists {
|
||||
return terminal{spelling: endpoint, active: true}, nil
|
||||
}
|
||||
if result, exists := resolved[key]; exists {
|
||||
@@ -288,7 +305,7 @@ func resolveFlagAliases(context flagAliasContext, mappings map[string]flagAliasM
|
||||
}
|
||||
continue
|
||||
}
|
||||
if _, collision := context.canonical[sourceKey]; collision {
|
||||
if _, collision := context.canonicalSpelling(sourceKey); collision {
|
||||
return resolvedFlagAliases{}, fmt.Errorf("flag alias %s conflicts with a canonical spelling in %s", source.token(), flagContextDescription(context))
|
||||
}
|
||||
result.bySpelling[sourceKey] = resolvedFlagAlias{source: source, target: terminal.spelling}
|
||||
|
||||
Reference in New Issue
Block a user