-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_env_file.sh
executable file
·136 lines (123 loc) · 4.42 KB
/
set_env_file.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
134
135
136
#!/bin/zsh
set -eu
LIGHT_RED='\033[1;31m'
CYAN='\033[0;36m'
GREEN='\033[0;32m'
LIGHT_GREEN='\033[1;32m'
DEFAULT='\033[0m'
ENV_FILE=.env
function print_pong_ascii_art() {
echo_colored "${LIGHT_GREEN}" "\nYou are ready to play pong like a pro!\n"
echo_colored "${LIGHT_GREEN}" " O ."
echo_colored "${LIGHT_GREEN}" " _/|\_-O"
echo_colored "${LIGHT_GREEN}" " ___|_______"
echo_colored "${LIGHT_GREEN}" " / | \\ "
echo_colored "${LIGHT_GREEN}" " / | \\ "
echo_colored "${LIGHT_GREEN}" " #################"
echo_colored "${LIGHT_GREEN}" " / _ ( )| \\ "
echo_colored "${LIGHT_GREEN}" " / ( ) | | \\ "
echo_colored "${LIGHT_GREEN}" " / \ |_/ | \\ "
echo_colored "${LIGHT_GREEN}" "/____\/|____|___________\\ "
echo_colored "${LIGHT_GREEN}" " | | |"
echo_colored "${LIGHT_GREEN}" " | / \ |"
echo_colored "${LIGHT_GREEN}" " | / \ |"
echo_colored "${LIGHT_GREEN}" " _/ /_\n"
}
function echo_colored() {
echo "${1}${2}${DEFAULT}"
}
function create_env_file() {
if [ ! -f .env ]; then
echo_colored "${CYAN}" "File ${ENV_FILE} does not exist. Creating it..."
else
echo_colored "${CYAN}" "File ${ENV_FILE} exists, do you want to overwrite it? (y/n)"
read -r OVERWRITE
if [ "${OVERWRITE}" = "y" ]; then
echo_colored "${CYAN}" "Overwriting ${ENV_FILE}...\n"
rm -f ${ENV_FILE}
else
echo_colored "${CYAN}" "Not overwriting ${ENV_FILE}"
exit 0
fi
fi
touch ${ENV_FILE}
}
function set_hostname() {
local HOST_IP_VAR="HOST_IP"
echo_colored "${CYAN}" "Setting ${HOST_IP_VAR}..."
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
set_env_var "${HOST_IP_VAR}" "$(hostname -I | awk '{print $1}')"
elif [[ "${OSTYPE}" == "darwin"* ]]; then
set_env_var "${HOST_IP_VAR}" "$(ipconfig getifaddr en0)"
else
echo_colored "${CYAN}" "Unsupported OS, please set HOST_IP manually"
ask_to_set_env_var "${HOST_IP_VAR}"
fi
}
function set_env_var() {
echo "${1}=\"${2}\"" >> ${ENV_FILE}
}
function ask_to_set_env_var() {
echo_colored "${CYAN}" "Enter value for ${1}:"
read -r VALUE
set_env_var "${1}" "${VALUE}"
}
function generate_random_env_var() {
echo_colored "${CYAN}" "Generating random value for ${1}..."
local VALUE=$(openssl rand -base64 32)
# delete all slashes and backslashes to avoid escaping issues
VALUE=$(echo "${VALUE}" | tr -d '\\/')
set_env_var "${1}" "${VALUE}"
}
function set_app42_env_vars() {
local APP_KEYS_DIR=~/.42keys
local APP_UID_FILE_NAME=transcendence_uid.txt
local APP_SECRET_FILE_NAME=transcendence_secret.txt
if [ ! -f ${APP_KEYS_DIR}/${APP_UID_FILE_NAME} ] || \
[ ! -f ${APP_KEYS_DIR}/${APP_SECRET_FILE_NAME} ]; then
echo_colored "${CYAN}" "File ${APP_KEYS_DIR}/${APP_UID_FILE_NAME} \
or ${APP_KEYS_DIR}/${APP_SECRET_FILE_NAME} does not exist."
echo_colored "${CYAN}" "Do you want to create them? (y/n)"
read -r SET_APP42_CREDS
if [ "${SET_APP42_CREDS}" = "y" ]; then
mkdir -p ${APP_KEYS_DIR}
echo_colored "${CYAN}" "Please enter your App42 credentials:"
echo_colored "${CYAN}" "App42 UID:"
read -r APP42_ID
echo "${APP42_ID}" > ${APP_KEYS_DIR}/${APP_UID_FILE_NAME}
echo_colored "${CYAN}" "App42 SECRET:"
read -r APP42_KEY
echo "${APP42_KEY}" > ${APP_KEYS_DIR}/${APP_SECRET_FILE_NAME}
set_env_var "APP42_ID" "${APP42_ID}"
set_env_var "APP42_KEY" "${APP42_KEY}"
else
echo_colored "${CYAN}" "Not creating directory ~/.42keys"
ask_to_set_env_var "APP42_ID"
ask_to_set_env_var "APP42_KEY"
fi
else
echo_colored "${CYAN}" "Setting App42 environment variables from ~/.keys42..."
local APP_ID=$(cat ${APP_KEYS_DIR}/$APP_UID_FILE_NAME)
local APP_KEY=$(cat ${APP_KEYS_DIR}/$APP_SECRET_FILE_NAME)
set_env_var "APP42_ID" "${APP_ID}"
set_env_var "APP42_KEY" "${APP_KEY}"
fi
}
function set_env() {
create_env_file
ask_to_set_env_var "POSTGRES_USER"
ask_to_set_env_var "POSTGRES_DB"
ask_to_set_env_var "DB_NAME"
ask_to_set_env_var "DB_USER"
ask_to_set_env_var "TEST_DB_NAME"
ask_to_set_env_var "APP_NAME"
set_app42_env_vars
generate_random_env_var "POSTGRES_PASSWORD"
generate_random_env_var "DB_PASSWORD"
generate_random_env_var "JWT_ACCESS_SECRET"
generate_random_env_var "JWT_REFRESH_SECRET"
set_hostname
print_pong_ascii_art
}
set_env
exit 0