Multi-Account Management
```bash
#!/bin/bash
# Switch between multiple accounts
switch_gemini_account() {
local account=$1
case $account in
personal)
unset GEMINI_API_KEY
unset GOOGLE_APPLICATION_CREDENTIALS
gemini auth logout
gemini # Trigger OAuth
;;
work)
export GEMINI_API_KEY="$(pass show gemini/work-api-key)"
unset GOOGLE_APPLICATION_CREDENTIALS
;;
enterprise)
unset GEMINI_API_KEY
export GOOGLE_CLOUD_PROJECT="company-project"
export GOOGLE_APPLICATION_CREDENTIALS="~/keys/company-sa.json"
;;
*)
echo "Unknown account: $account"
echo "Available: personal, work, enterprise"
return 1
;;
esac
echo "Switched to $account account"
# Auto-validate authentication with YOLO mode
gemini --yolo -p "Test authentication and report current auth method and quota status"
}
# Automated account testing
test_all_accounts() {
for account in personal work enterprise; do
echo "Testing $account account..."
switch_gemini_account "$account"
gemini --yolo -p "Quick test: what is 2+2? Also report account type and remaining quota."
done
}
# Usage
switch_gemini_account personal
```
Secure API Key Storage
```bash
#!/bin/bash
# Secure API key management with pass
# Install pass (password store)
sudo apt-get install pass # Debian/Ubuntu
brew install pass # macOS
# Initialize pass
gpg --gen-key
pass init your-email@example.com
# Store API key securely
pass insert gemini/api-key
# Use in scripts
export GEMINI_API_KEY="$(pass show gemini/api-key)"
# Or with keychain (macOS)
security add-generic-password \
-a "$USER" \
-s "gemini-api-key" \
-w "your-api-key-here"
# Retrieve from keychain
export GEMINI_API_KEY="$(security find-generic-password -s 'gemini-api-key' -w)"
```
Rate Limit Management
```bash
#!/bin/bash
# Handle rate limits gracefully
gemini_with_retry() {
local prompt="$1"
local use_yolo="${2:-false}"
local max_retries=3
local retry_delay=60
local yolo_flag=""
if [ "$use_yolo" = "true" ]; then
yolo_flag="--yolo"
fi
for i in $(seq 1 $max_retries); do
if gemini $yolo_flag -p "$prompt"; then
return 0
else
if [ $i -lt $max_retries ]; then
echo "Rate limited. Waiting ${retry_delay}s before retry $((i+1))/${max_retries}..."
sleep $retry_delay
retry_delay=$((retry_delay * 2)) # Exponential backoff
fi
fi
done
echo "Failed after $max_retries retries"
return 1
}
# YOLO-enabled retry for automated workflows
gemini_yolo_retry() {
local prompt="$1"
gemini_with_retry "$prompt" true
}
# Track usage
track_gemini_usage() {
local log_file="~/.gemini/usage.log"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "$timestamp - Request made" >> "$log_file"
# Count today's requests
local today=$(date '+%Y-%m-%d')
local count=$(grep "$today" "$log_file" | wc -l)
echo "Requests today: $count/1000"
if [ $count -ge 950 ]; then
echo "WARNING: Approaching daily limit!"
fi
}
```