forked from TracecatHQ/tracecat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env-migration.sh
executable file
·89 lines (77 loc) · 2.82 KB
/
env-migration.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
# SUMMARY
# -------
# This script migrates your existing .env file to match a new .env.template structure.
# Here's what it does:
#
# 1. Creates a timestamped backup of your current .env file.
# 2. Reads your existing .env file and the new .env.template.
# 3. Creates a new .env file based on the .env.template structure.
# 4. Preserves values from your old .env for keys that exist in the new template.
# 5. Adds new keys from the template (with empty values) if they didn't exist in your old .env.
# 6. Ignores keys from your old .env that don't exist in the new template.
#
# Note: This script will overwrite your current .env file, but a backup is always created first.
# You will be asked for confirmation before any changes are made.
# Function to read and parse .env file
parse_env() {
local file=$1
grep -v '^#' "$file" | grep '=' | sed 's/[[:space:]]*=[[:space:]]*/=/' | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//'
}
# Get current datetime
DATETIME=$(date "+%Y%m%d_%H%M%S")
# Paths to the files
OLD_ENV=".env"
NEW_TEMPLATE=".env.example"
OUTPUT_ENV=".env"
BACKUP_ENV=".env.backup_${DATETIME}"
# Check if .env file exists
if [ ! -f "$OLD_ENV" ]; then
echo "Error: $OLD_ENV file not found."
exit 1
fi
# Check if .env.template file exists
if [ ! -f "$NEW_TEMPLATE" ]; then
echo "Error: $NEW_TEMPLATE file not found."
exit 1
fi
# Prompt for backup
read -p "Do you want to create a backup of the current .env file? (y/n): " backup_choice
if [[ $backup_choice =~ ^[Yy]$ ]]; then
cp "$OLD_ENV" "$BACKUP_ENV"
echo "Backup created at $BACKUP_ENV"
fi
# Warning about overriding
echo "Warning: This script will override the current .env file."
read -p "Do you want to continue? (y/n): " continue_choice
if [[ ! $continue_choice =~ ^[Yy]$ ]]; then
echo "Operation cancelled."
exit 0
fi
# Temporary file for processing
TEMP_FILE=$(mktemp)
# Process the new template and create the output
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ $line =~ ^[[:space:]]*$ || $line =~ ^# ]]; then
# Empty line or comment, copy as is
echo "$line" >> "$TEMP_FILE"
elif [[ $line =~ ^[[:space:]]*([^=]+)[[:space:]]*= ]]; then
# Extract key
key=${BASH_REMATCH[1]}
# Look for the key in the old .env file
old_value=$(grep "^${key}=" "$OLD_ENV" | sed "s/^${key}=//")
if [[ -n "$old_value" ]]; then
# Key exists in old .env, use its value
echo "${key}=${old_value}" >> "$TEMP_FILE"
else
# Key doesn't exist in old .env, copy line as is
echo "$line" >> "$TEMP_FILE"
fi
else
# Any other line, copy as is
echo "$line" >> "$TEMP_FILE"
fi
done < "$NEW_TEMPLATE"
# Move the temporary file to the final output
mv "$TEMP_FILE" "$OUTPUT_ENV"
echo "Migration completed. New .env file created"