-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_env.sh
executable file
·192 lines (172 loc) · 4.83 KB
/
generate_env.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
# generate_env - generates local .env file for docker-compose
set -e
# USAGE
function usage
{
echo "TODO"
}
# 0. SET ARGUMENTS
arg_http_proxy=
arg_local_user_id=
arg_local_group_id=
arg_rails_name=
arg_rails_external_port=
arg_rails_run_env=
arg_rails_secret_key_base=
while [ -n "$1" ]; do
case $1 in
-p | --http-proxy ) shift
arg_http_proxy=$1
;;
-u | --user ) shift
arg_local_user_id=$1
;;
-g | --group ) shift
arg_local_group_id=$1
;;
-n | --name ) shift
arg_rails_name=$1
;;
-p | --port ) shift
arg_rails_external_port=$1
;;
-e | --environment ) shift
arg_rails_run_env=$1
;;
-s | --secret ) shift
arg_rails_secret_key_base=$1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
# WARNING: WILL OVERWRITE
if [ -f .env ]; then
read -r -n 1 -p "Overwrite existing .env file? " REPLY
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
: # pass through
else
echo "aborting"
exit 1 || return 1
fi
fi
# 1. HTTP_PROXY
# Default: blank
if [ -n "$arg_http_proxy" ]; then
echo "HTTP_PROXY=$arg_http_proxy" > .env
echo "HTTPS_PROXY=$arg_http_proxy" >> .env
elif [ ! $HTTP_PROXY = "" ]; then
echo "HTTP_PROXY=$HTTP_PROXY" > .env
echo "HTTPS_PROXY=$HTTPS_PROXY" >> .env
elif [ ! $http_proxy = "" ]; then
echo "HTTP_PROXY=$http_proxy" > .env
echo "HTTPS_PROXY=$https_proxy" >> .env
else
echo "HTTP_PROXY=" > .env
echo "HTTPS_PROXY=" >> .env
fi
# 2. LOCAL_USER_ID
# Default: *current user id
if [ -n "$arg_local_user_id" ]; then
echo "LOCAL_USER_ID=$arg_local_user_id" >> .env
elif [ ! $LOCAL_USER_ID = "" ]; then
echo "LOCAL_USER_ID=$LOCAL_USER_ID" >> .env
else
echo "LOCAL_USER_ID=$(id -u)" >> .env
fi
# 3. LOCAL_GROUP_ID
# Default: *current group id
if [ -n "$arg_local_group_id" ]; then
echo "LOCAL_GROUP_ID=$arg_local_group_id" >> .env
elif [ ! $LOCAL_GROUP_ID = "" ]; then
echo "LOCAL_GROUP_ID=$LOCAL_GROUP_ID" >> .env
else
echo "LOCAL_GROUP_ID=$(id -g)" >> .env
fi
# 4. RAILS_NAME
# Default: *current directory name
if [ -n "$arg_rails_name" ]; then
echo "RAILS_NAME=$arg_rails_name" >> .env
elif [ ! $RAILS_NAME = "" ]; then
echo "RAILS_NAME=$RAILS_NAME" >> .env
else
echo "RAILS_NAME=rails_${PWD##*/}" >> .env
fi
# 5. RAILS_EXTERNAL_PORT
# Default: 3000
if [ -n "$arg_rails_external_port" ]; then
echo "RAILS_EXTERNAL_PORT=$arg_rails_external_port" >> .env
elif [ ! $RAILS_EXTERNAL_PORT = "" ]; then
echo "RAILS_EXTERNAL_PORT=$RAILS_EXTERNAL_PORT" >> .env
else
echo "RAILS_EXTERNAL_PORT=3000" >> .env
fi
# 6. RAILS_RUN_ENV
# Default: development
if [ -n "$arg_rails_run_env" ]; then
echo "RAILS_RUN_ENV=$arg_rails_run_env" >> .env
elif [ ! $RAILS_RUN_ENV = "" ]; then
echo "RAILS_RUN_ENV=$RAILS_RUN_ENV" >> .env
else
echo "RAILS_RUN_ENV=development" >> .env
fi
# 7. RAILS_SECRET_KEY_BASE
# Default: *generate new secret with \rake secret\
if [ -n "$arg_rails_secret_key_base" ]; then
echo "RAILS_SECRET_KEY_BASE=$arg_rails_secret_key_base" >> .env
elif [ ! $RAILS_SECRET_KEY_BASE = "" ]; then
echo "RAILS_SECRET_KEY_BASE=$RAILS_SECRET_KEY_BASE" >> .env
elif [ $(command -v ruby) ]; then
echo "RAILS_SECRET_KEY_BASE=$(ruby -e 'require "securerandom"' -e 'puts SecureRandom.hex 64')" >> .env
else
echo "RAILS_SECRET_KEY_BASE=" >> .env
fi
# 8. RAILS_DEV_DB_USER
# Default: *empty
if [ ! $RAILS_DEV_DB_USER = "" ]; then
echo "RAILS_DEV_DB_USER=$RAILS_DEV_DB_USER" >> .env
else
echo "RAILS_DEV_DB_USER=" >> .env
fi
# 9. RAILS_DEV_DB_PASS
# Default: *empty
if [ ! $RAILS_DEV_DB_PASS = "" ]; then
echo "RAILS_DEV_DB_PASS=$RAILS_DEV_DB_PASS" >> .env
else
echo "RAILS_DEV_DB_PASS=" >> .env
fi
# 10. RAILS_STAG_DB_USER
# Default: *empty
if [ ! $RAILS_STAG_DB_USER = "" ]; then
echo "RAILS_STAG_DB_USER=$RAILS_STAG_DB_USER" >> .env
else
echo "RAILS_STAG_DB_USER=" >> .env
fi
# 11. RAILS_STAG_DB_PASS
# Default: *empty
if [ ! $RAILS_STAG_DB_PASS = "" ]; then
echo "RAILS_STAG_DB_PASS=$RAILS_STAG_DB_PASS" >> .env
else
echo "RAILS_STAG_DB_PASS=" >> .env
fi
# 12. RAILS_PROD_DB_USER
# Default: *empty
if [ ! $RAILS_PROD_DB_USER = "" ]; then
echo "RAILS_PROD_DB_USER=$RAILS_PROD_DB_USER" >> .env
else
echo "RAILS_PROD_DB_USER=" >> .env
fi
# 13. RAILS_PROD_DB_PASS
# Default: *empty
if [ ! $RAILS_PROD_DB_PASS = "" ]; then
echo "RAILS_PROD_DB_PASS=$RAILS_PROD_DB_PASS" >> .env
else
echo "RAILS_PROD_DB_PASS=" >> .env
fi
exit 0