-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/shell completion #8
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
Open
404Simon
wants to merge
5
commits into
fosrl:main
Choose a base branch
from
404Simon:feature/shell-completion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9a3f6b
feat: shell completion
404Simon 7e13471
chore: bump newt version
404Simon db70069
fix(completion): skip initialization hook to prevent config errors
404Simon b58627d
fix(deps): align newt replace directive with required version
404Simon 41ad91b
fix(completion): handle errors from shell completion generation
404Simon 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
Some comments aren't visible on the classic Files Changed page.
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,75 @@ | ||
| package completion | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/fosrl/cli/internal/logger" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func CompletionCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "completion [bash|zsh|fish]", | ||
| Short: "Generate shell completion script", | ||
| Long: `Generate shell completion script for the specified shell. | ||
|
|
||
| The completion script can be sourced to enable command-line completion for pangolin. | ||
|
|
||
| Bash: | ||
| $ source <(pangolin completion bash) | ||
|
|
||
| To load completions for each session, execute once: | ||
| Linux: | ||
| $ pangolin completion bash > /etc/bash_completion.d/pangolin | ||
| macOS: | ||
| $ pangolin completion bash > /usr/local/etc/bash_completion.d/pangolin | ||
|
|
||
| Zsh: | ||
| If shell completion is not already enabled in your environment, you will need | ||
| to enable it. You can execute the following once: | ||
| $ echo "autoload -U compinit; compinit" >> ~/.zshrc | ||
|
|
||
| To load completions for each session, execute once: | ||
| $ pangolin completion zsh > "${fpath[1]}/_pangolin" | ||
|
|
||
| You will need to start a new shell for this setup to take effect. | ||
|
|
||
| Fish: | ||
| $ pangolin completion fish | source | ||
|
|
||
| To load completions for each session, execute once: | ||
| $ pangolin completion fish > ~/.config/fish/completions/pangolin.fish | ||
| `, | ||
| DisableFlagsInUseLine: true, | ||
| ValidArgs: []string{"bash", "zsh", "fish"}, | ||
| Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| if err := completionMain(cmd, args); err != nil { | ||
| os.Exit(1) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func completionMain(cmd *cobra.Command, args []string) error { | ||
| switch args[0] { | ||
| case "bash": | ||
| if err := cmd.Root().GenBashCompletion(os.Stdout); err != nil { | ||
| logger.Error("Failed to generate bash completion: %v", err) | ||
| return err | ||
| } | ||
| case "zsh": | ||
| if err := cmd.Root().GenZshCompletion(os.Stdout); err != nil { | ||
| logger.Error("Failed to generate zsh completion: %v", err) | ||
| return err | ||
| } | ||
| case "fish": | ||
| if err := cmd.Root().GenFishCompletion(os.Stdout, true); err != nil { | ||
| logger.Error("Failed to generate fish completion: %v", err) | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } |
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
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,50 @@ | ||
| ## pangolin completion | ||
|
|
||
| Generate shell completion script | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Generate shell completion script for the specified shell. | ||
|
|
||
| The completion script can be sourced to enable command-line completion for pangolin. | ||
|
|
||
| Bash: | ||
| $ source <(pangolin completion bash) | ||
|
|
||
| To load completions for each session, execute once: | ||
| Linux: | ||
| $ pangolin completion bash > /etc/bash_completion.d/pangolin | ||
| macOS: | ||
| $ pangolin completion bash > /usr/local/etc/bash_completion.d/pangolin | ||
|
|
||
| Zsh: | ||
| If shell completion is not already enabled in your environment, you will need | ||
| to enable it. You can execute the following once: | ||
| $ echo "autoload -U compinit; compinit" >> ~/.zshrc | ||
|
|
||
| To load completions for each session, execute once: | ||
| $ pangolin completion zsh > "${fpath[1]}/_pangolin" | ||
|
|
||
| You will need to start a new shell for this setup to take effect. | ||
|
|
||
| Fish: | ||
| $ pangolin completion fish | source | ||
|
|
||
| To load completions for each session, execute once: | ||
| $ pangolin completion fish > ~/.config/fish/completions/pangolin.fish | ||
|
|
||
|
|
||
| ``` | ||
| pangolin completion [bash|zsh|fish] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for completion | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [pangolin](pangolin.md) - Pangolin CLI | ||
|
|
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
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.