-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf.sh
64 lines (57 loc) · 1.94 KB
/
conf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
# WARNING Do not set exit on error or undefined or pipefail!
# This script is intended to be sourced from bash profile
# Helper func to toggle env with built in bash functions.
# This script only supports ".env" files, see
# https://github.com/mozey/config#toggling-env
conf() {
# APP_DIR is the full path to the application basedir.
# The config file must exist under this path,
# and project files can be referenced relative to APP_DIR
APP_DIR=$(pwd)
export APP_DIR
# Default env is dev, first arg overrides
ENV=""
if [ $# -eq 1 ]; then
ENV=${1}
fi
if [ -z "${ENV}" ]; then
ENV="dev"
fi
# File loading precedence, see
# https://github.com/mozey/config#file-loading-precedence
FILE=""
if [[ ${ENV} == "dev" ]]; then
# Prefix is optional for ENV == "dev"
if [ -f "${APP_DIR}/.env" ]; then
FILE="${APP_DIR}/.env"
fi
fi
if [ -f "${APP_DIR}/${ENV}.env" ]; then
FILE="${APP_DIR}/${ENV}.env"
fi
if [ ! -f "${FILE}" ]; then
echo "config file not found for env ${ENV}"
return 1
fi
echo "Setting env for ${ENV} from ${FILE}"
# Set environment variables from file
# https://stackoverflow.com/a/20909045/639133
UNAME_STR=$(uname)
if [ "$UNAME_STR" = 'Linux' ]; then
export "$(grep -v '^#' "${FILE}" | xargs -d '\n')"
elif [ "$UNAME_STR" = 'FreeBSD' ] || [ "$UNAME_STR" = 'Darwin' ]; then
export "$(grep -v '^#' "${FILE}" | xargs -0)"
else
echo "unexpected uname ${UNAME_STR}"
return 1
fi
# NOTE Unlike conf.configu.sh, this script does not
# unset env vars subsequently removed from the config file.
# To refresh your env start a new terminal session,
# or use the configu command
# https://github.com/mozey/config#toggling-env-with-configu
# Print application env
printenv | sort | grep --color -E "APP_|AWS_"
return 0
}