@@ -12,6 +12,7 @@ SCRIPT_NAME=$(basename "$0")
1212NO_VENV=false
1313FALLBACK_TO_INBUILT_DOCKER_DB=false
1414SETUP_DJANGO=false
15+ PREFER_EXPORTED_ENV=false
1516
1617# Color definitions for output
1718RED=$( 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 ;;
0 commit comments