Files
mtusi2026lab3/scripts/init-env.sh
T

32 lines
994 B
Bash
Executable File

#!/bin/sh
# Create a local .env with fresh synthetic passwords. The file is ignored by
# Git, is readable only by the current user, and its values are never printed.
set -eu
cd "$(dirname "$0")/.."
if [ -f .env ]; then
echo ".env already exists; refusing to overwrite it" >&2
exit 0
fi
random_secret() {
LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 32
}
postgres_password=$(random_secret)
valkey_password=$(random_secret)
if [ "${#postgres_password}" -ne 32 ] || [ "${#valkey_password}" -ne 32 ]; then
echo "could not generate local secrets" >&2
exit 1
fi
revision=$(git rev-parse HEAD 2>/dev/null || echo unknown)
umask 077
sed \
-e "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=${postgres_password}|" \
-e "s|^VALKEY_PASSWORD=.*|VALKEY_PASSWORD=${valkey_password}|" \
-e "s|^GIT_REVISION=.*|GIT_REVISION=${revision}|" \
.env.example > .env
chmod 600 .env
echo "wrote .env with generated local synthetic credentials (mode 600); values not printed"