A modern, extensible command-line interface for ChatGPT with subcommand support, configuration management, and comprehensive logging.
# 1. Set your API key
export OPENAI_API_KEY="sk-your-api-key-here"
# 2. Build
go build -o chatgpt-cli main.go
# 3. Use it!
chatgpt-cli prompt "Explain Go channels"chatgpt-cli <command> [arguments]chatgpt-cli helpDisplays usage information and all available commands.
chatgpt-cli prompt "your prompt here"Send a prompt to ChatGPT and get a response.
Examples:
chatgpt-cli prompt "What is Go?"
chatgpt-cli prompt "Write a function to reverse a string"
chatgpt-cli prompt "Explain async/await in JavaScript"chatgpt-cli logsDisplay all stored application logs including prompts, responses, and errors.
Example Output:
Showing 2 log entries:
[1] 2024-01-31 14:30:15 - prompt
Prompt: What is Go?
Response: Go is a statically typed, compiled programming language...
[2] 2024-01-31 14:35:22 - prompt
Prompt: Explain channels
Response: Channels in Go are a typed conduit through which you can send...
List all configuration:
chatgpt-cli config listGet a specific value:
chatgpt-cli config get OPENAI_MODEL
chatgpt-cli config get OPENAI_API_URLSet a configuration value:
chatgpt-cli config set OPENAI_MODEL gpt-4
chatgpt-cli config set OPENAI_MAX_TOKENS 2000
chatgpt-cli config set OPENAI_TEMPERATURE 1.5All configuration is managed via environment variables:
| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY |
Your OpenAI API key | (required) |
OPENAI_API_URL |
API endpoint URL | https://api.openai.com/v1/chat/completions |
OPENAI_MODEL |
Model to use | gpt-3.5-turbo |
OPENAI_TIMEOUT |
Request timeout | 60s |
OPENAI_MAX_TOKENS |
Max tokens in response | 1000 |
OPENAI_TEMPERATURE |
Response randomness (0.0-2.0) | 0.7 |
CHATGPT_CLI_CONFIG_DIR |
Config directory | ~/.chatgpt-cli |
For current session:
export OPENAI_MODEL="gpt-4"
export OPENAI_MAX_TOKENS="2000"Permanently (add to ~/.bashrc or ~/.zshrc):
echo 'export OPENAI_API_KEY="sk-your-key"' >> ~/.bashrc
echo 'export OPENAI_MODEL="gpt-4"' >> ~/.bashrc
source ~/.bashrcYou can also set configuration at runtime (session only):
chatgpt-cli config set OPENAI_MODEL gpt-4
chatgpt-cli prompt "Now using GPT-4!"Note: Runtime config changes are not persisted. To make them permanent, add them to your shell profile.
- Config Directory:
~/.chatgpt-cli/(or$CHATGPT_CLI_CONFIG_DIR) - Logs:
~/.chatgpt-cli/logs.jsonl
Run the comprehensive test suite:
# Run all tests
go test -v
# Run tests with coverage
go test -v -cover
# Generate coverage report
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
# Run specific test
go test -v -run TestConfigCommand
# Run benchmarks
go test -bench=.Adding a new command is straightforward:
- Create the command handler function:
func myNewCommand(config *Config, args []string) error {
// Your command logic here
fmt.Println("Executing my new command!")
return nil
}- Register the command in
getCommands():
func getCommands() map[string]Command {
return map[string]Command{
// ... existing commands ...
"mynew": {
Name: "mynew",
Description: "My new command",
Handler: myNewCommand,
},
}
}-
Update help text in
helpCommand()to document your new command. -
Write tests in
main_test.go.
That's it! Your command is now available:
chatgpt-cli mynewchatgpt-cli/
βββ main.go # Main application (~700 lines)
βββ main_test.go # Comprehensive tests (~650 lines)
βββ go.mod # Go module file
βββ README.md # This file
βββ Makefile # Build automation
βββ .gitignore # Git ignore rules
The code is organized into logical sections:
- Constants & Types: Configuration and data structures
- Configuration Loading: Environment variable parsing
- Command Handlers: Individual command implementations
- API Communication: HTTP request handling
- Logging: Event logging system
- Command Registry: Command registration and routing
- Main Function: Entry point
User Input
β
parseCommand() β Extracts command name and args
β
getCommands() β Retrieves command registry
β
Command.Handler() β Executes appropriate handler
β
Result/Error
Environment Variables
β
loadConfig() β Parse with defaults
β
Config struct β Passed to all commands
β
Runtime modifications (config set) β Update env vars
# Get help
chatgpt-cli help
# Ask a question
chatgpt-cli prompt "What are Go interfaces?"
# View logs
chatgpt-cli logs
# Check configuration
chatgpt-cli config list# Use GPT-4 for a single query
OPENAI_MODEL=gpt-4 chatgpt-cli prompt "Explain quantum computing"
# Set longer timeout for complex queries
OPENAI_TIMEOUT=120s chatgpt-cli prompt "Write a complete REST API server"
# Save response to file
chatgpt-cli prompt "Write a README for my project" > README.md
# Use in a script
RESPONSE=$(chatgpt-cli prompt "Generate a git commit message")
git commit -m "$RESPONSE"# View current configuration
chatgpt-cli config list
# Get specific value
MODEL=$(chatgpt-cli config get OPENAI_MODEL)
echo "Current model: $MODEL"
# Change model for current session
chatgpt-cli config set OPENAI_MODEL gpt-4
chatgpt-cli prompt "Now using GPT-4"
# Set multiple values
chatgpt-cli config set OPENAI_MAX_TOKENS 2000
chatgpt-cli config set OPENAI_TEMPERATURE 1.2- API Key Storage: Never commit your API key to version control
- Environment Variables: Use environment variables for sensitive data
- Session-only Config: Runtime config changes don't persist to disk
- Masked Display: API key is masked in
config listoutput - HTTPS: All API communication is encrypted
# Make sure it's set
echo $OPENAI_API_KEY
# If not set
export OPENAI_API_KEY="sk-your-key"# Check available commands
chatgpt-cli help
# Make sure you're using the right syntax
chatgpt-cli prompt "text" # Correct
chatgpt-cli "text" # Incorrect (missing 'prompt' command)# Increase timeout
export OPENAI_TIMEOUT="120s"
# or
chatgpt-cli config set OPENAI_TIMEOUT 120sIf you encounter rate limit errors, wait a few moments before trying again. Check your usage at https://platform.openai.com/account/usage
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass (
go test -v) - Submit a pull request
MIT License - feel free to use and modify as needed.
- Built with Go's excellent standard library
- Powered by OpenAI's ChatGPT API
- Inspired by modern CLI design patterns
Need help? Run chatgpt-cli help or open an issue on GitHub.