Skip to content

Shell completion to suggest installed version feature added (resolves #420)#501

Open
RushabhRatnaparkhi wants to merge 4 commits into
tofuutils:mainfrom
RushabhRatnaparkhi:main
Open

Shell completion to suggest installed version feature added (resolves #420)#501
RushabhRatnaparkhi wants to merge 4 commits into
tofuutils:mainfrom
RushabhRatnaparkhi:main

Conversation

@RushabhRatnaparkhi

Copy link
Copy Markdown

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.

@kvendingoldo

Copy link
Copy Markdown
Collaborator

Looks file, thank you for PR!
@dvaumoron can you double-check it, please?

Comment thread cmd/tenv/subcmd.go Outdated
Comment thread cmd/tenv/subcmd.go Outdated
Comment thread cmd/tenv/subcmd.go
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"maps"
"slices"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move those two import up with the others standard library imports ?

@dvaumoron

Copy link
Copy Markdown
Contributor

Can you fix the compilation issues ?

Comment thread COMPLETION_IMPLEMENTATION.md Outdated
@@ -0,0 +1,69 @@
# Bash Completion Implementation for tenv

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that we need this markdown file in a main. Just post it to MR body, please and remove from PR

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the markdown, sorry it took so long , please let me know if there are any more changes you would like

Copilot AI review requested due to automatic review settings January 15, 2026 07:52
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ValidArgsFunction implementations to the use, install, and uninstall commands for shell completion
  • Introduced package-level commonVersionStrategies variable containing common version selection strategies
  • Added test_completion.sh script 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.

Comment thread cmd/tenv/subcmd.go
}

func newUninstallCmd(versionManager versionmanager.VersionManager) *cobra.Command {
func newUninstallCmd(versionManager versionmanager.VersionManager, params subCmdParams) *cobra.Command {

Copilot AI Jan 15, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
func newUninstallCmd(versionManager versionmanager.VersionManager, params subCmdParams) *cobra.Command {
func newUninstallCmd(versionManager versionmanager.VersionManager) *cobra.Command {

Copilot uses AI. Check for mistakes.
Comment thread cmd/tenv/subcmd.go
Comment on lines +367 to +369
conf := versionManager.Conf
// Initialize config for completion
conf.InitDisplayer(true) // quiet mode for completion

Copilot AI Jan 15, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment thread cmd/tenv/subcmd.go
}

func newUninstallCmd(versionManager versionmanager.VersionManager) *cobra.Command {
func newUninstallCmd(versionManager versionmanager.VersionManager, params subCmdParams) *cobra.Command {

Copilot AI Jan 15, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
func newUninstallCmd(versionManager versionmanager.VersionManager, params subCmdParams) *cobra.Command {
func newUninstallCmd(versionManager versionmanager.VersionManager, _params subCmdParams) *cobra.Command {

Copilot uses AI. Check for mistakes.
Comment thread cmd/tenv/subcmd.go
// Get local versions using one-liner
localVersions := slices.Collect(maps.Keys(versionManager.LocalSet()))

// Use global variable

Copilot AI Jan 15, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// Use global variable
// Use package-level strategies

Copilot uses AI. Check for mistakes.
Comment thread cmd/tenv/subcmd.go
Comment on lines +431 to +434
// Combine local versions and strategies
versions := make([]string, 0, len(localVersions)+len(commonVersionStrategies))
versions = append(versions, localVersions...)
versions = append(versions, commonVersionStrategies...)

Copilot AI Jan 15, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 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...)

Copilot uses AI. Check for mistakes.
Comment thread test_completion.sh
source /tmp/tenv_completion.bash

echo "Building tenv..."
make build > /dev/null 2>&1

Copilot AI Jan 15, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
make build > /dev/null 2>&1
if ! make build > /dev/null; then
echo "Build failed, aborting completion tests." >&2
exit 1
fi

Copilot uses AI. Check for mistakes.
Comment thread test_completion.sh
Comment on lines +16 to +18
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

Copilot AI Jan 15, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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"

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants