-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathswitch-env.sh
executable file
·69 lines (57 loc) · 1.72 KB
/
switch-env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
# Function to validate environment name
validate_env() {
if [[ ! "$1" =~ ^(local|production)$ ]]; then
echo "Error: Invalid environment. Please specify 'local' or 'production'."
exit 1
fi
}
# Function to check file existence
check_file() {
if [[ ! -f "$1" ]]; then
echo "Error: $1 does not exist."
exit 1
fi
}
# Function to validate environment file contents
validate_env_file() {
local file="$1"
local required_vars=("DJANGO_SETTINGS_MODULE" "SECRET_KEY")
for var in "${required_vars[@]}"; do
if ! grep -q "^${var}=" "$file"; then
echo "Error: Missing required variable $var in $file"
exit 1
fi
done
# Optional: Warn if DEBUG is not set in production
if [[ "$1" == "production" ]] && ! grep -q "^DEBUG=" "$file"; then
echo "Warning: DEBUG is not set in .env.production. Defaulting to False."
echo "DEBUG=False" >> "$file"
fi
}
# Main script
if [[ -z "$1" ]]; then
echo "Error: No environment specified. Please specify 'local' or 'production'."
exit 1
fi
validate_env "$1"
source_file=".env.$1"
check_file "$source_file"
# Validate the environment file contents
validate_env_file "$source_file"
# Backup existing .env if it exists
if [[ -f .env ]]; then
backup_file=".env.backup.$(date +%Y%m%d_%H%M%S)"
cp .env "$backup_file"
chmod 600 "$backup_file" # Restrict to owner read/write only
echo "Existing .env backed up to $backup_file"
fi
# Copy the specified environment file to .env
cp "$source_file" .env
# Verify the copy was successful
if [[ $? -eq 0 ]]; then
echo "Successfully switched to $1 environment."
else
echo "Error: Failed to switch environment."
exit 1
fi