cmdgen
v0.0.6
Published
AI-powered CLI tool that generates and executes commands using OpenAI
Readme
CMDGen 🤖
An AI-powered CLI tool that generates and executes commands using OpenAI. Simply describe what you want to do, and CMDGen will generate the appropriate command for you.
Features
- 🤖 AI-powered command generation using OpenAI GPT-3.5-turbo
- ⚡ Interactive and one-shot modes for flexible usage
- 🔄 Command regeneration if the first attempt isn't quite right
- 🛡️ Built-in safety checks for potentially dangerous operations
- 💾 Secure API key storage in
~/.cmdgen/config.json - 🎨 Beautiful CLI interface with colors and spinners
- ⏱️ 30-second command timeout protection
- 🔍 Command validation and verification
- 💻 OS-aware command generation
Installation
Prerequisites
- Node.js (v16 or higher)
- npm or yarn
- OpenAI API key (get one from OpenAI Platform)
Install globally
Option 1: Using npm (recommended)
npm install -g cmdgenOption 2: Clone and install locally
git clone <repository-url>
cd cmdgen
npm install
npm linkDevelopment
git clone <repository-url>
cd cmdgen
npm install
npm startUpdating
To update to the latest version:
npm install -g cmdgen-cli@latestQuick Start
Initialize CMDGen with your OpenAI API key:
cmdgen initUse CMDGen in multiple ways:
Interactive mode:
cmdgenThen describe what you want to do:
prompt a command > ffmpeg command to crop video input.mp4 1080px x 1080pxOne-shot mode:
cmdgen "compress all jpg files in current directory"Enhanced interactive mode:
cmdgen startThis mode includes better validation and command verification.
Review and execute the generated command:
Executing command: find . -name "*.jpg" -exec jpegoptim --strip-all {} \; Press Enter to run, 'c' to cancel, 'r' to regenerate
Usage
Commands
cmdgen init- Set up your OpenAI API keycmdgen start- Start enhanced interactive modecmdgen --help- Show help informationcmdgen --version- Show version information
Modes
Interactive Mode:
When you run cmdgen without arguments, you'll enter an interactive session where you can:
- Describe your command in natural language
- Review the generated command before execution
- Execute the command by pressing Enter
- Cancel the command by typing 'c'
- Regenerate a new command by typing 'r'
- Exit the session by typing 'exit'
Enhanced Interactive Mode (cmdgen start):
This mode includes additional features:
- Command validation: Automatic verification of generated commands
- Better error handling: More detailed feedback on command generation
- Improved user experience: Streamlined command generation flow
One-shot Mode:
When you run cmdgen "your prompt here", it will:
- Generate the command based on your prompt
- Show the generated command
- Ask for confirmation before execution
- Execute the command after you confirm
Examples
Interactive Mode:
prompt a command > compress all jpg files in current directory
> find . -name "*.jpg" -exec jpegoptim --strip-all {} \;
prompt a command > create a backup of my documents folder
> tar -czf documents_backup_$(date +%Y%m%d).tar.gz ~/Documents
prompt a command > find all files larger than 100MB
> find . -type f -size +100M -exec ls -lh {} \;
prompt a command > convert all png files to jpg
> for file in *.png; do convert "$file" "${file%.png}.jpg"; doneOne-shot Mode:
cmdgen "compress all jpg files in current directory"
# Output: find . -name "*.jpg" -exec jpegoptim --strip-all {} \;
cmdgen "create a backup of my documents folder"
# Output: tar -czf documents_backup_$(date +%Y%m%d).tar.gz ~/Documents
cmdgen "find all files larger than 100MB"
# Output: find . -type f -size +100M -exec ls -lh {} \;Configuration
CMDGen stores its configuration in ~/.cmdgen/config.json. This includes:
- Your OpenAI API key (stored securely)
- Configuration is automatically created on first run
- OS detection for platform-specific commands
Safety Features
- Command validation: CMDGen validates API keys and command syntax
- Command verification: Automatic verification that generated text is actually a command
- Execution confirmation: You must confirm before any command runs (Enter to run, 'c' to cancel, 'r' to regenerate)
- Timeout protection: Commands timeout after 30 seconds
- Error handling: Comprehensive error messages and graceful failure handling
- Dangerous operation detection: AI model is trained to detect potentially harmful commands
- Safe defaults: File operations use safe defaults and confirm destructive operations
- OS-aware generation: Commands are generated specifically for your operating system
Requirements
- OpenAI API Key: Get one from OpenAI Platform
- Node.js: Version 16 or higher
- Internet Connection: Required for AI command generation
- System Tools: Commands generated may require specific tools (ffmpeg, imagemagick, etc.)
Troubleshooting
Common Issues
"OpenAI API key not found"
- Run
cmdgen initto set up your API key - Ensure the API key starts with 'sk-'
- Run
"Failed to generate command"
- Check your internet connection
- Verify your OpenAI API key is valid
- Ensure you have sufficient API credits
- Check if the request is clear and not potentially dangerous
"Command execution failed"
- Check if the required tools are installed on your system
- Verify file paths and permissions
- Review the error output for specific issues
- Commands timeout after 30 seconds
"Request unclear or potentially dangerous"
- Rephrase your request to be more specific
- Avoid requests that could harm your system
"Generated text doesn't appear to be a valid command"
- Try rephrasing your request more clearly
- Be more specific about what you want to accomplish
- Use technical terms instead of casual language
Getting Help
- Run
cmdgen --helpfor command-line help - Check the error messages for specific guidance
- Ensure all prerequisites are installed
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the ISC License.
Disclaimer
This tool executes commands on your system. Always review generated commands before execution, especially for operations that could affect your files or system. The authors are not responsible for any damage caused by the use of this tool.
Technical Details
- Model: OpenAI GPT-3.5-turbo
- Max Tokens: 200 for command generation
- Temperature: 0.1 for consistent results
- Timeout: 30 seconds for command execution
- Configuration: Stored in
~/.cmdgen/config.json - OS Detection: Automatically detects your operating system for platform-specific commands
- Command Verification: Built-in validation to ensure generated text is actually a command
