Skip to content

Commit c14d492

Browse files
committed
feat: Add error handling for missing .env file and required variables
1 parent 5e2cd35 commit c14d492

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

scripts/common.bash

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ source scripts/languages.bash
44

55
# Load environment variables from .env file
66
function load_env_vars() {
7-
if [ -f .env ]; then
8-
# shellcheck disable=SC2046
9-
export echo $(sed <.env 's/#.*//g' | xargs | envsubst)
10-
11-
# Check if required variables are set and not equal to default values
12-
if [ "$NICKNAME" = "your_nickname" ] || [ "$LANGUAGE" = "choose_your_language" ]; then
13-
echo "Error: Required environment variables are set to default values."
14-
echo "Please update NICKNAME and LANGUAGE in the .env file with appropriate values."
15-
exit 1
16-
fi
7+
if [ ! -f .env ]; then
8+
echo "Error: .env file not found."
9+
exit 1
10+
fi
1711

18-
# Check if the specified language is valid
12+
# shellcheck disable=SC2046
13+
export $(sed <.env 's/#.*//g' | xargs | envsubst)
1914

20-
if [[ ! "${!language_extensions[@]}" =~ $LANGUAGE ]]; then
21-
echo "Error: Invalid language specified in the .env file."
22-
echo "Please set LANGUAGE to one of the following valid languages:"
23-
echo "${!language_extensions[@]}"
24-
exit 1
25-
fi
15+
# Check if NICKNAME and LANGUAGE variables are set
16+
if [ -z "$NICKNAME" ] || [ -z "$LANGUAGE" ]; then
17+
echo "Error: Required environment variables NICKNAME and/or LANGUAGE are not set."
18+
echo "Please set NICKNAME and LANGUAGE in the .env file with appropriate values."
19+
exit 1
20+
fi
21+
22+
# Check if the specified language is valid
23+
if [[ ! "${!language_extensions[@]}" =~ $LANGUAGE ]]; then
24+
echo "Error: Invalid language specified in the .env file."
25+
echo "Please set LANGUAGE to one of the following valid languages:"
26+
echo "${!language_extensions[@]}"
27+
exit 1
2628
fi
2729
}
2830

0 commit comments

Comments
 (0)