Skip to content

Commit

Permalink
fix after rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Jul 13, 2020
1 parent a08ddcb commit 51b84f6
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
10 changes: 5 additions & 5 deletions bash_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=()
Expand Down Expand Up @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions custom_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
46 changes: 23 additions & 23 deletions custom_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -1446,25 +1446,25 @@ 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) {
completions = append(completions, comp)
}
}
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) {
completions = append(completions, comp)
}
}
return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp
})
}))

// Test completing an empty string
output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--introot", "")
Expand Down Expand Up @@ -1643,25 +1643,25 @@ 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) {
completions = append(completions, comp)
}
}
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) {
completions = append(completions, comp)
}
}
return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp
})
}))

// Test completing an empty string
output, err := executeCommand(rootCmd, ShellCompRequestCmd, "--introot", "")
Expand Down
6 changes: 3 additions & 3 deletions fish_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions fish_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 '-'
Expand All @@ -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 ':'
Expand Down
4 changes: 2 additions & 2 deletions zsh_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -*-
Expand Down

0 comments on commit 51b84f6

Please sign in to comment.