From cb564e3f48d7df4ff2777ef53f919d9b53440f50 Mon Sep 17 00:00:00 2001 From: Matthew Lopez <73856503+MatthewL246@users.noreply.github.com> Date: Thu, 23 May 2024 21:21:46 -0400 Subject: [PATCH] Create function to load.env files and refactor scripts --- scripts/backup.sh | 8 +------ scripts/compile-custom-inkay.sh | 6 +---- scripts/internal/firstrun-minio-container.sh | 7 +----- scripts/internal/framework.sh | 23 +++++++++++++++++++ .../update-account-servers-database.sh | 17 ++++---------- scripts/internal/update-postgres-password.sh | 7 +----- scripts/restore.sh | 8 +------ scripts/upload-3ds-files.sh | 6 +---- 8 files changed, 33 insertions(+), 49 deletions(-) diff --git a/scripts/backup.sh b/scripts/backup.sh index d152e72..0e3995c 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -7,13 +7,7 @@ this before doing something risky with the datbases to prevent data loss." add_positional_argument "backup-name" "backup_name" "Name of the backup directory, defaults to the current date and time" false parse_arguments "$@" -if [[ ! -f "$git_base_dir/environment/minio.local.env" ]]; then - print_error "Missing environment file minio.local.env. Did you run setup-environment.sh?" - exit 1 -fi -source "$git_base_dir/environment/minio.env" -source "$git_base_dir/environment/minio.local.env" -source "$git_base_dir/environment/postgres.env" +load_dotenv minio.env minio.local.env postgres.env if [[ -z "$backup_name" ]]; then backup_name="backup_$(date +%Y-%m-%dT%H.%M.%S)" diff --git a/scripts/compile-custom-inkay.sh b/scripts/compile-custom-inkay.sh index 8635cc6..692fcce 100755 --- a/scripts/compile-custom-inkay.sh +++ b/scripts/compile-custom-inkay.sh @@ -7,11 +7,7 @@ Miiverse applet. It must be run if you want to use Juxt." add_option "-r --reset" "should_reset" "Resets the Inkay CA certificate to Pretendo's instead of using the mitmproxy certificate" parse_arguments "$@" -if [[ ! -f "$git_base_dir/.env" ]]; then - print_error "Missing .env file. Did you run setup-environment.sh?" - exit 1 -fi -source "$git_base_dir/.env" +load_dotenv .env if [[ -z "${WIIU_IP:-}" ]]; then print_warning "Missing environment variable WIIU_IP. Did you specify a Wii U IP address when you ran setup-environment.sh?" print_info "Continuing without automatic FTP upload." diff --git a/scripts/internal/firstrun-minio-container.sh b/scripts/internal/firstrun-minio-container.sh index eb633ad..df714f2 100755 --- a/scripts/internal/firstrun-minio-container.sh +++ b/scripts/internal/firstrun-minio-container.sh @@ -4,12 +4,7 @@ source "$(dirname "$(realpath "$0")")/framework.sh" parse_arguments "$@" -if [[ ! -f "$git_base_dir/environment/minio.local.env" ]]; then - print_error "Missing environment file minio.local.env. Did you run setup-environment.sh?" - exit 1 -fi -source "$git_base_dir/environment/minio.env" -source "$git_base_dir/environment/minio.local.env" +load_dotenv minio.env minio.local.env minio_init_script=$(cat "$git_base_dir/scripts/run-in-container/minio-init.sh") diff --git a/scripts/internal/framework.sh b/scripts/internal/framework.sh index 88fcec2..7158137 100644 --- a/scripts/internal/framework.sh +++ b/scripts/internal/framework.sh @@ -124,6 +124,29 @@ run_command_until_success() { done } +# Load a .env file file from the environment directory. The global .env file is a special case, as it is loaded from the +# repository root. Variables are sourced as non-exported global variables. +# +# Usage: load_dotenv dotenv_files... +# Example: load_dotenv .env example.env example.local.env +load_dotenv() { + for env_file in "$@"; do + local env_file_path + if [[ "$env_file" = ".env" ]]; then + env_file_path="$git_base_dir/$env_file" + else + env_file_path="$git_base_dir/environment/$env_file" + fi + + if [[ -f "$env_file_path" ]]; then + source "$env_file_path" + else + print_error "Environment file not found: $env_file. Did you run the setup script?" + exit 1 + fi + done +} + # Argument parsing framework # Uses a lot of Bash parameter expansion tricks: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion argument_variables=() diff --git a/scripts/internal/update-account-servers-database.sh b/scripts/internal/update-account-servers-database.sh index 2f651a6..4b2f36b 100755 --- a/scripts/internal/update-account-servers-database.sh +++ b/scripts/internal/update-account-servers-database.sh @@ -6,20 +6,11 @@ parse_arguments "$@" create_server_script=$(cat "$git_base_dir/scripts/run-in-container/update-account-servers-database.js") -if [[ ! -f "$git_base_dir/.env" ]]; then - print_error "Missing .env file. Did you run setup-environment.sh?" - exit 1 -fi -source "$git_base_dir/.env" +load_dotenv .env -necessary_environment_files=("friends" "miiverse-api" "wiiu-chat" "super-mario-maker") -for environment in "${necessary_environment_files[@]}"; do - if [[ ! -f "$git_base_dir/environment/$environment.local.env" ]]; then - print_error "Missing environment file $environment.local.env. Did you run setup-environment.sh?" - exit 1 - fi - source "$git_base_dir/environment/$environment.env" - source "$git_base_dir/environment/$environment.local.env" +dotenv_files=("friends" "miiverse-api" "wiiu-chat" "super-mario-maker") +for file in "${dotenv_files[@]}"; do + load_dotenv "$file.env" "$file.local.env" done docker compose up -d account diff --git a/scripts/internal/update-postgres-password.sh b/scripts/internal/update-postgres-password.sh index ea6a6cd..2edb143 100755 --- a/scripts/internal/update-postgres-password.sh +++ b/scripts/internal/update-postgres-password.sh @@ -4,12 +4,7 @@ source "$(dirname "$(realpath "$0")")/framework.sh" parse_arguments "$@" -if [[ ! -f "$git_base_dir/environment/postgres.local.env" ]]; then - print_error "Missing environment file postgres.local.env. Did you run setup-environment.sh?" - exit 1 -fi -source "$git_base_dir/environment/postgres.env" -source "$git_base_dir/environment/postgres.local.env" +load_dotenv postgres.env postgres.local.env docker compose up -d postgres diff --git a/scripts/restore.sh b/scripts/restore.sh index 6a5dde1..dc87e0e 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -10,13 +10,7 @@ add_positional_argument "backup-directory" "backup_dir" "The backup directory to add_option "-f --force" "force" "Skips the restore confirmation prompt" parse_arguments "$@" -if [[ ! -f "$git_base_dir/environment/minio.local.env" ]]; then - print_error "Missing environment file minio.local.env. Did you run setup-environment.sh?" - exit 1 -fi -source "$git_base_dir/environment/minio.env" -source "$git_base_dir/environment/minio.local.env" -source "$git_base_dir/environment/postgres.env" +load_dotenv minio.env minio.local.env postgres.env if [[ ! -d "$backup_dir" ]]; then print_error "Backup directory $backup_dir does not exist." diff --git a/scripts/upload-3ds-files.sh b/scripts/upload-3ds-files.sh index 19622c4..2c5d39c 100755 --- a/scripts/upload-3ds-files.sh +++ b/scripts/upload-3ds-files.sh @@ -6,11 +6,7 @@ set_description "This uploads required files from console-files to your 3DS to c add_option "-r --reset" "should_reset" "Reset the Juxt certificate to Pretendo's instead of using the mitmproxy certificate" parse_arguments "$@" -if [[ ! -f "$git_base_dir/.env" ]]; then - print_error "Missing .env file. Did you run setup-environment.sh?" - exit 1 -fi -source "$git_base_dir/.env" +load_dotenv .env if [[ -z "${DS_IP:-}" ]]; then print_warning "Missing environment variable DS_IP. Did you specify a 3DS IP address when you ran setup-environment.sh?" print_info "Continuing without automatic FTP upload."