Shell completion to suggest installed version feature added (resolves #420)#501
Shell completion to suggest installed version feature added (resolves #420)#501RushabhRatnaparkhi wants to merge 4 commits into
Conversation
a5139b1 to
230d715
Compare
|
Looks file, thank you for PR! |
3a9500f to
c27cbd3
Compare
| "github.com/spf13/cobra" | ||
| "github.com/spf13/pflag" | ||
| "maps" | ||
| "slices" |
There was a problem hiding this comment.
Can you move those two import up with the others standard library imports ?
|
Can you fix the compilation issues ? |
| @@ -0,0 +1,69 @@ | |||
| # Bash Completion Implementation for tenv | |||
There was a problem hiding this comment.
I am not sure that we need this markdown file in a main. Just post it to MR body, please and remove from PR
There was a problem hiding this comment.
I have removed the markdown, sorry it took so long , please let me know if there are any more changes you would like
Signed-off-by: RushabhRatnaparkhi <rushuratnaparkhi2@gmail.com>
Signed-off-by: RushabhRatnaparkhi <rushuratnaparkhi2@gmail.com>
Signed-off-by: RushabhRatnaparkhi <rushuratnaparkhi2@gmail.com>
Signed-off-by: RushabhRatnaparkhi <rushuratnaparkhi2@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR adds bash completion functionality for installed tool versions in tenv, allowing users to get autocomplete suggestions for locally installed versions and common strategies when using commands like tenv <tool> use, install, and uninstall.
Changes:
- Added
ValidArgsFunctionimplementations to theuse,install, anduninstallcommands for shell completion - Introduced package-level
commonVersionStrategiesvariable containing common version selection strategies - Added
test_completion.shscript to demonstrate and test the completion functionality
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| cmd/tenv/subcmd.go | Added completion logic to use, install, and uninstall commands; imported maps and slices packages; defined common version strategies |
| test_completion.sh | New test script to demonstrate bash completion functionality for different commands and tools |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| func newUninstallCmd(versionManager versionmanager.VersionManager) *cobra.Command { | ||
| func newUninstallCmd(versionManager versionmanager.VersionManager, params subCmdParams) *cobra.Command { |
There was a problem hiding this comment.
The function signature for newUninstallCmd has been changed to accept a params parameter, but the call site in cmd/tenv/tenv.go at line 261 still calls it with only one argument (versionManager). This will cause a compilation error. The function signature should either be reverted to match the original signature without params, or the call site needs to be updated to pass the params argument.
| func newUninstallCmd(versionManager versionmanager.VersionManager, params subCmdParams) *cobra.Command { | |
| func newUninstallCmd(versionManager versionmanager.VersionManager) *cobra.Command { |
| conf := versionManager.Conf | ||
| // Initialize config for completion | ||
| conf.InitDisplayer(true) // quiet mode for completion |
There was a problem hiding this comment.
There is a syntax error with incorrect indentation and spacing. The line should be properly aligned with the rest of the code, and the variable declaration "conf := versionManager.Conf" is unnecessary as conf.InitDisplayer is being called directly without using the conf variable elsewhere in this function.
| conf := versionManager.Conf | |
| // Initialize config for completion | |
| conf.InitDisplayer(true) // quiet mode for completion | |
| // Initialize config for completion | |
| versionManager.Conf.InitDisplayer(true) // quiet mode for completion |
| } | ||
|
|
||
| func newUninstallCmd(versionManager versionmanager.VersionManager) *cobra.Command { | ||
| func newUninstallCmd(versionManager versionmanager.VersionManager, params subCmdParams) *cobra.Command { |
There was a problem hiding this comment.
The params parameter is accepted by this function but never used. Since params is not utilized in this function, it should either be removed from the function signature (reverting to the original signature) or be prefixed with an underscore (_params) to indicate it's intentionally unused.
| func newUninstallCmd(versionManager versionmanager.VersionManager, params subCmdParams) *cobra.Command { | |
| func newUninstallCmd(versionManager versionmanager.VersionManager, _params subCmdParams) *cobra.Command { |
| // Get local versions using one-liner | ||
| localVersions := slices.Collect(maps.Keys(versionManager.LocalSet())) | ||
|
|
||
| // Use global variable |
There was a problem hiding this comment.
The comment says "Use global variable" but this is misleading as commonVersionStrategies is a package-level variable, not a global variable. Consider removing this comment or clarifying it to say "Use package-level strategies" to better reflect the code structure.
| // Use global variable | |
| // Use package-level strategies |
| // Combine local versions and strategies | ||
| versions := make([]string, 0, len(localVersions)+len(commonVersionStrategies)) | ||
| versions = append(versions, localVersions...) | ||
| versions = append(versions, commonVersionStrategies...) |
There was a problem hiding this comment.
The order in which versions are presented to the user is inconsistent across commands. In the install command (line 168), common strategies are appended first followed by local versions, while in the use command (lines 432-434), local versions are appended first followed by strategies. This inconsistency makes the user experience less predictable. Consider standardizing the order across all commands - either always show local versions first, or always show strategies first.
| // Combine local versions and strategies | |
| versions := make([]string, 0, len(localVersions)+len(commonVersionStrategies)) | |
| versions = append(versions, localVersions...) | |
| versions = append(versions, commonVersionStrategies...) | |
| // Combine strategies and local versions (strategies first, to match install command) | |
| versions := make([]string, 0, len(localVersions)+len(commonVersionStrategies)) | |
| versions = append(versions, commonVersionStrategies...) | |
| versions = append(versions, localVersions...) |
| source /tmp/tenv_completion.bash | ||
|
|
||
| echo "Building tenv..." | ||
| make build > /dev/null 2>&1 |
There was a problem hiding this comment.
The script silences both stdout and stderr when building (line 14), which could hide important build errors that would cause subsequent tests to fail in unexpected ways. Consider redirecting only stdout to /dev/null and allowing stderr to be visible, or check the exit code and fail early if the build fails.
| make build > /dev/null 2>&1 | |
| if ! make build > /dev/null; then | |
| echo "Build failed, aborting completion tests." >&2 | |
| exit 1 | |
| fi |
| echo "Installing a couple of OpenTofu versions for testing..." | ||
| ./build/tenv tofu install 1.10.5 > /dev/null 2>&1 | ||
| ./build/tenv tofu install 1.10.6 > /dev/null 2>&1 |
There was a problem hiding this comment.
The script does not check for errors when installing test versions (lines 17-18). If these installations fail silently, the subsequent completion tests may produce misleading results or false positives. Consider checking exit codes and reporting installation failures, or at least verifying that the expected versions were actually installed before running the completion tests.
| echo "Installing a couple of OpenTofu versions for testing..." | |
| ./build/tenv tofu install 1.10.5 > /dev/null 2>&1 | |
| ./build/tenv tofu install 1.10.6 > /dev/null 2>&1 | |
| install_tofu_version() { | |
| local version="$1" | |
| if ! ./build/tenv tofu install "$version" > /dev/null 2>&1; then | |
| echo "Error: Failed to install OpenTofu version ${version} for completion tests." >&2 | |
| exit 1 | |
| fi | |
| } | |
| echo "Installing a couple of OpenTofu versions for testing..." | |
| install_tofu_version "1.10.5" | |
| install_tofu_version "1.10.6" |
a31dda8 to
173c454
Compare
Summary
Implements bash completion for installed tool versions in tenv. Users can now press TAB after tenv use to see locally installed versions and common strategies like "latest".
Changes Made
Added ValidArgsFunction to use, install, and uninstall commands in cmd/tenv/subcmd.go
Completion shows installed versions (from LocalSet()) plus predefined strategies
Fast completion without file parsing overhead
Dependencies
No new dependencies required. Uses existing:
Cobra CLI framework (already in project)
tenv's version management system
Testing
Verified with both programmatic (tenv __complete) and real bash completion testing.