Skip to content

Commit cfa5e7e

Browse files
authored
feat: Add env-migration script (TracecatHQ#315)
1 parent 93974ea commit cfa5e7e

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

env-migration.sh

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
3+
# SUMMARY
4+
# -------
5+
# This script migrates your existing .env file to match a new .env.template structure.
6+
# Here's what it does:
7+
#
8+
# 1. Creates a timestamped backup of your current .env file.
9+
# 2. Reads your existing .env file and the new .env.template.
10+
# 3. Creates a new .env file based on the .env.template structure.
11+
# 4. Preserves values from your old .env for keys that exist in the new template.
12+
# 5. Adds new keys from the template (with empty values) if they didn't exist in your old .env.
13+
# 6. Ignores keys from your old .env that don't exist in the new template.
14+
#
15+
# Note: This script will overwrite your current .env file, but a backup is always created first.
16+
# You will be asked for confirmation before any changes are made.
17+
18+
# Function to read and parse .env file
19+
parse_env() {
20+
local file=$1
21+
grep -v '^#' "$file" | grep '=' | sed 's/[[:space:]]*=[[:space:]]*/=/' | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//'
22+
}
23+
24+
# Get current datetime
25+
DATETIME=$(date "+%Y%m%d_%H%M%S")
26+
27+
# Paths to the files
28+
OLD_ENV=".env"
29+
NEW_TEMPLATE=".env.example"
30+
OUTPUT_ENV=".env"
31+
BACKUP_ENV=".env.backup_${DATETIME}"
32+
33+
# Check if .env file exists
34+
if [ ! -f "$OLD_ENV" ]; then
35+
echo "Error: $OLD_ENV file not found."
36+
exit 1
37+
fi
38+
39+
# Check if .env.template file exists
40+
if [ ! -f "$NEW_TEMPLATE" ]; then
41+
echo "Error: $NEW_TEMPLATE file not found."
42+
exit 1
43+
fi
44+
45+
# Prompt for backup
46+
read -p "Do you want to create a backup of the current .env file? (y/n): " backup_choice
47+
if [[ $backup_choice =~ ^[Yy]$ ]]; then
48+
cp "$OLD_ENV" "$BACKUP_ENV"
49+
echo "Backup created at $BACKUP_ENV"
50+
fi
51+
52+
# Warning about overriding
53+
echo "Warning: This script will override the current .env file."
54+
read -p "Do you want to continue? (y/n): " continue_choice
55+
if [[ ! $continue_choice =~ ^[Yy]$ ]]; then
56+
echo "Operation cancelled."
57+
exit 0
58+
fi
59+
60+
# Temporary file for processing
61+
TEMP_FILE=$(mktemp)
62+
63+
# Process the new template and create the output
64+
while IFS= read -r line || [[ -n "$line" ]]; do
65+
if [[ $line =~ ^[[:space:]]*$ || $line =~ ^# ]]; then
66+
# Empty line or comment, copy as is
67+
echo "$line" >> "$TEMP_FILE"
68+
elif [[ $line =~ ^[[:space:]]*([^=]+)[[:space:]]*= ]]; then
69+
# Extract key
70+
key=${BASH_REMATCH[1]}
71+
# Look for the key in the old .env file
72+
old_value=$(grep "^${key}=" "$OLD_ENV" | sed "s/^${key}=//")
73+
if [[ -n "$old_value" ]]; then
74+
# Key exists in old .env, use its value
75+
echo "${key}=${old_value}" >> "$TEMP_FILE"
76+
else
77+
# Key doesn't exist in old .env, copy line as is
78+
echo "$line" >> "$TEMP_FILE"
79+
fi
80+
else
81+
# Any other line, copy as is
82+
echo "$line" >> "$TEMP_FILE"
83+
fi
84+
done < "$NEW_TEMPLATE"
85+
86+
# Move the temporary file to the final output
87+
mv "$TEMP_FILE" "$OUTPUT_ENV"
88+
89+
echo "Migration completed. New .env file created"

0 commit comments

Comments
 (0)