github.com/daixiang0/gci@v0.13.0/cmd/gci/completion.go (about) 1 package gci 2 3 import ( 4 "strings" 5 6 "github.com/spf13/cobra" 7 ) 8 9 func subCommandOrGoFileCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 10 var commandAliases []string 11 for _, subCmd := range cmd.Commands() { 12 commandAliases = append(commandAliases, subCmd.Name()) 13 commandAliases = append(commandAliases, subCmd.Aliases...) 14 } 15 for _, subCmdStr := range commandAliases { 16 if strings.HasPrefix(subCmdStr, toComplete) { 17 // completion for commands is already provided by cobra 18 // do not return file completion 19 return []string{}, cobra.ShellCompDirectiveNoFileComp 20 } 21 } 22 return goFileCompletion(cmd, args, toComplete) 23 } 24 25 func goFileCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 26 return []string{"go"}, cobra.ShellCompDirectiveFilterFileExt 27 }