@@ -58,6 +58,43 @@ while IFS= read -r line || [ -n "$line" ]; do
5858 fi
5959done < " $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+
6198if [ ${# 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