-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path_load_env_vars
executable file
·41 lines (33 loc) · 1.23 KB
/
_load_env_vars
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
#! /bin/bash
#############################################################
### Script to export env vars from .env file with comments
#############################################################
# Check for .env file
if [ ! -f $PWD/.env ]; then
echo "No .env file found!!!"
return 1
fi
main() {
# Test if sed is gnu sed
MYSED=sed
gnu_mentions=$(sed --version 2>&1 | grep -i 'gnu' | wc -l | xargs -I VAR bash -c "echo VAR")
# If gnu isn't mentioned after `sed --version` then conclude it's not gnu sed
if [[ $gnu_mentions == 0 ]]; then
# If gsed exists then use it instead of sed
if [[ $(command -v gsed) ]]; then
MYSED=gsed
else
echo """
This script only works with GNU sed.
If you are on a MAC then use 'brew install gnu-sed'
"""
exit 1
fi
fi
# GNU-sed expression to export vars while ignoring lines beginning with # in .env file
cmd=$($MYSED -ne '/^#/d; /^export /{p;d}; /.*=/ s/^/export / p' .env)
eval $cmd
}
## Trick to check if this script is being sourced or sh-ed
unset BASH_SOURCE 2>/dev/null
test ".$0" == ".$BASH_SOURCE" && echo "You must <SOURCE> (not SH) this script!!!" || main "$@"