-
Notifications
You must be signed in to change notification settings - Fork 0
feat(completion): Implement completion commands #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,36 @@ | ||
| package completion | ||
|
|
||
| import ( | ||
| "morpherctl/internal/completion" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var CompletionCmd = &cobra.Command{ | ||
| Use: "completion [bash|zsh|fish|powershell]", | ||
| Short: "Generate shell completion script", | ||
| Long: `Generate shell completion script for morpherctl. | ||
|
|
||
| The completion script supports bash, zsh, fish, and powershell. | ||
| To load completions in your current shell session: | ||
|
|
||
| Bash: | ||
| $ source <(morpherctl completion bash) | ||
|
|
||
| Zsh: | ||
| $ source <(morpherctl completion zsh) | ||
|
|
||
| Fish: | ||
| $ morpherctl completion fish | source | ||
|
|
||
| PowerShell: | ||
| PS> morpherctl completion powershell | Out-String | Invoke-Expression | ||
|
|
||
| To load completions for every new session, write to a file and source in your shell's config file e.g. ~/.bashrc or ~/.zshrc.`, | ||
| ValidArgs: completion.GetSupportedShells(), | ||
| Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| shell := args[0] | ||
| return completion.GenerateCompletion(cmd, shell) | ||
| }, | ||
| } |
This file contains hidden or 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 hidden or 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,44 @@ | ||
| package completion | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // GenerateCompletion generates shell completion script for the given shell. | ||
| func GenerateCompletion(cmd *cobra.Command, shell string) error { | ||
| switch shell { | ||
| case "bash": | ||
| if err := cmd.Root().GenBashCompletion(os.Stdout); err != nil { | ||
| return fmt.Errorf("failed to generate bash completion: %w", err) | ||
| } | ||
| return nil | ||
| case "zsh": | ||
| if err := cmd.Root().GenZshCompletion(os.Stdout); err != nil { | ||
| return fmt.Errorf("failed to generate zsh completion: %w", err) | ||
| } | ||
| return nil | ||
| case "fish": | ||
| if err := cmd.Root().GenFishCompletion(os.Stdout, true); err != nil { | ||
| return fmt.Errorf("failed to generate fish completion: %w", err) | ||
| } | ||
| return nil | ||
| case "powershell": | ||
| if err := cmd.Root().GenPowerShellCompletion(os.Stdout); err != nil { | ||
| return fmt.Errorf("failed to generate powershell completion: %w", err) | ||
| } | ||
| return nil | ||
| default: | ||
| if err := cmd.Help(); err != nil { | ||
| return fmt.Errorf("failed to display help: %w", err) | ||
| } | ||
| return nil | ||
jongwooo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| // GetSupportedShells returns the list of supported shell types. | ||
| func GetSupportedShells() []string { | ||
| return []string{"bash", "zsh", "fish", "powershell"} | ||
| } | ||
This file contains hidden or 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,65 @@ | ||
| package completion | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGenerateCompletion(t *testing.T) { | ||
| // Create a root command for testing. | ||
| rootCmd := &cobra.Command{Use: "test"} | ||
| rootCmd.AddCommand(&cobra.Command{Use: "subcommand"}) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| shell string | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "bash completion", | ||
| shell: "bash", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "zsh completion", | ||
| shell: "zsh", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "fish completion", | ||
| shell: "fish", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "powershell completion", | ||
| shell: "powershell", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "invalid shell", | ||
| shell: "invalid", | ||
| expectError: false, // Help() doesn't return an error. | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := GenerateCompletion(rootCmd, tt.shell) | ||
| if tt.expectError { | ||
| assert.Error(t, err) | ||
| } else { | ||
| assert.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetSupportedShells(t *testing.T) { | ||
| shells := GetSupportedShells() | ||
| expectedShells := []string{"bash", "zsh", "fish", "powershell"} | ||
|
|
||
| assert.ElementsMatch(t, expectedShells, shells) | ||
| assert.Len(t, shells, 4) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.