Create function to load.env files and refactor scripts

This commit is contained in:
Matthew Lopez
2024-05-23 21:21:46 -04:00
parent 811add80d3
commit cb564e3f48
8 changed files with 33 additions and 49 deletions

View File

@@ -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)"

View File

@@ -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."

View File

@@ -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")

View File

@@ -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=()

View File

@@ -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

View File

@@ -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

View File

@@ -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."

View File

@@ -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."