Skip to content

Commit f50c27d

Browse files
feat: Add a check for monogodb uri corruption (#19)
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
1 parent c9aaf5e commit f50c27d

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

on-prem/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ CLICKHOUSE_USERNAME=currents
8484
# MongoDB
8585
MONGODB_USERNAME=currents-user
8686
MONGODB_DATABASE=currents
87+
# NOTE: Long line - if editing in nano, use `nano -w .env` to prevent line wrapping
8788
MONGODB_URI=mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@mongodb:27017/${MONGODB_DATABASE}?authSource=admin&replicaSet=rs0
8889

8990
# Domain configuration for Traefik routing

on-prem/scripts/check-env.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,43 @@ while IFS= read -r line || [ -n "$line" ]; do
5858
fi
5959
done < "$EXAMPLE_FILE"
6060

61+
# =============================================================================
62+
# Check for corrupted values (e.g., line wrapping from terminal editors)
63+
# =============================================================================
64+
CORRUPTED=()
65+
66+
# Check MONGODB_URI for spaces (indicates line wrapping corruption)
67+
if grep -q "^MONGODB_URI=" "$ENV_FILE"; then
68+
mongodb_uri=$(grep "^MONGODB_URI=" "$ENV_FILE" | cut -d'=' -f2-)
69+
if [[ "$mongodb_uri" =~ [[:space:]] ]]; then
70+
CORRUPTED+=("MONGODB_URI contains spaces - likely corrupted by editor line wrapping")
71+
fi
72+
# Check if it looks like it was split across lines (missing expected parts)
73+
if [[ ! "$mongodb_uri" =~ ^mongodb(\+srv)?:\/\/ ]] || [[ ! "$mongodb_uri" =~ "@" ]]; then
74+
CORRUPTED+=("MONGODB_URI appears incomplete - may have been split by line wrapping")
75+
fi
76+
fi
77+
78+
# Check for orphan lines that look like continuation of MONGODB_URI
79+
if grep -qE "^(authSource|replicaSet|mongodb:|27017)" "$ENV_FILE"; then
80+
CORRUPTED+=("Found orphan lines that may be fragments of a wrapped MONGODB_URI")
81+
fi
82+
83+
if [ ${#CORRUPTED[@]} -gt 0 ]; then
84+
echo -e "${RED}Corrupted values detected:${NC}"
85+
for msg in "${CORRUPTED[@]}"; do
86+
echo -e " ${RED}!${NC} $msg"
87+
done
88+
echo ""
89+
echo -e "${YELLOW}This usually happens when editing .env with a terminal editor that wraps long lines.${NC}"
90+
echo "To fix:"
91+
echo " 1. Delete the corrupted lines from .env"
92+
echo " 2. Re-edit using: nano -w .env (the -w flag disables line wrapping)"
93+
echo " 3. Or copy the original from .env.example: grep 'MONGODB_URI' .env.example"
94+
echo ""
95+
exit 1
96+
fi
97+
6198
if [ ${#MISSING[@]} -eq 0 ] && [ ${#COMMENTED[@]} -eq 0 ]; then
6299
echo -e "${GREEN}All environment variables are present in .env${NC}"
63100
exit 0

0 commit comments

Comments
 (0)