Mod
12/03/2023, 1:01 PMCies
12/04/2023, 9:17 AM#!/bin/sh
# Filename: dotenv
# Purpose: import environment variables from .env file and make available to other commands
# Authors: Jason Leung (<mailto:jason@madcoda.com|jason@madcoda.com>), multiple contributors (<https://github.com/madcoda/dotenv-shell/graphs/contributors>)
# Bug-Reports: see <https://github.com/madcoda/dotenv-shell/issues>
# License: This file is licensed under the MIT License (MIT).
################################################################################
set -e
log_verbose() {
if [ "$VERBOSE" = 1 ]; then
echo "[dotenv.sh] $1" >&2
fi
}
is_set() {
eval val=\""\$$1"\"
if [ -z "$(eval "echo \$$1")" ]; then
return 1
else
return 0
fi
}
is_comment() {
case "$1" in
\#*)
log_verbose "Skip: $1"
return 0
;;
esac
return 1
}
is_blank() {
case "$1" in
'')
log_verbose "Skip: $1"
return 0
;;
esac
return 1
}
export_envs() {
while IFS='=' read -r key temp || [ -n "$key" ]; do
if is_comment "$key"; then
continue
fi
if is_blank "$key"; then
continue
fi
value=$(eval echo "$temp")
eval export "$key='$value'";
done < $1
}
# inject any defaults into the shell
if is_set "DOTENV_DEFAULT"; then
log_verbose "Setting defaults via $DOTENV_DEFAULT"
if [ -f "$DOTENV_DEFAULT" ]; then
export_envs "$DOTENV_DEFAULT"
else
echo '$DOTENV_DEFAULT file not found'
fi
fi
if is_set "DOTENV_FILE"; then
log_verbose "Reading from $DOTENV_FILE"
else
DOTENV_FILE=".env"
fi
# inject .env configs into the shell
if [ -f "$DOTENV_FILE" ]; then
export_envs "$DOTENV_FILE"
else
echo "$DOTENV_FILE file not found"
fi
# then run whatever commands you like
if [ $# -gt 0 ]; then
exec "$@"
fi
Cies
12/04/2023, 9:19 AMDOTENV_FILE=conf/local.env JAVA_TOOL_OPTIONS="-Dlog4j_rootLogger=INFO,Console,Rolling" ./conf/dotenv java -jar my_app.jar
Cies
12/04/2023, 9:20 AMEnvFile
plugin in IDEA is also very helpful when using this.CLOVIS
12/05/2023, 1:12 PMexport
in your file, then use the source
command:
# vars.env
export foo=bar
export other=3
Then just
# loads the environment variables
source vars.env
after that, you can execute your server normally.Cies
12/05/2023, 2:06 PM.env
file, that is also recognized by the EnvFile
plugin in IntelliJ.CLOVIS
12/05/2023, 2:17 PMCies
12/05/2023, 3:50 PMCies
12/05/2023, 3:52 PMCLOVIS
12/05/2023, 4:29 PMCies
12/05/2023, 4:31 PM