Skip to content
Myr

Prefix bash functions with root command name (#643)

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

* Prefix bash functions with root command name Prior to this commit the autocomplete bash functions were being prefixed with the root command name, but references to those functions from subcommands were having the subcommands prefixed instead - causing the function lookups to fail and error out. For example (as observed in kubernetes/kubernetes#60517): kubectl create -f [Tab] failed with the following message: kubectl create -f __create_handle_filename_extension_flag: command not found in this case the function being invoked should be __kubectl_handle_filename_extension_flag Signed-off-by: John McCabe <john@johnmccabe.net> * Test filename extension and subdirs_in_dir for subcommands This commit adds two regex based tests to ensure that the handle filename extension and handle subdirs in dir functions are prefixed by the root command when present in subcommands. Previously they had been prefixed incorrectly with the subcommand name.

authored by John McCabe · committed by Eric Paris · parent a1e4933 · Browse files · 2 files+22 added−2 removed
This commit

State

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

Trailers

  • Signed-off-by: John McCabe <john@johnmccabe.net>

Diff stats

2 files+22 added−2 removed

  • other:+22 added−2 removed

2 files+22 added−2 removed

modified:bash_completions_test.go+20 added
3 unmodified lines
4 "bytes"5 "fmt"6 "os"7 "os/exec"8 "regexp"9 "strings"10 "testing"11)12
8 unmodified lines
21 t.Errorf("Expecting to contain: \n %q\nGot:\n %q\n", expected, found)22 }23}2425func checkRegex(t *testing.T, found, pattern string) {26 matched, err := regexp.MatchString(pattern, found)27 if err != nil {28 t.Errorf("Error thrown performing MatchString: \n %s\n", err)29 }30 if !matched {31 t.Errorf("Expecting to match: \n %q\nGot:\n %q\n", pattern, found)32 }33}3435func runShellCheck(s string) error {36 excluded := []string{37 "SC2034", // PREFIX appears unused. Verify it or export it.38 }
57 unmodified lines
96 Example: "Just run cobra-test echo",97 Run: emptyRun,98 }99100 echoCmd.Flags().String("filename", "", "Enter a filename")101 echoCmd.MarkFlagFilename("filename", "json", "yaml", "yml")102 echoCmd.Flags().String("config", "", "config to use (located in /config/PROFILE/)")103 echoCmd.Flags().SetAnnotation("config", BashCompSubdirsInDir, []string{"config"})104105 printCmd := &Command{106 Use: "print [string to print]",107 Args: MinimumNArgs(1),108 Short: "Print anything to the screen",
54 unmodified lines
163 // check for filename extension flags164 check(t, output, `must_have_one_noun+=("three")`)165 // check for filename extension flags166 check(t, output, fmt.Sprintf(`flags_completion+=("__%s_handle_filename_extension_flag json|yaml|yml")`, rootCmd.Name()))167 // check for filename extension flags in a subcommand168 checkRegex(t, output, fmt.Sprintf(`_root_echo\(\)\n{[^}]*flags_completion\+=\("__%s_handle_filename_extension_flag json\|yaml\|yml"\)`, rootCmd.Name()))169 // check for custom flags170 check(t, output, `flags_completion+=("__complete_custom")`)171 // check for subdirs_in_dir flags172 check(t, output, fmt.Sprintf(`flags_completion+=("__%s_handle_subdirs_in_dir_flag themes")`, rootCmd.Name()))173 // check for subdirs_in_dir flags in a subcommand174 checkRegex(t, output, fmt.Sprintf(`_root_echo\(\)\n{[^}]*flags_completion\+=\("__%s_handle_subdirs_in_dir_flag config"\)`, rootCmd.Name()))175176 checkOmit(t, output, deprecatedCmd.Name())177178 // If available, run shellcheck against the script.
39 unmodified lines
modified:bash_completions.go+2 added-2 removed
315 unmodified lines
316 buf.WriteString(fmt.Sprintf(" flags_with_completion+=(%q)\n", name))317318 var ext string319 if len(value) > 0 {320 ext = fmt.Sprintf("__%s_handle_filename_extension_flag ", cmd.Name()) + strings.Join(value, "|")320 ext = fmt.Sprintf("__%s_handle_filename_extension_flag ", cmd.Root().Name()) + strings.Join(value, "|")321 } else {322 ext = "_filedir"323 }324 buf.WriteString(fmt.Sprintf(" flags_completion+=(%q)\n", ext))
9 unmodified lines
334 buf.WriteString(fmt.Sprintf(" flags_with_completion+=(%q)\n", name))335336 var ext string337 if len(value) == 1 {338 ext = fmt.Sprintf("__%s_handle_subdirs_in_dir_flag ", cmd.Name()) + value[0]338 ext = fmt.Sprintf("__%s_handle_subdirs_in_dir_flag ", cmd.Root().Name()) + value[0]339 } else {340 ext = "_filedir -d"341 }342 buf.WriteString(fmt.Sprintf(" flags_completion+=(%q)\n", ext))
213 unmodified lines