From 964c294e0b50cf885a9551e79ab2d7632f2d284e Mon Sep 17 00:00:00 2001 From: umarcor Date: Mon, 13 Apr 2020 18:44:20 +0200 Subject: [PATCH] fix after rebasing --- bash_completions.go | 10 ++++----- cobra/cmd/init_test.go | 2 +- command_test.go | 2 +- custom_completions.go | 4 ++-- custom_completions_test.go | 46 +++++++++++++++++++------------------- fish_completions.go | 6 ++--- fish_completions_test.go | 8 +++---- zsh_completions.go | 4 ++-- 8 files changed, 41 insertions(+), 41 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index 0ecee42ff..193f0576c 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -521,8 +521,8 @@ func prepareCustomAnnotationsForFlags(cmd *Command) { } func writeFlags(buf io.StringWriter, cmd *Command) { - prepareCustomAnnotationsForFlags(cmd) - WrStringAndCheck(buf, ` flags=() + prepareCustomAnnotationsForFlags(cmd) + WrStringAndCheck(buf, ` flags=() two_word_flags=() local_nonpersistent_flags=() flags_with_completion=() @@ -586,11 +586,11 @@ func writeRequiredNouns(buf io.StringWriter, cmd *Command) { for _, value := range cmd.ValidArgs { // Remove any description that may be included following a tab character. // Descriptions are not supported by bash completion. - value = strings.Split(value, "\t")[0] - WrStringAndCheck(buf, fmt.Sprintf(" must_have_one_noun+=(%q)\n", value)) + value = strings.Split(value, "\t")[0] + WrStringAndCheck(buf, fmt.Sprintf(" must_have_one_noun+=(%q)\n", value)) } if cmd.ValidArgsFunction != nil { - buf.WriteString(" has_completion_function=1\n") + WrStringAndCheck(buf, " has_completion_function=1\n") } } diff --git a/cobra/cmd/init_test.go b/cobra/cmd/init_test.go index c4b3f09a2..282fcd728 100644 --- a/cobra/cmd/init_test.go +++ b/cobra/cmd/init_test.go @@ -59,7 +59,7 @@ func TestGoldenInitCmd(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - initCmd.Flags().Set("pkg-name", tt.pkgName) + er(initCmd.Flags().Set("pkg-name", tt.pkgName)) viper.Set("useViper", true) projectPath, err := initializeProject(tt.args) defer func() { diff --git a/command_test.go b/command_test.go index d6cb850e9..3873e5737 100644 --- a/command_test.go +++ b/command_test.go @@ -783,7 +783,7 @@ func TestPersistentRequiredFlagsWithDisableFlagParsing(t *testing.T) { parent := &Command{Use: "parent", Run: emptyRun} parent.PersistentFlags().Bool("foo", false, "") flag := parent.PersistentFlags().Lookup("foo") - parent.MarkPersistentFlagRequired("foo") + er(parent.MarkPersistentFlagRequired("foo")) child := &Command{Use: "child", Run: emptyRun} child.DisableFlagParsing = true diff --git a/custom_completions.go b/custom_completions.go index c25c03e40..c2194697d 100644 --- a/custom_completions.go +++ b/custom_completions.go @@ -509,13 +509,13 @@ func CompDebug(msg string, printToStdErr bool) { os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err == nil { defer f.Close() - f.WriteString(msg) + WrStringAndCheck(f, msg) } } if printToStdErr { // Must print to stderr for this not to be read by the completion script. - fmt.Fprintf(os.Stderr, msg) + fmt.Fprint(os.Stderr, msg) } } diff --git a/custom_completions_test.go b/custom_completions_test.go index 276b8a77b..65b76e740 100644 --- a/custom_completions_test.go +++ b/custom_completions_test.go @@ -720,17 +720,17 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { rootCmd.AddCommand(childCmd) rootCmd.Flags().IntP("requiredFlag", "r", -1, "required flag") - rootCmd.MarkFlagRequired("requiredFlag") + er(rootCmd.MarkFlagRequired("requiredFlag")) requiredFlag := rootCmd.Flags().Lookup("requiredFlag") rootCmd.PersistentFlags().IntP("requiredPersistent", "p", -1, "required persistent") - rootCmd.MarkPersistentFlagRequired("requiredPersistent") + er(rootCmd.MarkPersistentFlagRequired("requiredPersistent")) requiredPersistent := rootCmd.PersistentFlags().Lookup("requiredPersistent") rootCmd.Flags().StringP("release", "R", "", "Release name") childCmd.Flags().BoolP("subRequired", "s", false, "sub required flag") - childCmd.MarkFlagRequired("subRequired") + er(childCmd.MarkFlagRequired("subRequired")) childCmd.Flags().BoolP("subNotRequired", "n", false, "sub not required flag") // Test that a required flag is suggested even without the - prefix @@ -904,19 +904,19 @@ func TestFlagFileExtFilterCompletionInGo(t *testing.T) { // No extensions. Should be ignored. rootCmd.Flags().StringP("file", "f", "", "file flag") - rootCmd.MarkFlagFilename("file") + er(rootCmd.MarkFlagFilename("file")) // Single extension rootCmd.Flags().StringP("log", "l", "", "log flag") - rootCmd.MarkFlagFilename("log", "log") + er(rootCmd.MarkFlagFilename("log", "log")) // Multiple extensions rootCmd.Flags().StringP("yaml", "y", "", "yaml flag") - rootCmd.MarkFlagFilename("yaml", "yaml", "yml") + er(rootCmd.MarkFlagFilename("yaml", "yaml", "yml")) // Directly using annotation rootCmd.Flags().StringP("text", "t", "", "text flag") - rootCmd.Flags().SetAnnotation("text", BashCompFilenameExt, []string{"txt"}) + er(rootCmd.Flags().SetAnnotation("text", BashCompFilenameExt, []string{"txt"})) // Test that the completion logic returns the proper info for the completion // script to handle the file filtering @@ -1026,15 +1026,15 @@ func TestFlagDirFilterCompletionInGo(t *testing.T) { // Filter directories rootCmd.Flags().StringP("dir", "d", "", "dir flag") - rootCmd.MarkFlagDirname("dir") + er(rootCmd.MarkFlagDirname("dir")) // Filter directories within a directory rootCmd.Flags().StringP("subdir", "s", "", "subdir") - rootCmd.Flags().SetAnnotation("subdir", BashCompSubdirsInDir, []string{"themes"}) + er(rootCmd.Flags().SetAnnotation("subdir", BashCompSubdirsInDir, []string{"themes"})) // Multiple directory specification get ignored rootCmd.Flags().StringP("manydir", "m", "", "manydir") - rootCmd.Flags().SetAnnotation("manydir", BashCompSubdirsInDir, []string{"themes", "colors"}) + er(rootCmd.Flags().SetAnnotation("manydir", BashCompSubdirsInDir, []string{"themes", "colors"})) // Test that the completion logic returns the proper info for the completion // script to handle the directory filtering @@ -1370,7 +1370,7 @@ func TestValidArgsFuncInBashScript(t *testing.T) { rootCmd.AddCommand(child) buf := new(bytes.Buffer) - rootCmd.GenBashCompletion(buf) + er(rootCmd.GenBashCompletion(buf)) output := buf.String() check(t, output, "has_completion_function=1") @@ -1385,7 +1385,7 @@ func TestNoValidArgsFuncInBashScript(t *testing.T) { rootCmd.AddCommand(child) buf := new(bytes.Buffer) - rootCmd.GenBashCompletion(buf) + er(rootCmd.GenBashCompletion(buf)) output := buf.String() checkOmit(t, output, "has_completion_function=1") @@ -1401,7 +1401,7 @@ func TestCompleteCmdInBashScript(t *testing.T) { rootCmd.AddCommand(child) buf := new(bytes.Buffer) - rootCmd.GenBashCompletion(buf) + er(rootCmd.GenBashCompletion(buf)) output := buf.String() check(t, output, ShellCompNoDescRequestCmd) @@ -1417,7 +1417,7 @@ func TestCompleteNoDesCmdInZshScript(t *testing.T) { rootCmd.AddCommand(child) buf := new(bytes.Buffer) - rootCmd.GenZshCompletionNoDesc(buf) + er(rootCmd.GenZshCompletionNoDesc(buf)) output := buf.String() check(t, output, ShellCompNoDescRequestCmd) @@ -1433,7 +1433,7 @@ func TestCompleteCmdInZshScript(t *testing.T) { rootCmd.AddCommand(child) buf := new(bytes.Buffer) - rootCmd.GenZshCompletion(buf) + er(rootCmd.GenZshCompletion(buf)) output := buf.String() check(t, output, ShellCompRequestCmd) @@ -1446,7 +1446,7 @@ func TestFlagCompletionInGo(t *testing.T) { Run: emptyRun, } rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot") - rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + er(rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { completions := []string{} for _, comp := range []string{"1\tThe first", "2\tThe second", "10\tThe tenth"} { if strings.HasPrefix(comp, toComplete) { @@ -1454,9 +1454,9 @@ func TestFlagCompletionInGo(t *testing.T) { } } return completions, ShellCompDirectiveDefault - }) + })) rootCmd.Flags().String("filename", "", "Enter a filename") - rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + er(rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { completions := []string{} for _, comp := range []string{"file.yaml\tYAML format", "myfile.json\tJSON format", "file.xml\tXML format"} { if strings.HasPrefix(comp, toComplete) { @@ -1464,7 +1464,7 @@ func TestFlagCompletionInGo(t *testing.T) { } } return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp - }) + })) // Test completing an empty string output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--introot", "") @@ -1643,7 +1643,7 @@ func TestFlagCompletionInGoWithDesc(t *testing.T) { Run: emptyRun, } rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot") - rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + er(rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { completions := []string{} for _, comp := range []string{"1\tThe first", "2\tThe second", "10\tThe tenth"} { if strings.HasPrefix(comp, toComplete) { @@ -1651,9 +1651,9 @@ func TestFlagCompletionInGoWithDesc(t *testing.T) { } } return completions, ShellCompDirectiveDefault - }) + })) rootCmd.Flags().String("filename", "", "Enter a filename") - rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + er(rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { completions := []string{} for _, comp := range []string{"file.yaml\tYAML format", "myfile.json\tJSON format", "file.xml\tXML format"} { if strings.HasPrefix(comp, toComplete) { @@ -1661,7 +1661,7 @@ func TestFlagCompletionInGoWithDesc(t *testing.T) { } } return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp - }) + })) // Test completing an empty string output, err := executeCommand(rootCmd, ShellCompRequestCmd, "--introot", "") diff --git a/fish_completions.go b/fish_completions.go index 66a6357cb..17a3c3991 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -8,7 +8,7 @@ import ( "strings" ) -func genFishComp(buf *bytes.Buffer, name string, includeDesc bool) { +func genFishComp(buf io.StringWriter, name string, includeDesc bool) { // Variables should not contain a '-' or ':' character nameForVar := name nameForVar = strings.Replace(nameForVar, "-", "_", -1) @@ -18,8 +18,8 @@ func genFishComp(buf *bytes.Buffer, name string, includeDesc bool) { if !includeDesc { compCmd = ShellCompNoDescRequestCmd } - buf.WriteString(fmt.Sprintf("# fish completion for %-36s -*- shell-script -*-\n", name)) - buf.WriteString(fmt.Sprintf(` + WrStringAndCheck(buf, fmt.Sprintf("# fish completion for %-36s -*- shell-script -*-\n", name)) + WrStringAndCheck(buf, fmt.Sprintf(` function __%[1]s_debug set file "$BASH_COMP_DEBUG_FILE" if test -n "$file" diff --git a/fish_completions_test.go b/fish_completions_test.go index 532b6055a..bb4ebfe63 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -15,7 +15,7 @@ func TestCompleteNoDesCmdInFishScript(t *testing.T) { rootCmd.AddCommand(child) buf := new(bytes.Buffer) - rootCmd.GenFishCompletion(buf, false) + er(rootCmd.GenFishCompletion(buf, false)) output := buf.String() check(t, output, ShellCompNoDescRequestCmd) @@ -31,7 +31,7 @@ func TestCompleteCmdInFishScript(t *testing.T) { rootCmd.AddCommand(child) buf := new(bytes.Buffer) - rootCmd.GenFishCompletion(buf, true) + er(rootCmd.GenFishCompletion(buf, true)) output := buf.String() check(t, output, ShellCompRequestCmd) @@ -41,7 +41,7 @@ func TestCompleteCmdInFishScript(t *testing.T) { func TestProgWithDash(t *testing.T) { rootCmd := &Command{Use: "root-dash", Args: NoArgs, Run: emptyRun} buf := new(bytes.Buffer) - rootCmd.GenFishCompletion(buf, false) + er(rootCmd.GenFishCompletion(buf, false)) output := buf.String() // Functions name should have replace the '-' @@ -56,7 +56,7 @@ func TestProgWithDash(t *testing.T) { func TestProgWithColon(t *testing.T) { rootCmd := &Command{Use: "root:colon", Args: NoArgs, Run: emptyRun} buf := new(bytes.Buffer) - rootCmd.GenFishCompletion(buf, false) + er(rootCmd.GenFishCompletion(buf, false)) output := buf.String() // Functions name should have replace the ':' diff --git a/zsh_completions.go b/zsh_completions.go index c25ce680c..914d6e424 100644 --- a/zsh_completions.go +++ b/zsh_completions.go @@ -70,12 +70,12 @@ func (c *Command) genZshCompletion(w io.Writer, includeDesc bool) error { return err } -func genZshComp(buf *bytes.Buffer, name string, includeDesc bool) { +func genZshComp(buf io.StringWriter, name string, includeDesc bool) { compCmd := ShellCompRequestCmd if !includeDesc { compCmd = ShellCompNoDescRequestCmd } - buf.WriteString(fmt.Sprintf(`#compdef _%[1]s %[1]s + WrStringAndCheck(buf, fmt.Sprintf(`#compdef _%[1]s %[1]s # zsh completion for %-36[1]s -*- shell-script -*-