Refactor generate-config script to run all apps.

This commit is contained in:
2025-05-28 14:11:47 -07:00
parent b1ec7dd271
commit 848ff42b75

View File

@@ -3,17 +3,10 @@
# by evaluating variables in the app's .env file and splitting them # by evaluating variables in the app's .env file and splitting them
# into regular config and secret variables based on the "# Secrets" marker # into regular config and secret variables based on the "# Secrets" marker
# #
# Usage: bin/generate-config <app-name> # Usage: bin/generate-config [app-name]
set -e set -e
# Verify that an app name was provided
if [ $# -lt 1 ]; then
echo "Error: Missing app name"
echo "Usage: $(basename "$0") <app-name>"
exit 1
fi
# Source environment variables from load-env.sh # Source environment variables from load-env.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(dirname "$SCRIPT_DIR")" REPO_DIR="$(dirname "$SCRIPT_DIR")"
@@ -21,29 +14,31 @@ if [ -f "$REPO_DIR/load-env.sh" ]; then
source "$REPO_DIR/load-env.sh" source "$REPO_DIR/load-env.sh"
fi fi
APP_NAME="$1" # Function to process a single app
APP_DIR="$APPS_DIR/$APP_NAME" process_app() {
ENV_FILE="$APP_DIR/config/.env" local APP_NAME="$1"
CONFIG_FILE="$APP_DIR/config/config.env" local APP_DIR="$APPS_DIR/$APP_NAME"
SECRETS_FILE="$APP_DIR/config/secrets.env" local ENV_FILE="$APP_DIR/config/.env"
local CONFIG_FILE="$APP_DIR/config/config.env"
local SECRETS_FILE="$APP_DIR/config/secrets.env"
# Check if the app exists # Check if the app exists
if [ ! -d "$APP_DIR" ]; then if [ ! -d "$APP_DIR" ]; then
echo "Error: App '$APP_NAME' not found" echo "Error: App '$APP_NAME' not found"
exit 1 return 1
fi fi
# Check if the .env file exists # Check if the .env file exists
if [ ! -f "$ENV_FILE" ]; then if [ ! -f "$ENV_FILE" ]; then
echo "Error: Environment file not found: $ENV_FILE" echo "Warning: Environment file not found: $ENV_FILE"
exit 1 return 0
fi fi
# Process the .env file # Process the .env file
echo "Generating config files for $APP_NAME..." echo "Generating config files for $APP_NAME..."
# Create temporary files for processed content # Create temporary files for processed content
TMP_FILE="$APP_DIR/config/processed.env" # $(mktemp) local TMP_FILE="$APP_DIR/config/processed.env"
# Process the file with envsubst to expand variables # Process the file with envsubst to expand variables
envsubst < "$ENV_FILE" > $TMP_FILE envsubst < "$ENV_FILE" > $TMP_FILE
@@ -53,7 +48,7 @@ echo "# Generated by \`generate-config\` on $(date)" > "$CONFIG_FILE"
echo "# Generated by \`generate-config\` on $(date)" > "$SECRETS_FILE" echo "# Generated by \`generate-config\` on $(date)" > "$SECRETS_FILE"
# Find the line number of the "# Secrets" marker # Find the line number of the "# Secrets" marker
SECRETS_LINE=$(grep -n "^# Secrets" $TMP_FILE | cut -d':' -f1) local SECRETS_LINE=$(grep -n "^# Secrets" $TMP_FILE | cut -d':' -f1)
if [ -n "$SECRETS_LINE" ]; then if [ -n "$SECRETS_LINE" ]; then
# Extract non-comment lines with "=" before the "# Secrets" marker # Extract non-comment lines with "=" before the "# Secrets" marker
@@ -72,3 +67,19 @@ rm -f "$TMP_FILE"
echo "Generated:" echo "Generated:"
echo " - $CONFIG_FILE" echo " - $CONFIG_FILE"
echo " - $SECRETS_FILE" echo " - $SECRETS_FILE"
}
# Process all apps or specific app
if [ $# -lt 1 ]; then
# No app name provided - process all apps
for app_dir in "$APPS_DIR"/*; do
if [ -d "$app_dir" ]; then
APP_NAME="$(basename "$app_dir")"
process_app "$APP_NAME"
fi
done
exit 0
fi
APP_NAME="$1"
process_app "$APP_NAME"