Skip to content

Commit 4fe78d5

Browse files
committed
Make .env file take precedence over exported vars, add a flag to make this behaviour configurable
1 parent a8f9a23 commit 4fe78d5

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

bin/mathesar

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SCRIPT_NAME=$(basename "$0")
1212
NO_VENV=false
1313
FALLBACK_TO_INBUILT_DOCKER_DB=false
1414
SETUP_DJANGO=false
15+
PREFER_EXPORTED_ENV=false
1516

1617
# Color definitions for output
1718
RED=$(tput setaf 1 2>/dev/null || echo "")
@@ -128,11 +129,12 @@ set_env_vars_from_file() {
128129
var_name="${trimmed%%=*}"
129130
var_value="${trimmed#*=}"
130131

131-
# Only export the variable if it is not already set.
132+
# If PREFER_EXPORTED_ENV is true, only set from .env if variable is not already set.
133+
# If PREFER_EXPORTED_ENV is false, always set from .env
132134
# This check uses bash parameter expansion: if the variable is not set, ${!var_name+_} is empty.
133135
# If it's set even when empty, it'll return a value (when empty, it'll return _).
134136
# `!` is required below - it denotes that the value of var_name should be used as the env variable name.
135-
if [[ -z "${!var_name+_}" ]]; then
137+
if [[ "${PREFER_EXPORTED_ENV}" = false ]] || [[ -z "${!var_name+_}" ]]; then
136138
export "$var_name"="$var_value"
137139
fi
138140
done < "${ENV_FILE}"
@@ -241,7 +243,7 @@ run_mathesar() {
241243
popd > /dev/null
242244
fi
243245

244-
local gunicorn_opts="config.wsgi -b 0.0.0.0:${MATHESAR_PORT}"
246+
local gunicorn_opts="config.wsgi -b 0.0.0.0:${MATHESAR_PORT} --chdir ${BASE_DIR}"
245247
if [ "${DEBUG}" = "true" ]; then
246248
gunicorn_opts+=" --log-level=debug"
247249
fi
@@ -254,9 +256,7 @@ run_mathesar() {
254256
fi
255257

256258
info "Starting gunicorn webserver..."
257-
pushd "${BASE_DIR}" > /dev/null
258-
exec "$gunicorn_bin" $gunicorn_opts
259-
popd > /dev/null
259+
exec "$gunicorn_bin" $gunicorn_opts
260260
}
261261

262262

@@ -282,6 +282,10 @@ Commands:
282282
Options:
283283
--port <port>, -p <port> Specify the port for Mathesar's web service (default is 8000).
284284
285+
--prefer-exported-env, -e Prefer existing exported environment variables over the .env file.
286+
If this option is set and required environment variables aren't exported,
287+
the values from the .env file will be substituted for the missing ones.
288+
285289
version
286290
Show version information.
287291
@@ -385,8 +389,12 @@ if [ "${COMMAND}" = "run" ]; then
385389
err "The --port/-p option requires an argument."
386390
fi
387391
;;
392+
--prefer-exported-env|-e)
393+
PREFER_EXPORTED_ENV=true
394+
shift
395+
;;
388396
-*)
389-
# Support clubbed short options for fallback (-f), no-venv (-n), and setup-django (-s).
397+
# Support clubbed short options for fallback (-f), no-venv (-n), prefer-exported-env (-e), and setup-django (-s).
390398
# If the option string includes any 'p' we now no longer allow clubbing.
391399
shortopts="${1#-}"
392400
if [[ "$shortopts" == *"p"* ]]; then
@@ -398,6 +406,7 @@ if [ "${COMMAND}" = "run" ]; then
398406
f) FALLBACK_TO_INBUILT_DOCKER_DB=true ;;
399407
n) NO_VENV=true ;;
400408
s) SETUP_DJANGO=true ;;
409+
e) PREFER_EXPORTED_ENV=true ;;
401410
*)
402411
usage_err "Unknown option: -$letter"
403412
;;

scripts/install.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ update_current_shell_env() {
508508
fi
509509
fi
510510
elif ! . "${INSTALL_DIR}/bin/mathesar_path_source"; then
511-
echo "Warning: Unable to source mathesar_path_source in the current session."
512-
echo "Please run: source \"${INSTALL_DIR}/bin/mathesar_path_source\" or restart your shell"
513-
return 1
511+
echo "Warning: Unable to source mathesar_path_source in the current session."
512+
echo "Please run: source \"${INSTALL_DIR}/bin/mathesar_path_source\" or restart your shell"
513+
return 1
514514
fi
515515

516516
# Re-exec the shell if interactive
@@ -613,11 +613,14 @@ configure_path() {
613613

614614
info "Attempting to update the current shell environment..."
615615
if update_current_shell_env; then
616-
success "Everything's ready, you can now start Mathesar by executing \"mathesar run\". Use \"mathesar help\" for more information.".
616+
success "Everything's ready, you can now start Mathesar by executing \"mathesar run\"."
617+
echo "Use \"mathesar help\" for more information.".
618+
echo "Please restart your shell if the 'mathesar' command is not found."
617619
else
618620
echo "Unable to automatically update the current shell environment."
619621
echo "Please run the following command in your terminal to update your PATH:"
620622
echo "source \"${INSTALL_DIR}/bin/mathesar_path_source\""
623+
echo "Please restart your shell if the 'mathesar' command is still not found."
621624
fi
622625
fi
623626
fi

0 commit comments

Comments
 (0)