feat(deploy): target .mam.env and add --migrate-legacy flag
This commit is contained in:
+33
-11
@@ -1,33 +1,54 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# generate-env.sh — create a local .env from the committed .env.example template.
|
# generate-env.sh — create a local .mam.env from the committed .mam.env.example template.
|
||||||
#
|
#
|
||||||
# Behaviour:
|
# Behaviour:
|
||||||
# - .env absent → copy .env.example to .env, print the path.
|
# - .mam.env absent → copy .mam.env.example to .mam.env, print the path.
|
||||||
# - .env present → no-op (leaves your edits intact), exit 0.
|
# - .mam.env present → no-op (leaves your edits intact), exit 0.
|
||||||
# - .env present --force → overwrite .env from .env.example (backs up to .env.bak).
|
# - .mam.env present --force → overwrite .mam.env from .mam.env.example (backs up to .mam.env.bak).
|
||||||
|
# - --migrate-legacy → explicitly rename existing .env to .mam.env if absent.
|
||||||
#
|
#
|
||||||
# Paths are resolved relative to this script (repo root = parent of deploy/),
|
# Paths are resolved relative to this script (repo root = parent of deploy/),
|
||||||
# so it works regardless of the caller's cwd.
|
# so it works regardless of the caller's cwd.
|
||||||
#
|
#
|
||||||
# Usage: deploy/generate-env.sh [--force] [-h|--help]
|
# Usage: deploy/generate-env.sh [--force] [--migrate-legacy] [-h|--help]
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||||
SRC="$REPO_ROOT/.env.example"
|
SRC="$REPO_ROOT/.mam.env.example"
|
||||||
DST="$REPO_ROOT/.env"
|
DST="$REPO_ROOT/.mam.env"
|
||||||
|
LEGACY_ENV="$REPO_ROOT/.env"
|
||||||
|
|
||||||
FORCE=0
|
FORCE=0
|
||||||
|
MIGRATE=0
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
--force) FORCE=1; shift ;;
|
--force) FORCE=1; shift ;;
|
||||||
|
--migrate-legacy) MIGRATE=1; shift ;;
|
||||||
-h|--help)
|
-h|--help)
|
||||||
echo "Usage: $0 [--force]"
|
echo "Usage: $0 [--force] [--migrate-legacy]"
|
||||||
echo " Create .env from .env.example. --force overwrites an existing .env."
|
echo " Create .mam.env from .mam.env.example."
|
||||||
|
echo " --force overwrites an existing .mam.env (backs up to .mam.env.bak)."
|
||||||
|
echo " --migrate-legacy explicitly renames an existing legacy .env to .mam.env."
|
||||||
exit 0 ;;
|
exit 0 ;;
|
||||||
*) echo "ERROR: unknown arg: $1" >&2; echo "Usage: $0 [--force]" >&2; exit 2 ;;
|
*) echo "ERROR: unknown arg: $1" >&2; echo "Usage: $0 [--force] [--migrate-legacy]" >&2; exit 2 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ "$MIGRATE" = "1" ]; then
|
||||||
|
if [ -f "$DST" ]; then
|
||||||
|
echo "ERROR: cannot migrate: $DST already exists." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ ! -f "$LEGACY_ENV" ]; then
|
||||||
|
echo "ERROR: cannot migrate: legacy file $LEGACY_ENV not found." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
mv -f "$LEGACY_ENV" "$DST"
|
||||||
|
chmod 0600 "$DST" 2>/dev/null || true
|
||||||
|
echo "migrated: $LEGACY_ENV -> $DST"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
[ -f "$SRC" ] || { echo "ERROR: template not found: $SRC" >&2; exit 1; }
|
[ -f "$SRC" ] || { echo "ERROR: template not found: $SRC" >&2; exit 1; }
|
||||||
|
|
||||||
if [ -f "$DST" ] && [ "$FORCE" != "1" ]; then
|
if [ -f "$DST" ] && [ "$FORCE" != "1" ]; then
|
||||||
@@ -37,9 +58,10 @@ fi
|
|||||||
|
|
||||||
if [ -f "$DST" ] && [ "$FORCE" = "1" ]; then
|
if [ -f "$DST" ] && [ "$FORCE" = "1" ]; then
|
||||||
cp -p "$DST" "$DST.bak"
|
cp -p "$DST" "$DST.bak"
|
||||||
echo "backed up existing .env -> $DST.bak"
|
echo "backed up existing .mam.env -> $DST.bak"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cp "$SRC" "$DST"
|
cp "$SRC" "$DST"
|
||||||
|
chmod 0600 "$DST" 2>/dev/null || true
|
||||||
echo "created: $DST"
|
echo "created: $DST"
|
||||||
echo "Next: edit $DST and fill in any secrets (look for 'replace_me')."
|
echo "Next: edit $DST and fill in any secrets (look for 'replace_me')."
|
||||||
|
|||||||
Reference in New Issue
Block a user