Skip to content
Myr

fix RegisterFlagCompletionFunc concurrent map writes error (#1423)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix-RegisterFlagCompletionFunc-concurrent * set to root command * move to non-public fields

authored by silenceshell · committed by GitHub · parent 2dea4f2 · Browse files · 3 files+12 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+12 added−7 removed

  • other:+12 added−7 removed

3 files+12 added−7 removed

modified:bash_completions.go+1 added-1 removed
510 unmodified lines
511}512513// Setup annotations for go completions for registered flags514func prepareCustomAnnotationsForFlags(cmd *Command) {515 for flag := range flagCompletionFunctions {515 for flag := range cmd.Root().flagCompletionFunctions {516 // Make sure the completion script calls the __*_go_custom_completion function for517 // every registered flag. We need to do this here (and not when the flag was registered518 // for completion) so that we can know the root command name for the prefix519 // of __<prefix>_go_custom_completion
164 unmodified lines
modified:command.go+3 added
140 unmodified lines
141 // globNormFunc is the global normalization function142 // that we can use on every pflag set and children commands143 globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName144145 //flagCompletionFunctions is map of flag completion functions.146 flagCompletionFunctions map[*flag.Flag]func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)147148 // usageFunc is usage func defined by user.149 usageFunc func(*Command) error150 // usageTemplate is usage template defined by user.151 usageTemplate string
1532 unmodified lines
modified:completions.go+8 added-6 removed
15 unmodified lines
16 // completion results without their description. It is used by the shell completion scripts.17 ShellCompNoDescRequestCmd = "__completeNoDesc"18)1920// Global map of flag completion functions.21var flagCompletionFunctions = map[*pflag.Flag]func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective){}2220// ShellCompDirective is a bit map representing the different behaviors the shell21// can be instructed to have once completions have been provided.22type ShellCompDirective int23
66 unmodified lines
90 flag := c.Flag(flagName)91 if flag == nil {92 return fmt.Errorf("RegisterFlagCompletionFunc: flag '%s' does not exist", flagName)93 }9495 root := c.Root()97 if _, exists := flagCompletionFunctions[flag]; exists {96 if _, exists := root.flagCompletionFunctions[flag]; exists {97 return fmt.Errorf("RegisterFlagCompletionFunc: flag '%s' already registered", flagName)98 }99 if root.flagCompletionFunctions == nil {100 root.flagCompletionFunctions = map[*pflag.Flag]func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective){}101 }100 flagCompletionFunctions[flag] = f102 root.flagCompletionFunctions[flag] = f103 return nil104}105106// Returns a string listing the different directive enabled in the specified parameter
268 unmodified lines
375376 // Find the completion function for the flag or command377 var completionFn func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)378 if flag != nil {377 completionFn = flagCompletionFunctions[flag]379 completionFn = c.Root().flagCompletionFunctions[flag]380 } else {381 completionFn = finalCmd.ValidArgsFunction382 }383 if completionFn != nil {
362 unmodified lines