Skip to content

Commit

Permalink
chore(feat): add fix to switch-env
Browse files Browse the repository at this point in the history
  • Loading branch information
ndu committed Dec 27, 2024
1 parent 7a9134a commit 7c2f380
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
66 changes: 58 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,66 @@ Before running the application, ensure you have the following:
5. Environment Switching: Use this script (switch-env.sh) to easily switch between environments:
```sh
#!/bin/bash
if [ "$1" == "local" ]; then
cp .env.local .env
echo "Switched to local environment"
elif [ "$1" == "prod" ]; then
cp .env.production .env
echo "Switched to production environment"

validate_env() {
if [[ ! "$1" =~ ^(local|production)$ ]]; then
echo "Error: Invalid environment. Please specify 'local' or 'production'."
exit 1
fi
}

check_file() {
if [[ ! -f "$1" ]]; then
echo "Error: $1 does not exist."
exit 1
fi
}

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

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
}

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_env_file "$source_file"

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

cp "$source_file" .env

if [[ $? -eq 0 ]]; then
echo "Successfully switched to $1 environment."
else
echo "Please specify environment: local or prod"
echo "Error: Failed to switch environment."
exit 1
fi
```
```
Make it executable and use:

```sh
Expand Down
1 change: 1 addition & 0 deletions switch-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ validate_env_file "$source_file"
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

Expand Down

0 comments on commit 7c2f380

Please sign in to comment.