-
Notifications
You must be signed in to change notification settings - Fork 47
/
check-env-variables.sh
executable file
·84 lines (75 loc) · 2.56 KB
/
check-env-variables.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
#!/bin/bash -e
# shellcheck disable=SC1091
source "./scripts/helpers/suppress-output.sh"
# shellcheck disable=SC1091
source "./scripts/helpers/prior-confirmation.sh"
{
[[ -n "$ZSH_VERSION" && "$ZSH_EVAL_CONTEXT" =~ :file$ ]] ||
[[ -n "$BASH_VERSION" ]] && (return 0 2>/dev/null);
} && sourced=1 || sourced=0
[[ "$sourced" -eq 0 ]] && exit="exit" || exit="return"
quiet=$(should_suppress_output "$@")
yes=$(has_prior_confirmation "$@")
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# verify expected env variables are set
for EXPECTED_ENV_VAR in "$@"; do
EXPECTED_ENV_VAR=$(xargs echo -n <<< "$EXPECTED_ENV_VAR")
if {
[[ -n "$EXPECTED_ENV_VAR" ]] &&
[[ "$EXPECTED_ENV_VAR" != "--quiet" ]] &&
[[ "$EXPECTED_ENV_VAR" != "-q" ]] &&
[[ "$EXPECTED_ENV_VAR" != "--yes" ]] &&
[[ "$EXPECTED_ENV_VAR" != "-y" ]];
}; then
# shellcheck disable=SC2296
if {
[[ -n "$BASH_VERSION" ]] && [[ -z "${!EXPECTED_ENV_VAR}" ]];
} || {
[[ -n "$ZSH_VERSION" ]] && [[ -z "${(P)EXPECTED_ENV_VAR}" ]];
}; then
MISSING_ENV="true"
if [[ "$quiet" -eq 0 ]]; then
printf "%bERROR - You must have %b${EXPECTED_ENV_VAR}%b set in your environment to run this script!%b\n" "${RED}" "${YELLOW}" "${RED}" "${NC}"
fi
fi
fi
done
if [[ -n "$MISSING_ENV" ]]; then
$exit 1;
fi
if [[ "$quiet" -eq 0 ]]; then
printf "\n%bVerify these env variables seem correct:%b\n" "${GREEN}" "${NC}"
# # print all expected env variables
for EXPECTED_ENV_VAR in "$@"; do
EXPECTED_ENV_VAR=$(xargs echo -n <<< "$EXPECTED_ENV_VAR")
if {
[[ -n "$EXPECTED_ENV_VAR" ]] &&
[[ "$EXPECTED_ENV_VAR" != "--quiet" ]] &&
[[ "$EXPECTED_ENV_VAR" != "-q" ]] &&
[[ "$EXPECTED_ENV_VAR" != "--yes" ]] &&
[[ "$EXPECTED_ENV_VAR" != "-y" ]];
}; then
if [[ "$EXPECTED_ENV_VAR" == *"AWS"* ]] || [[ "$EXPECTED_ENV_VAR" == *"PASS"* ]]; then
echo "- $EXPECTED_ENV_VAR=*******"
else
# shellcheck disable=SC2296
[[ -n "$BASH_VERSION" ]] && value="${!EXPECTED_ENV_VAR}" || value="${(P)EXPECTED_ENV_VAR}"
echo "- $EXPECTED_ENV_VAR=${value}"
fi
fi
done
if { [[ -z "${CI}" ]] && [[ "$yes" -eq 0 ]]; } && { [[ -n "$BASH_VERSION" ]] || [[ "$sourced" -eq 0 ]]; }; then
printf "%bAre you sure you want to continue? (press y to confirm)%b\n\n" "${YELLOW}" "${NC}"
read -p "continue? (press y to confirm)" -n 1 -r
echo # (optional) move to a new line
if [[ ! ("$REPLY" =~ ^[Yy]$) ]]; then
echo "Aborted..."
$exit 1;
else
echo "Continuing..."
fi
fi
fi