Standardize email-diagnostics.sh menu formatting and add input validation

IMPROVEMENTS:
- Added input validation for check type (1-2) with retry loop
- Added input validation for time period (1-5) with retry loop
- Added email format validation (user@domain.com pattern)
- Added domain format validation (example.com pattern)
- Added color codes to menu options (${CYAN}1)${NC} format)
- Improved error messages for invalid input

VALIDATION DETAILS:
- Check type: Only accepts 1 or 2, rejects invalid input with clear error
- Time period: Only accepts 1-5, rejects invalid input with clear error
- Email format: Validates user@domain.com pattern
- Domain format: Validates domain.com pattern (alphanumeric, dots, hyphens)
- All inputs with defaults continue to work seamlessly

MENU STANDARDS COMPLIANCE:
✓ Input validation (CRITICAL)
✓ Default values (already had)
✓ Color codes (CRITICAL)
✓ Error messages on invalid input (IMPORTANT)
✓ Retry logic for failed validation (IMPORTANT)

Lines modified: ~60 (input validation + color codes)

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
cschantz
2026-02-11 20:53:26 -05:00
parent fc6ce7f6d7
commit 52821a795e
+73 -24
View File
@@ -27,40 +27,90 @@ echo ""
# Ask what to check
echo -e "${BOLD}What would you like to check?${NC}"
echo ""
echo " 1) Specific email address (e.g., user@example.com)"
echo " 2) Entire domain (e.g., example.com)"
echo -e " ${CYAN}1)${NC} Specific email address (e.g., user@example.com)"
echo -e " ${CYAN}2)${NC} Entire domain (e.g., example.com)"
echo ""
read -p "Enter choice [1]: " check_type
check_type=${check_type:-1}
# Validate check_type input
while true; do
read -p "Enter choice [1]: " check_type
check_type=${check_type:-1}
if ! [[ "$check_type" =~ ^[1-2]$ ]]; then
print_error "Invalid choice. Please enter 1 or 2"
continue
fi
break
done
# Get email/domain to check
echo ""
if [ "$check_type" = "2" ]; then
read -p "Enter domain to check (e.g., example.com): " target
search_pattern="@${target}"
check_label="domain $target"
else
read -p "Enter email address to check: " target
search_pattern="$target"
check_label="email $target"
fi
if [ -z "$target" ]; then
print_error "No email/domain provided"
exit 1
if [ "$check_type" = "2" ]; then
# Domain input with validation
while true; do
read -p "Enter domain to check (e.g., example.com): " target
# Validate domain format (basic check)
if [ -z "$target" ]; then
print_error "Domain cannot be empty"
continue
fi
# Check for invalid characters (allow alphanumeric, dots, hyphens)
if ! [[ "$target" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
print_error "Invalid domain format. Use format like: example.com"
continue
fi
search_pattern="@${target}"
check_label="domain $target"
break
done
else
# Email address input with validation
while true; do
read -p "Enter email address to check: " target
# Validate email format (basic check)
if [ -z "$target" ]; then
print_error "Email address cannot be empty"
continue
fi
# Check for valid email format (user@domain.com)
if ! [[ "$target" =~ ^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
print_error "Invalid email format. Use format like: user@example.com"
continue
fi
search_pattern="$target"
check_label="email $target"
break
done
fi
# Time period to check
echo ""
echo "Check logs from:"
echo " 1) Last 1 hour"
echo " 2) Last 6 hours"
echo " 3) Last 24 hours (recommended)"
echo " 4) Last 48 hours"
echo " 5) Last week"
echo -e " ${CYAN}1)${NC} Last 1 hour"
echo -e " ${CYAN}2)${NC} Last 6 hours"
echo -e " ${CYAN}3)${NC} Last 24 hours (recommended)"
echo -e " ${CYAN}4)${NC} Last 48 hours"
echo -e " ${CYAN}5)${NC} Last week"
echo ""
read -p "Enter choice [3]: " time_choice
time_choice=${time_choice:-3}
# Validate time_choice input
while true; do
read -p "Enter choice [3]: " time_choice
time_choice=${time_choice:-3}
if ! [[ "$time_choice" =~ ^[1-5]$ ]]; then
print_error "Invalid choice. Please enter 1-5"
continue
fi
break
done
case "$time_choice" in
1) hours=1 ;;
@@ -68,7 +118,6 @@ case "$time_choice" in
3) hours=24 ;;
4) hours=48 ;;
5) hours=168 ;;
*) hours=24 ;;
esac
echo ""