From 7d50454d1436e138ab8b4d79222bc29925e1e953 Mon Sep 17 00:00:00 2001 From: riggraz Date: Tue, 24 Sep 2019 18:57:42 +0200 Subject: [PATCH] Add env vars to docker-compose and improve env check --- .env-example | 7 +++--- check-env.sh | 43 ++++++++++++++++++++++++++----------- config/application.rb | 4 ++-- config/environments/test.rb | 4 ++-- docker-compose.yml | 5 ++++- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/.env-example b/.env-example index 090b05d6..627f8f78 100644 --- a/.env-example +++ b/.env-example @@ -4,8 +4,7 @@ POSTGRES_USER=yourusernamehere POSTGRES_PASSWORD=yourpasswordhere APP_NAME="You App Name Here" +SHOW_LOGO=yes +POSTS_PER_PAGE=15 -EMAIL_CONFIRMATION=0 - -SHOW_LOGO=1 -POSTS_PER_PAGE=15 \ No newline at end of file +EMAIL_CONFIRMATION=no \ No newline at end of file diff --git a/check-env.sh b/check-env.sh index 94ac6cf1..7307bf13 100644 --- a/check-env.sh +++ b/check-env.sh @@ -1,19 +1,36 @@ #!/bin/bash -set -e # Abort on error - -function check_env_vars () { - for name; do - : ${!name:?$name must not be empty} - done -} +# Exit immediately if a command exits with a non-zero status. +set -e +# Check if .env file is present if [ ! -f .env ]; then - echo 'A .env file must be present.' - exit 2 + echo "A .env file must be present. Please create a .env file in the root directory." + exit 1 fi -if ! check_env_vars "ENVIRONMENT" "APP_NAME" "POSTGRES_USER" "POSTGRES_PASSWORD"; then - echo 'Some variables are not set in .env, please refer to script/check-env.sh for the list' - exit 4 -fi +# Array of environment variables that must be present +env_vars=( + "ENVIRONMENT" \ + "POSTGRES_USER" \ + "POSTGRES_PASSWORD" \ + "EMAIL_CONFIRMATION" \ + "APP_NAME" \ + "SHOW_LOGO" \ + "POSTS_PER_PAGE" \ +) + +# Check each one +n_of_errors=0 +for each in "${env_vars[@]}"; do + if ! [[ -v $each ]]; then + echo "$each is not set in .env file" + n_of_errors=$((n_of_errors+1)) + fi +done + +if [ $n_of_errors -gt 0 ]; then + echo "You need to set these ${n_of_errors} variables in your .env file." + echo "See .env-example for a configuration example." + exit 2 +fi \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 56c979a4..206bb6ee 100644 --- a/config/application.rb +++ b/config/application.rb @@ -21,11 +21,11 @@ module App end def email_confirmation? - ENV["EMAIL_CONFIRMATION"] == "1" + ENV["EMAIL_CONFIRMATION"] == "yes" end def show_logo? - ENV["SHOW_LOGO"] == "1" + ENV["SHOW_LOGO"] == "yes" end def posts_per_page diff --git a/config/environments/test.rb b/config/environments/test.rb index bdd9eaa8..3b7f2e37 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -4,8 +4,8 @@ # and recreated between test runs. Don't rely on the data there! # Set up default environment variables -ENV["EMAIL_CONFIRMATION"] = "0" -ENV["POSTS_PER_PAGE"] = "8" +ENV["EMAIL_CONFIRMATION"] = "no" +ENV["POSTS_PER_PAGE"] = "15" Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. diff --git a/docker-compose.yml b/docker-compose.yml index 51fb7dd7..83d8b2f2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,9 +12,12 @@ services: environment: - UPDATE=0 - ENVIRONMENT - - APP_NAME - POSTGRES_USER - POSTGRES_PASSWORD + - EMAIL_CONFIRMATION + - APP_NAME + - SHOW_LOGO + - POSTS_PER_PAGE volumes: - .:/app ports: