-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add shell completion #222
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
Add shell completion #222
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a8b25a4
Update shell completion settings
thelovekesh c50a7ef
Add shell completion functions
thelovekesh 1c3afe4
Add flag completion functions
thelovekesh ff048df
Add completions func to offer license names and enable file comp
thelovekesh 3aa45eb
Add comp for license
thelovekesh 78a6aab
Add shell completion for global flags
thelovekesh 33c88ec
Add shell completions while installing wpm
thelovekesh a738c64
Update formatting
thelovekesh 473059b
Fix shell comp loading in zsh
thelovekesh 48d31da
Add shell comp in powershell
thelovekesh 1c45cde
Update powershell script formatting
thelovekesh c794d9d
Fix completions path eval
thelovekesh 8459826
Make bin path addition idempotent
thelovekesh 99d4b3e
Update arch detection
thelovekesh 77f7e58
Remove redundant comments
thelovekesh 57c0e93
Update release template
thelovekesh 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
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,145 @@ | ||
| package completion | ||
|
|
||
| import ( | ||
| "os" | ||
| "sort" | ||
|
|
||
| "go.wpm.so/cli/pkg/pm/wpmjson" | ||
| "go.wpm.so/cli/pkg/pm/wpmjson/types" | ||
| "go.wpm.so/cli/pkg/pm/wpmlock" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // PackagesFromWpmJson offers completion for package names declared in | ||
| // wpm.json's dependencies and devDependencies. | ||
| func PackagesFromWpmJson() cobra.CompletionFunc { | ||
| return Unique(func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
|
|
||
| cfg, err := wpmjson.Read(cwd) | ||
| if err != nil || cfg == nil { | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
|
|
||
| var names []string | ||
| if cfg.Dependencies != nil { | ||
| for name := range *cfg.Dependencies { | ||
| names = append(names, name) | ||
| } | ||
| } | ||
| if cfg.DevDependencies != nil { | ||
| for name := range *cfg.DevDependencies { | ||
| names = append(names, name) | ||
| } | ||
| } | ||
| sort.Strings(names) | ||
| return names, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
| } | ||
|
|
||
| // PackagesFromLockfile offers completion for package names recorded in | ||
| // wpm.lock. | ||
| func PackagesFromLockfile() cobra.CompletionFunc { | ||
| return func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
|
|
||
| lock, err := wpmlock.Read(cwd) | ||
| if err != nil || lock == nil { | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
|
|
||
| names := make([]string, 0, len(lock.Packages)) | ||
| for name := range lock.Packages { | ||
| names = append(names, name) | ||
| } | ||
| sort.Strings(names) | ||
| return names, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| } | ||
|
|
||
| // PackageTypes offers completion for the closed set of valid package types. | ||
| func PackageTypes() cobra.CompletionFunc { | ||
| return FromList( | ||
| string(types.TypePlugin), | ||
| string(types.TypeTheme), | ||
| string(types.TypeMuPlugin), | ||
| ) | ||
| } | ||
|
|
||
| // PackageVisibility offers completion for the closed set of valid visibility. | ||
| func PackageVisibility() cobra.CompletionFunc { | ||
| return FromList( | ||
| string(types.VisibilityPublic), | ||
| string(types.VisibilityPrivate), | ||
| ) | ||
| } | ||
|
|
||
| // PublishTags suggests a non-exhaustive list of common dist-tags for | ||
| // `wpm publish --tag`. | ||
| func PublishTags() cobra.CompletionFunc { | ||
| return func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
| return []string{"latest", "next", "beta", "alpha"}, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| } | ||
|
|
||
| // PackageLicenses suggests a non-exhaustive list of common SPDX licenses. | ||
| func PackageLicenses() cobra.CompletionFunc { | ||
| return func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
| return []string{"GPL-2.0-or-later", "GPL-3.0-or-later"}, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| } | ||
|
|
||
| // FileNames opts a flag or positional argument back into the shell's default | ||
| // file completion. | ||
| func FileNames() cobra.CompletionFunc { | ||
| return func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
| return nil, cobra.ShellCompDirectiveDefault | ||
| } | ||
| } | ||
|
|
||
| // FromList offers completion for the given list of options. | ||
| func FromList(options ...string) cobra.CompletionFunc { | ||
| return Unique(cobra.FixedCompletions(options, cobra.ShellCompDirectiveNoFileComp)) | ||
| } | ||
|
|
||
| // Unique wraps a completion func and removes completion results that are | ||
| // already consumed (i.e., appear in "args"). | ||
| // | ||
| // For example: | ||
| // | ||
| // # initial completion: args is empty, so all results are shown | ||
| // command <tab> | ||
| // one two three | ||
| // | ||
| // # "one" is already used so omitted | ||
| // command one <tab> | ||
| // two three | ||
| func Unique(fn cobra.CompletionFunc) cobra.CompletionFunc { | ||
| return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| all, dir := fn(cmd, args, toComplete) | ||
| if len(all) == 0 || len(args) == 0 { | ||
| return all, dir | ||
| } | ||
|
|
||
| alreadyCompleted := make(map[string]struct{}, len(args)) | ||
| for _, a := range args { | ||
| alreadyCompleted[a] = struct{}{} | ||
| } | ||
|
|
||
| out := make([]string, 0, len(all)) | ||
| for _, c := range all { | ||
| if _, ok := alreadyCompleted[c]; !ok { | ||
| out = append(out, c) | ||
| } | ||
| } | ||
|
|
||
| return out, dir | ||
| } | ||
| } | ||
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
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.