-
Notifications
You must be signed in to change notification settings - Fork 3
/
mann
executable file
·319 lines (274 loc) · 7.53 KB
/
mann
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
# Zenflows is designed to implement the Valueflows vocabulary,
# written and maintained by srfsh <[email protected]>.
# Copyright (C) 2021-2023 Dyne.org foundation <[email protected]>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
set -e
progname=$(basename "$0")
export MIX_ENV="${MIX_ENV:-dev}"
main() {
while getopts xpdt opt 2>/dev/null; do
case $opt in
x) set -x ;;
p) export MIX_ENV=prod ;;
d) export MIX_ENV=dev ;;
t) export MIX_ENV=test ;;
\?) usage ;;
esac
done
shift "$((OPTIND-1))"
case $1 in
help) shift ; help "$@" ;;
mix) shift ; cmd_mix "$@" ;;
serve) shift ; cmd_serve "$@" ;;
clean) shift ; cmd_clean "$@" ;;
test) shift ; cmd_test "$@" ;;
cover) shift ; cmd_cover "$@" ;;
cover.clean) shift ; cmd_cover_clean "$@" ;;
env.setup) shift ; cmd_env_setup "$@" ;;
dep.setup) shift ; cmd_dep_setup "$@" ;;
dep.clean) shift ; cmd_dep_clean "$@" ;;
dep.copy) shift ; cmd_dep_copy "$@" ;;
db.setup) shift ; cmd_db_setup "$@" ;;
db.reset) shift ; cmd_db_reset "$@" ;;
db.create) shift ; cmd_db_create "$@" ;;
db.drop) shift ; cmd_db_drop "$@" ;;
db.migrate) shift ; cmd_db_migrate "$@" ;;
db.rollback) shift ; cmd_db_rollback "$@" ;;
db.remigrate) shift ; cmd_db_remigrate "$@" ;;
doc) shift ; cmd_doc "$@" ;;
doc.serve) shift ; cmd_doc_serve "$@" ;;
doc.clean) shift ; cmd_doc_clean "$@" ;;
devop.setup) shift ; cmd_devop_setup "$@" ;;
devop.serve) shift ; cmd_devop_serve "$@" ;;
devop.comp) shift ; cmd_devop_comp "$@" ;;
devop.build) shift ; cmd_devop_build "$@" ;;
devop.run) shift ; cmd_devop_run "$@" ;;
devop.exec) shift ; cmd_devop_exec "$@" ;;
devop.up) shift ; cmd_devop_up "$@" ;;
devop.down) shift ; cmd_devop_down "$@" ;;
devop.logs) shift ; cmd_devop_logs "$@" ;;
*) usage ;;
esac
}
die() {
printf '%s: %s\n' "$progname" "$*" >&2
exit 1
}
warn() {
printf '%s: %s\n' "$progname" "$*" >&2
}
usage() {
cat <<EOF >&2
usage: $progname [-x] [-p | -d | -t] cmd [args...]
quick overview: $progname help
EOF
exit 1
}
help() {
cat <<EOF
options:
-x Write a trace for each command. See sh(1) for details.
-p Set MIX_ENV=prod.
-d Set MIX_ENV=dev.
-t Set MIX_ENV=test.
commands:
help Print this text.
mix Run 'mix'.
serve Run the application using 'iex'.
clean Clean all unnecessary files.
test Run the tests using 'mix test'.
cover Run coverage on tests.
cover.clean Clean generated coverage files.
env.setup Setup configs.
dep.setup Setup dependencies.
dep.clean Clean dependencies.
dep.copy Copy deps to .deps. Should be used only by devs.
db.setup Create the database and run the migrations.
db.reset Drop the database, recrate and remigrate it.
db.create Create the database.
db.drop Drop the database.
db.migrate Run the migration scripts.
db.rollback Roll back migrations.
db.remigrate Unroll all the migrations and migrate them again.
doc Generate static docs.
doc.serve Serve the documantation with Erlang.
doc.clean Clean the generated docs.
devop.setup Generate the pre-filled docker-compose file.
devop.serve Build, setup, run everything, and attach to back-end shell.
devop.db.setup Create the database and run the migrations.
devop.comp Run 'docker-compose'.
devop.build Run 'docker-compose build.
devop.run Run 'docker-compose run'.
devop.exec Run 'docker-compose exec.
devop.up Run 'docker-compose up'.
devop.down Run 'docker-compose down'.
devop.logs Run 'docker-compose logs'.
EOF
}
setup() {
if ! test -e conf/env.sh ; then
warn "the file 'conf/env.sh' doesn't exist; running env.setup"
cmd_env_setup
fi
}
readenv() {
. conf/env.sh
}
# commands
cmd_mix() {
setup
readenv
mix "$@"
}
cmd_serve() {
setup
readenv
iex -S mix "$@"
}
cmd_clean() {
cmd_cover_clean
cmd_doc_clean
cmd_dep_clean
rm -rf _build
}
cmd_test() {
export MIX_ENV=test
cmd_mix test "$@"
}
cmd_cover() {
export MIX_ENV=test
cmd_mix test --cover "$@"
}
cmd_cover_clean() {
rm -rf cover
}
cmd_env_setup() {
if test -e conf/env.sh ; then
warn "the file 'conf/env.sh' already exists; skipping"
else
key=$(openssl rand -hex 64)
salt=$(openssl rand -hex 64)
sed 's,#ADMIN_KEY#,'"$key"', ; s,#ROOM_SALT#,'"$salt"',' conf/.env.templ >conf/env.sh
warn "the 'conf/env.sh' file is generated; please edit it to suit your needs"
fi
}
cmd_dep_setup() {
test -d deps || cp -rf .deps deps
}
cmd_dep_clean() {
rm -rf deps
}
cmd_dep_copy() {
ext=$(date +%Y%m%d%H%M%S)
mv .deps .deps."$ext"
cp -rf deps .deps
}
cmd_db_setup() {
cmd_db_create
cmd_db_migrate
}
cmd_db_reset() {
cmd_db_drop
cmd_db_setup
}
cmd_db_create() {
cmd_mix ecto.create "$@"
}
cmd_db_drop() {
cmd_mix ecto.drop "$@"
}
cmd_db_migrate() {
cmd_mix ecto.migrate "$@"
}
cmd_db_rollback() {
cmd_mix ecto.rollback "$@"
}
cmd_db_remigrate() {
cmd_db_rollback --all
cmd_db_migrate
}
cmd_doc() {
cmd_mix docs -f html
}
cmd_doc_serve() {
if ! test -e .docs/index.html ; then
warn "the docs hasn't been generated yet; running 'doc' to generate them"
cmd_doc
fi
erl -noshell -s inets -eval '
{ok, _Pid} = inets:start(httpd, [
{server_name, "zenflows document server"},
{server_root, ".docs/"},
{document_root, ".docs/"},
{port, 8192},
{directory_index, ["index.html"]},
{mime_types, [
{"html", "text/html"},
{"js", "text/javascript"},
{"css", "text/css"}
]}
]),
io:format("the docs are served at: http://localhost:8192/~nhit control-c twitce to exit~n").
'
}
cmd_doc_clean() {
rm -rf .docs
}
cmd_devop_setup() {
if test -e devop/docker-compose.yml ; then
warn "the file 'devop/docker-compose.yml' already exists; skipping"
else
admin_key=$(openssl rand -hex 64)
db_pass=$(openssl rand -hex 16)
room_salt=$(openssl rand -base64 64 | tr -d \\n)
sed 's,#ADMIN_KEY#,'"$admin_key"', ;
s,#DB_PASS#,'"$db_pass"', ;
s,#ROOM_SALT#,'"$room_salt"',' devop/.docker-compose.templ >devop/docker-compose.yml
warn "the 'devop/docker-compose.yml' file is generated; please edit it to suit your needs"
fi
}
cmd_devop_serve() {
cmd_devop_comp run --service-ports --rm srv start_iex
}
cmd_devop_comp() {
cmd_devop_setup
if which docker-compose >/dev/null
then
docker-compose -p zenflows -f devop/docker-compose.yml "$@"
else
docker compose -p zenflows -f devop/docker-compose.yml "$@"
fi
}
cmd_devop_build() {
cmd_devop_comp build "$@"
}
cmd_devop_run() {
cmd_devop_comp run "$@"
}
cmd_devop_exec() {
cmd_devop_comp exec "$@"
}
cmd_devop_up() {
cmd_devop_comp up "$@"
}
cmd_devop_down() {
cmd_devop_comp down "$@"
}
cmd_devop_logs() {
cmd_devop_comp logs "$@"
}
main "$@"