-
Notifications
You must be signed in to change notification settings - Fork 102
Delegate help and completion to extensions when applicable #1137
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a49ee51
Delegate help and completion to extensions when applicable
jeri-temporal 02cca02
New tests work with windows paths too
jeri-temporal dc4e7fd
Merge branch 'main' into main
jeri-temporal 6a01b9b
Fix typos in comments
jeri-temporal 2c54973
Switch back to "/" as command delimiter, add test for nested extensio…
jeri-temporal 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
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 |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| package temporalcli | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "fmt" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "golang.org/x/exp/maps" | ||
| ) | ||
|
|
||
| // customizeHelpCommand adds the --all/-a flag to Cobra's built-in help command | ||
|
|
@@ -53,36 +56,64 @@ func customizeHelpCommand(rootCmd *cobra.Command) { | |
| } | ||
|
|
||
| // registerExtensionCommands adds discovered extensions as placeholder commands | ||
| // so they appear in the default help output. It filters extensions based on | ||
| // the current command's path in the hierarchy. | ||
| // so they appear in shell completion and the default help output. It filters extensions | ||
| // based on the current command's path in the hierarchy. | ||
| func registerExtensionCommands(cmd *cobra.Command) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that this is used for shell completion as well, it feels like it probably belongs in |
||
| cmdPath := strings.Fields(cmd.CommandPath()) | ||
| seen := make(map[string]bool) | ||
|
|
||
| for _, ext := range discoverExtensions() { | ||
| // When built-in subcommands are nested under other subcommands (e.g. `temporal activity cancel`), | ||
| // they're guaranteed to have a defined parent (`temporal activity`, which has the parent `temporal`). | ||
| // Extension subcommands can also be nested, but when `temporal foo bar` is created via an executable | ||
| // named temporal-foo-bar, there's no guarantee that the `temporal foo` command exists. For the full | ||
| // command to show up in help and completion, placeholders must exist at every level. | ||
| extensionsAndExecutables := discoverExtensions() | ||
|
|
||
| extensionKeys := maps.Keys(extensionsAndExecutables) | ||
|
|
||
| // Shorter command paths first ensures the paths shown in the short description of placeholder commands | ||
| // point to the closest match | ||
| slices.SortFunc(extensionKeys, func(a, b string) int { | ||
| return cmp.Compare(strings.Count(a, "/"), strings.Count(b, "/")) | ||
| }) | ||
| for _, extKey := range extensionKeys { | ||
| ext := strings.Split(extKey, "/") | ||
| // Extension must be deeper than current command and share the same prefix | ||
| if len(ext) <= len(cmdPath) || !slices.Equal(ext[:len(cmdPath)], cmdPath) { | ||
| continue | ||
| } | ||
|
|
||
| // Get the next level command name | ||
| nextPart := ext[len(cmdPath)] | ||
| extPath := ext[len(cmdPath):] | ||
|
|
||
| // Skip if already added | ||
| if seen[nextPart] { | ||
| continue | ||
| } | ||
| parent := cmd | ||
| executablePath := extensionsAndExecutables[extKey] | ||
|
|
||
| // Skip if a built-in command exists | ||
| if found, _, _ := cmd.Find([]string{nextPart}); found != cmd { | ||
| continue | ||
| for i, nextPart := range extPath { | ||
| if found, _, _ := parent.Find([]string{nextPart}); found != parent { | ||
| // Because we order extensions by depth, we can trust that any command that already exists already | ||
| // has the most correct definition for its depth. | ||
| parent = found | ||
| continue | ||
| } | ||
|
|
||
| var short string | ||
|
|
||
| if i == len(extPath)-1 { | ||
| short = fmt.Sprintf("An extension command located at %s", executablePath) | ||
| } else { | ||
| short = fmt.Sprintf("Extension commands under %s", strings.Join(ext[:len(cmdPath)+i+1], " ")) | ||
|
jeri-temporal marked this conversation as resolved.
|
||
| } | ||
|
|
||
| newCmd := &cobra.Command{ | ||
| Use: nextPart, | ||
| // Short descriptions must be unique at a given level because otherwise shell completion will | ||
| // group all commands with the same description on the same line | ||
| Short: short, | ||
| DisableFlagParsing: true, | ||
| Run: func(*cobra.Command, []string) {}, | ||
| } | ||
| parent.AddCommand(newCmd) | ||
| parent = newCmd | ||
| } | ||
|
|
||
| seen[nextPart] = true | ||
| cmd.AddCommand(&cobra.Command{ | ||
| Use: nextPart, | ||
| DisableFlagParsing: true, | ||
| Run: func(*cobra.Command, []string) {}, | ||
| }) | ||
| } | ||
| } | ||
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.