Current section
Files
Jump to
Current section
Files
secrethub_cli
COMPLETION_SUMMARY.md
COMPLETION_SUMMARY.md
# Shell Completion Implementation Summary
## Overview
Comprehensive shell completion has been implemented for the SecretHub CLI, supporting both Bash and Zsh shells with intelligent, context-aware autocompletion.
## Files Created
### Core Implementation
1. **`lib/secrethub_cli/completion.ex`**
- Main completion module
- Generates Bash and Zsh completion scripts
- Provides installation instructions
- Exports completion scripts programmatically
2. **`lib/mix/tasks/cli.gen.completion.ex`**
- Mix task for generating completion files
- Usage: `mix cli.gen.completion [bash|zsh|all]`
- Saves scripts to `priv/completion/` directory
- Displays installation instructions
3. **CLI Integration** (modifications to `lib/secrethub_cli.ex`)
- Added `completion` command
- Usage: `secrethub completion bash|zsh`
- Outputs script to stdout for easy installation
### Generated Completion Scripts
4. **`priv/completion/secrethub.bash`** (8.1 KB)
- Full Bash completion script
- Context-aware command completion
- Dynamic data fetching from server
- Option and format completion
5. **`priv/completion/_secrethub`** (12 KB)
- Full Zsh completion script
- Descriptive command completion
- Advanced option grouping
- Enhanced user experience
### Documentation
6. **`README.md`**
- Complete CLI documentation
- Shell completion installation guide
- Usage examples and quickstart
- Configuration and troubleshooting
7. **`COMPLETION.md`**
- Detailed completion installation guide
- Bash and Zsh instructions
- Troubleshooting section
- Advanced usage examples
- Performance considerations
## Features Implemented
### Bash Completion
✅ **Main commands**: login, logout, whoami, secret, policy, agent, config, completion, help, version
✅ **Subcommand completion**:
- `secret`: list, get, create, update, delete, versions, rollback
- `policy`: list, get, create, update, delete, simulate, templates
- `agent`: list, status, logs
- `config`: list, get, set
✅ **Global options**: --help, --version, --server, --format, --quiet, --verbose
✅ **Command-specific options**:
- Login: --role-id, --secret-id
- Secret: --value, --from-file
- Policy: --from-template, --name
✅ **Format completion**: json, table, yaml
✅ **Dynamic completion** (when authenticated):
- Secret paths from server
- Policy names from server
- Agent IDs from server
- Policy templates (static list)
- Config keys
✅ **Context-aware**: Knows which command/subcommand you're in
### Zsh Completion
✅ **All Bash features** plus:
✅ **Descriptive completion**: Each command shows a description
- Example: `secret -- Manage secrets`
✅ **Option descriptions**: Help text for each option
- Example: `--format[Output format]`
✅ **Grouped options**: Related options grouped together
✅ **Advanced value completion**:
- Config value completion based on key
- File path completion for --from-file
- URL completion for --server
✅ **Better presentation**: Cleaner, more informative output
## Installation Methods
### Bash
**System-wide:**
```bash
sudo secrethub completion bash > /etc/bash_completion.d/secrethub
source ~/.bashrc
```
**User-local:**
```bash
mkdir -p ~/.bash_completion.d
secrethub completion bash > ~/.bash_completion.d/secrethub
echo 'source ~/.bash_completion.d/secrethub' >> ~/.bashrc
source ~/.bashrc
```
**macOS (Homebrew):**
```bash
secrethub completion bash > $(brew --prefix)/etc/bash_completion.d/secrethub
source ~/.bash_profile
```
### Zsh
**User-local (recommended):**
```bash
mkdir -p ~/.zsh/completion
secrethub completion zsh > ~/.zsh/completion/_secrethub
echo 'fpath=(~/.zsh/completion $fpath)' >> ~/.zshrc
source ~/.zshrc
```
**System-wide:**
```bash
sudo secrethub completion zsh > /usr/local/share/zsh/site-functions/_secrethub
exec zsh
```
**oh-my-zsh:**
```bash
mkdir -p ~/.oh-my-zsh/custom/plugins/secrethub
secrethub completion zsh > ~/.oh-my-zsh/custom/plugins/secrethub/_secrethub
# Add 'secrethub' to plugins in ~/.zshrc
source ~/.zshrc
```
## Usage Examples
### Basic Command Completion
```bash
$ secrethub <TAB>
login logout whoami secret policy agent config completion help version
$ secrethub sec<TAB>
secrethub secret
```
### Subcommand Completion
```bash
$ secrethub secret <TAB>
list get create update delete versions rollback
$ secrethub policy <TAB>
list get create update delete simulate templates
```
### Option Completion
```bash
$ secrethub --<TAB>
--help --version --server --format --quiet --verbose
$ secrethub --format <TAB>
json table yaml
```
### Dynamic Completion (requires authentication)
```bash
$ secrethub secret get <TAB>
prod.db.password
prod.db.username
dev.api.key
staging.redis.password
$ secrethub policy get <TAB>
developers
admins
read-only
business-hours
```
### Policy Template Completion
```bash
$ secrethub policy create --from-template <TAB>
admin read_only business_hours time_limited emergency_access
```
### Config Completion
```bash
$ secrethub config get <TAB>
server_url default_format log_level
$ secrethub config set default_format <TAB>
json table yaml
```
## Dynamic Completion Details
### How It Works
1. **Authentication Check**: Verifies `~/.secrethub/config.toml` exists
2. **API Query**: Calls SecretHub CLI with `--format json`
3. **Parse Response**: Extracts relevant fields (paths, names, IDs)
4. **Present Options**: Offers as completion candidates
### Supported Dynamic Completions
| Context | What's Completed | API Call |
|---------|------------------|----------|
| `secret get/update/delete <TAB>` | Secret paths | `secrethub secret list --format json` |
| `policy get/update/delete <TAB>` | Policy names | `secrethub policy list --format json` |
| `agent status/logs <TAB>` | Agent IDs | `secrethub agent list --format json` |
### Performance
- Dynamic completions query the server in real-time
- Falls back gracefully if server is unreachable
- Static completions (commands, options) always work
- No caching implemented (ensures fresh data)
## Testing Completion
### Quick Test
```bash
# Bash
secrethub sec<TAB>
# Should complete to: secrethub secret
# Zsh
secrethub sec<TAB>
# Should show: secret -- Manage secrets
```
### Full Test Suite
1. **Command completion**
```bash
secrethub <TAB>
```
2. **Subcommand completion**
```bash
secrethub secret <TAB>
```
3. **Option completion**
```bash
secrethub --format <TAB>
```
4. **Dynamic completion** (after `secrethub login`)
```bash
secrethub secret get <TAB>
```
5. **Policy template completion**
```bash
secrethub policy create --from-template <TAB>
```
## Development Workflow
### Generating Completions
```bash
# During development, use Mix task
cd apps/secrethub_cli
mix cli.gen.completion all
# Files generated in:
# - priv/completion/secrethub.bash
# - priv/completion/_secrethub
```
### Updating Completions
When adding new commands or options:
1. Update the completion module (`lib/secrethub_cli/completion.ex`)
2. Update both Bash and Zsh scripts in the module
3. Regenerate: `mix cli.gen.completion all`
4. Test with both shells
5. Update documentation
### Manual Updates
Completion scripts can also be manually edited:
**Bash**: `priv/completion/secrethub.bash`
**Zsh**: `priv/completion/_secrethub`
After editing, users need to reinstall:
```bash
secrethub completion bash > ~/.bash_completion.d/secrethub
source ~/.bashrc
```
## Best Practices
### For Users
1. **Keep completion updated**: Regenerate when CLI is updated
2. **Use dynamic completion**: Login to get server-side suggestions
3. **Clear cache**: If Zsh completion acts strangely, run `rm -f ~/.zcompdump*; compinit`
4. **Check PATH**: Ensure `secrethub` is in your PATH
### For Developers
1. **Test both shells**: Bash and Zsh have different syntax and behavior
2. **Handle errors gracefully**: Dynamic completion should fail silently
3. **Use descriptive text**: Especially important for Zsh completion
4. **Keep it fast**: Avoid slow operations in completion functions
5. **Document changes**: Update COMPLETION.md when adding features
## Troubleshooting
### Bash Completion Not Working
1. **Install bash-completion**
```bash
# Debian/Ubuntu
sudo apt install bash-completion
# RHEL/CentOS
sudo yum install bash-completion
# macOS
brew install bash-completion@2
```
2. **Source the script manually**
```bash
source ~/.bash_completion.d/secrethub
```
3. **Check for conflicts**
```bash
complete -p secrethub
# Should show: complete -F _secrethub secrethub
```
### Zsh Completion Not Working
1. **Check fpath**
```zsh
echo $fpath
# Should include completion directory
```
2. **Clear cache**
```zsh
rm -f ~/.zcompdump*
compinit
```
3. **Verify load order** (in .zshrc)
```zsh
# fpath MUST come before compinit
fpath=(~/.zsh/completion $fpath)
autoload -Uz compinit
compinit
```
### Dynamic Completion Not Working
1. **Verify authentication**
```bash
secrethub whoami
```
2. **Check config file**
```bash
cat ~/.secrethub/config.toml
```
3. **Test API access**
```bash
secrethub secret list --format json
```
## Future Enhancements
Potential improvements for future versions:
1. **Caching**: Cache API responses for better performance
2. **Fish shell**: Add Fish shell completion support
3. **PowerShell**: Add Windows PowerShell completion
4. **Smart caching**: Cache with TTL, invalidate on changes
5. **Offline mode**: Provide static completion when offline
6. **Custom completers**: Allow plugins to add custom completion
7. **Help integration**: Show inline help during completion
## Technical Details
### Bash Completion Function
- Uses `COMP_WORDS` and `COMP_CWORD` for context
- `COMPREPLY` array for suggestions
- `compgen` for filtering matches
- Context detection via command position
### Zsh Completion Function
- Uses `_arguments` for option parsing
- `_describe` for labeled completions
- State machine for subcommand handling
- More powerful syntax than Bash
### API Integration
Helper functions in completion scripts:
- `_secrethub_get_secrets()`: Fetch secret paths
- `_secrethub_get_policies()`: Fetch policy names
- `_secrethub_get_agents()`: Fetch agent IDs
- `_secrethub_get_policy_templates()`: Static template list
## Compatibility
### Tested With
- **Bash**: 4.0+ (required for associative arrays)
- **Zsh**: 5.0+ (modern completion system)
- **OS**: Linux, macOS
- **Terminals**: Any POSIX-compatible terminal
### Known Limitations
1. **Windows**: Requires WSL or Git Bash
2. **Old Bash**: Version 3.x not fully supported
3. **Network**: Dynamic completion needs server access
4. **Performance**: Large datasets may slow completion
## Resources
- **Bash Completion Guide**: https://github.com/scop/bash-completion
- **Zsh Completion Guide**: http://zsh.sourceforge.net/Doc/Release/Completion-System.html
- **CLI Documentation**: See README.md
- **Installation Guide**: See COMPLETION.md
## Support
For issues or questions:
1. Check COMPLETION.md for detailed troubleshooting
2. Verify SecretHub CLI version: `secrethub --version`
3. Check shell version: `bash --version` or `zsh --version`
4. Report bugs: https://github.com/secrethub/secrethub/issues
## License
Copyright © 2024 SecretHub. All rights reserved.