-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvo.sh
executable file
·133 lines (113 loc) · 2.88 KB
/
envo.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env bash
# set -x
progname=$(basename "$0")
VERSION="[$progname] v0.1"
OPT_SILENT=false
OPT_NO_COLORS=false
ENVFILE=.env
# Colors
RED='\033[1;31m'
NC='\033[0m'
GREY='\033[1;30m'
ACCENT='\033[1;34m'
function error() {
if [[ "$OPT_NO_COLORS" == "true" ]]; then
printf "${1}\n"
else
printf "${RED}${1}${NC}\n"
fi
}
function msg() {
if [[ "$OPT_SILENT" == "false" ]]; then
if [[ "$OPT_NO_COLORS" == "true" ]]; then
printf "${1}\n"
else
printf "${GREY}${1}${NC}\n"
fi
fi
}
function print_running_command() {
if [[ "$OPT_SILENT" == "false" ]]; then
command=$1
if [[ "$OPT_NO_COLORS" == "true" ]]; then
printf "[$progname] running '${command}' with env from $ENVFILE..\n"
else
printf "${GREY}[$progname] running '${ACCENT}${command}${GREY}' with env from $ENVFILE..${NC}\n"
fi
fi
}
function usage() {
echo "usage: $progname [-v] [-h] [-nc] [-s] [-e KEY=VALUE] [-f infile] command"
echo " "
echo " example: $progname -f ~/my/secret/env/file.env -e CONCURRENCY=12 node ./app.js"
echo " WARNING: $progname requires .env file in $PWD. You can use another location with -f opt."
echo " "
echo " Options:"
echo " -v | -V | --version print $progname version"
echo " -h | --help display help"
echo " -nc | --no-colors print outputs without colors"
echo " -s | --silent silent mode - omit displaying info logs"
echo " -f | --env-file infile use other file than .env to load env vars"
echo " -e | --env-var KEY=VALUE override a single env var after loading env file, can be stacked"
}
if [[ -z "$1" ]]; then
usage
exit 1
fi
# Initializes overrides string to allow using -e KEY=VALUE overrides
OVERRIDES=""
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
case $1 in
-V | -v | --version)
msg "$VERSION"
exit 0
;;
-h | --help)
usage
exit 0
;;
-s | --silent)
OPT_SILENT=true
;;
-nc | --no-colors)
OPT_NO_COLORS=true
;;
-f | --env-file)
shift
msg "[$progname] use env from non-standard path: $1"
ENVFILE=$1
;;
-e | --env-var)
shift
msg "[$progname] override $1"
OVERRIDES="$OVERRIDES $1"
;;
esac
shift
done
if [[ "$1" == '--' ]]; then shift; fi
if [[ ! -e $ENVFILE ]]; then
error "[$progname] ERROR: env file $ENVFILE doesn't exist (you can specify another file with --env-file | -f flag"
exit 1
fi
if [[ -z "${@}" ]]; then
usage
exit 1
fi
# Load env vars from file
ENV_VARS_FROM_FILE=$(cat $ENVFILE | xargs)
if [[ -n "$ENV_VARS_FROM_FILE" ]]; then
export $ENV_VARS_FROM_FILE
fi
# Load overrides if present
if [[ -n "$OVERRIDES" ]]; then
export $OVERRIDES
fi
print_running_command ${@} $ENVFILE
which $1 >>/dev/null
if [[ "$?" != "0" ]]; then
error "[$progname] ERROR: command not found"
error "[$progname] ERROR: '$1' not found, using 'which $1'"
exit 1
fi
${@}