-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const CompletionFlag = "completion" | ||
|
||
func registerCompletionFlag(cmd *cobra.Command) { | ||
cmd.Flags().String(CompletionFlag, "", "Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'.") | ||
err := cmd.RegisterFlagCompletionFunc( | ||
CompletionFlag, | ||
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"bash", "zsh", "fish", "powershell"}, cobra.ShellCompDirectiveNoFileComp | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func completion(cmd *cobra.Command, args []string) error { | ||
completionFlag, err := cmd.Flags().GetString(CompletionFlag) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
switch completionFlag { | ||
case "bash": | ||
if err := cmd.Root().GenBashCompletion(cmd.OutOrStdout()); err != nil { | ||
return err | ||
} | ||
case "zsh": | ||
if err := cmd.Root().GenZshCompletion(cmd.OutOrStdout()); err != nil { | ||
return err | ||
} | ||
case "fish": | ||
if err := cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true); err != nil { | ||
return err | ||
} | ||
case "powershell": | ||
if err := cmd.Root().GenPowerShellCompletionWithDesc(cmd.OutOrStdout()); err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("%v: invalid shell", completionFlag) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters