-
Notifications
You must be signed in to change notification settings - Fork 194
feat: add command completion #650
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
daniel-hutao
merged 1 commit into
devstream-io:main
from
prodanlabs:feat-command-completion
Jun 10, 2022
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
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,61 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/devstream-io/devstream/internal/pkg/completion" | ||
| ) | ||
|
|
||
| func completionCMD(out io.Writer) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "completion", | ||
| Short: "Generate the autocompletion script for dtm for the specified shell", | ||
| Long: "See each sub-command's help for details on how to use the generated script.", | ||
| DisableFlagsInUseLine: true, | ||
| Args: cobra.ExactValidArgs(1), | ||
| } | ||
|
|
||
| binaryName := filepath.Base(os.Args[0]) | ||
| bash := &cobra.Command{ | ||
| Use: "bash", | ||
| Short: "generate autocompletion script for bash", | ||
| Example: completion.BashExample(binaryName), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return completion.CompletionBash(out, cmd) | ||
| }, | ||
| } | ||
|
|
||
| zsh := &cobra.Command{ | ||
| Use: "zsh", | ||
| Short: "generate autocompletion script for zsh", | ||
| Example: completion.ZshExample(binaryName), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return completion.CompletionZsh(out, cmd) | ||
| }, | ||
| } | ||
|
|
||
| fish := &cobra.Command{ | ||
| Use: "fish", | ||
| Short: "generate autocompletion script for fish", | ||
| Example: completion.FishExample(binaryName), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return cmd.Root().GenFishCompletion(out, true) | ||
| }, | ||
| } | ||
|
|
||
| powershell := &cobra.Command{ | ||
| Use: "powershell", | ||
| Short: "generate autocompletion script for powershell", | ||
| Example: completion.PowershellExample(binaryName), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return cmd.Root().GenPowerShellCompletionWithDesc(out) | ||
| }, | ||
| } | ||
| cmd.AddCommand(bash, zsh, fish, powershell) | ||
|
|
||
| return cmd | ||
| } |
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
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
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,100 @@ | ||
| package completion | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/devstream-io/devstream/cmd/devstream/list" | ||
| "github.com/devstream-io/devstream/pkg/util/log" | ||
| ) | ||
|
|
||
| func FlagPluginsCompletion(cmd *cobra.Command, flag string) { | ||
| if err := cmd.RegisterFlagCompletionFunc(flag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return list.PluginsNameSlice(), cobra.ShellCompDirectiveDefault | ||
| }); err != nil { | ||
| log.Warn(err) | ||
| } | ||
| } | ||
|
|
||
| func FlagConfigFileCompletion(cmd *cobra.Command) { | ||
| validConfigFilenames := []string{"yaml"} | ||
|
|
||
| if err := cmd.Flags().SetAnnotation("config-file", cobra.BashCompFilenameExt, validConfigFilenames); err != nil { | ||
| log.Warn(err) | ||
| } | ||
| } | ||
|
|
||
| func CompletionBash(out io.Writer, cmd *cobra.Command) error { | ||
| err := cmd.Root().GenBashCompletion(out) | ||
|
|
||
| // The default binary name downloaded from the Releases page is dtm-{os}-amd64 | ||
| // solve the problem that autocompletion fails when the name of the binary is not dtm | ||
| if binary := filepath.Base(os.Args[0]); binary != "dtm" { | ||
| renamedBinary := ` | ||
| # the user renamed the dtm binary | ||
| if [[ $(type -t compopt) = "builtin" ]]; then | ||
| complete -o default -F __start_dtm %[1]s | ||
| else | ||
| complete -o default -o nospace -F __start_dtm %[1]s | ||
| fi | ||
| ` | ||
| fmt.Fprintf(out, renamedBinary, binary) | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func CompletionZsh(out io.Writer, cmd *cobra.Command) error { | ||
| err := cmd.Root().GenZshCompletionNoDesc(out) | ||
|
|
||
| // The default binary name downloaded from the Releases page is dtm-{os}-amd64 | ||
| // solve the problem that autocompletion fails when the name of the binary is not dtm | ||
| if binary := filepath.Base(os.Args[0]); binary != "dtm" { | ||
| renamedBinary := ` | ||
| # the user renamed the dtm binary | ||
| compdef _dtm %[1]s | ||
| ` | ||
| fmt.Fprintf(out, renamedBinary, binary) | ||
| } | ||
|
|
||
| fmt.Fprintf(out, "compdef _dtm dtm") | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func BashExample(binary string) string { | ||
| return fmt.Sprintf(`Load is completions in the current shell session: | ||
| # source <(%s completion bash) | ||
|
|
||
| Load completions for every new session: | ||
| (in Linux)# %s completion bash > /etc/bash_completion.d/dtm | ||
| (in MacOS)# %s completion bash > $(brew --prefix)/etc/bash_completion.d/dtm`, binary, binary, binary) | ||
| } | ||
|
|
||
| func ZshExample(binary string) string { | ||
| return fmt.Sprintf(`Load is completions in the current shell session: | ||
| # source <(%s completion zsh) | ||
|
|
||
| Load completions for every new session: | ||
| # %s completion zsh > "${fpath[1]}/_dtm"`, binary, binary) | ||
| } | ||
|
|
||
| func FishExample(binary string) string { | ||
| return fmt.Sprintf(`Load is completions in the current shell session: | ||
| # %s completion fish | source | ||
|
|
||
| Load completions for every new session: | ||
| # %s completion fish > ~/.config/fish/completions/dtm.fish`, binary, binary) | ||
| } | ||
|
|
||
| func PowershellExample(binary string) string { | ||
| return fmt.Sprintf(`Load is completions in the current shell session: | ||
| C:\> %s completion powershell | Out-String | Invoke-Expression | ||
|
|
||
| Load completions for every new session: | ||
| add the output of the above command to powershell profile.`, binary) | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said before, it's not mandatory to achieve and you also didn't think it is necessary to achieve, but since you have achieved it, I refer to the official documentation and found that this implementation is not applicable to
FishandPowerShellFish doesn't support
BashCompFilenameExtPowershell doesn't support
BashCompFilenameExtA more generic implementation is as follows.
https://github.com/spf13/cobra/blob/d8184d32696bee36f682bcb3ace5642af54cd7ad/shell_completions.md#specify-valid-filename-extensions-for-flags-that-take-a-filename
Of course, if you still don't think it's necessary to implement this feature, you can just remove the relevant code. Thanks again.