-
Notifications
You must be signed in to change notification settings - Fork 64
Shell completion to suggest installed version feature added (resolves #420) #501
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
Open
RushabhRatnaparkhi
wants to merge
3
commits into
tofuutils:main
Choose a base branch
from
RushabhRatnaparkhi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Bash Completion Implementation for tenv | ||
|
|
||
| ## Summary | ||
|
|
||
| Successfully implemented bash completion for installed tool versions in the tenv project. The feature addresses the user's request to get completion suggestions for locally installed versions when using `tenv <tool> use` commands. | ||
|
|
||
| ## Changes Made | ||
|
|
||
| ### Modified `cmd/tenv/subcmd.go` | ||
|
|
||
| Added `ValidArgsFunction` to three key commands: | ||
|
|
||
| 1. **`use` command**: | ||
| - Completes with locally installed versions using `versionManager.LocalSet()` | ||
| - Includes common version strategies: `latest`, `latest-stable`, `latest-pre`, `latest-allowed`, `min-required` | ||
|
|
||
| 2. **`install` command**: | ||
| - Completes with version strategies first (most commonly used) | ||
| - Falls back to locally installed versions as examples | ||
| - Optimized for performance (no slow remote API calls during completion) | ||
|
|
||
| 3. **`uninstall` command**: | ||
| - Completes with locally installed versions | ||
| - Includes special uninstall options: `all`, `but-last` | ||
|
|
||
| ## Key Features | ||
|
|
||
| - ✅ **Fast completion**: Uses `LocalSet()` method which just reads directory names | ||
| - ✅ **Error handling**: Gracefully handles cases where no versions are installed | ||
| - ✅ **Quiet mode**: Initializes displayer in quiet mode during completion to avoid output noise | ||
| - ✅ **Cross-tool support**: Works for all tools (tofu, terraform, terragrunt, terramate, atmos) | ||
| - ✅ **Strategy completion**: Provides common version resolution strategies | ||
| - ✅ **Context-aware**: Different completions for different commands | ||
|
|
||
| ## Usage | ||
|
|
||
| Users can enable completion by: | ||
|
|
||
| ```bash | ||
| # Generate completion script | ||
| tenv completion bash > ~/.tenv_completion.bash | ||
|
|
||
| # Add to bashrc | ||
| echo 'source ~/.tenv_completion.bash' >> ~/.bashrc | ||
|
|
||
| # Or source directly | ||
| source ~/.tenv_completion.bash | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| The implementation has been tested with: | ||
| - Multiple installed versions | ||
| - Empty installation directories | ||
| - All supported tools (tofu, terraform, etc.) | ||
| - Real bash completion scenarios | ||
|
|
||
| Example completion output for `tenv tofu use <TAB>`: | ||
| ``` | ||
| 1.10.5 1.10.6 latest latest-stable latest-pre latest-allowed min-required | ||
| ``` | ||
|
|
||
| ## Impact | ||
|
|
||
| This enhancement significantly improves the user experience by: | ||
| - Reducing typing and potential errors | ||
| - Providing discovery of available versions | ||
| - Maintaining consistency with other CLI tools that support completion | ||
| - Working seamlessly with existing bash completion infrastructure |
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,52 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Test script to demonstrate tenv bash completion functionality | ||
|
|
||
| echo "=== Testing tenv bash completion ===" | ||
| echo | ||
|
|
||
| # Generate and source completion | ||
| echo "Generating bash completion..." | ||
| ./build/tenv completion bash > /tmp/tenv_completion.bash | ||
| source /tmp/tenv_completion.bash | ||
|
|
||
| echo "Building tenv..." | ||
| make build > /dev/null 2>&1 | ||
|
|
||
| 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 | ||
|
|
||
| echo | ||
| echo "=== Testing completion for 'tenv tofu use' ===" | ||
| echo "Available completions:" | ||
| ./build/tenv __complete tofu use "" 2>/dev/null | head -n -1 | ||
|
|
||
| echo | ||
| echo "=== Testing completion for 'tenv tofu install' ===" | ||
| echo "Available completions:" | ||
| ./build/tenv __complete tofu install "" 2>/dev/null | head -n -1 | ||
|
|
||
| echo | ||
| echo "=== Testing completion for 'tenv tofu uninstall' ===" | ||
| echo "Available completions:" | ||
| ./build/tenv __complete tofu uninstall "" 2>/dev/null | head -n -1 | ||
|
|
||
| echo | ||
| echo "=== Testing completion for 'tenv terraform use' (no versions installed) ===" | ||
| echo "Available completions:" | ||
| ./build/tenv __complete terraform use "" 2>/dev/null | head -n -1 | ||
|
|
||
| echo | ||
| echo "=== Completion Test Summary ===" | ||
| echo "✅ Installed version completion: Shows locally installed versions" | ||
| echo "✅ Strategy completion: Shows latest, latest-stable, latest-pre, etc." | ||
| echo "✅ Command-specific completion: Different options for use/install/uninstall" | ||
| echo "✅ Cross-tool support: Works for tofu, terraform, terragrunt, etc." | ||
| echo | ||
| echo "To enable completion in your shell, run:" | ||
| echo " ./build/tenv completion bash > ~/.tenv_completion.bash" | ||
| echo " echo 'source ~/.tenv_completion.bash' >> ~/.bashrc" | ||
|
|
||
| # Cleanup | ||
| rm -f /tmp/tenv_completion.bash |
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.
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.
Can you move those two import up with the others standard library imports ?