forked from stackernews/stacker.news
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sndev
executable file
·571 lines (452 loc) · 11.2 KB
/
sndev
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/bin/sh
set -e
docker__compose() {
if [ ! -x "$(command -v docker-compose)" ]; then
echo "docker compose is not installed"
echo "installation instructions are here: https://docs.docker.com/desktop/"
exit 0
fi
if [ -z "$COMPOSE_PROFILES" ]; then
COMPOSE_PROFILES="images,search,payments,email,capture"
fi
ENV_LOCAL=
if [ -f .env.local ]; then
ENV_LOCAL='--env-file .env.local'
fi
CURRENT_UID=$(id -u) CURRENT_GID=$(id -g) COMPOSE_PROFILES=$COMPOSE_PROFILES command docker compose --env-file .env.development $ENV_LOCAL "$@"
}
docker__exec() {
if [ ! -x "$(command -v docker)" ]; then
echo "docker is not installed"
echo "installation instructions are here: https://docs.docker.com/desktop/"
exit 0
fi
DOCKER_CLI_HINTS=false command docker exec -i "$@"
}
docker__sn_lnd() {
t=$1
if [ "$t" = "-t" ]; then
shift
else
t=""
fi
docker__exec $t -u lnd sn_lnd lncli "$@"
}
docker__stacker_lnd() {
t=$1
if [ "$t" = "-t" ]; then
shift
else
t=""
fi
docker__exec $t -u lnd stacker_lnd lncli "$@"
}
docker__stacker_cln() {
t=$1
if [ "$t" = "-t" ]; then
shift
else
t=""
fi
docker__exec $t -u clightning stacker_cln lightning-cli --regtest "$@"
}
sndev__start() {
shift
if [ $# -eq 0 ]; then
docker__compose up --build
exit 0
fi
docker__compose up "$@"
}
sndev__help_start() {
help="
start the sndev env
USAGE
$ sndev start [OPTIONS] [SERVICE...]
OPTIONS"
echo "$help"
docker__compose up --help | awk '/Options:/{y=1;next}y'
}
sndev__stop() {
shift
docker__compose down "$@"
}
sndev__help_stop() {
help="
stop the sndev env
USAGE
$ sndev stop [OPTIONS] [SERVICE...]
OPTIONS"
echo "$help"
docker__compose down --help | awk '/Options:/{y=1;next}y'
}
sndev__open() {
shift
service=$(docker__compose ps $1 --format '{{.Label "CONNECT"}}')
if [ -z "$service" ]; then
echo "no url found for $1"
exit 1
fi
service="http://$service"
echo "opening $1 ... $service"
if [ "$(uname)" = "Darwin" ]; then
open $service
elif [ "$(uname)" = "Linux" ]; then
xdg-open $service
elif [ "$(uname)" = "Windows_NT" ]; then
start $service
fi
}
sndev__help_open() {
help="
open a container's url if it has one
USAGE
$ sndev open SERVICE
OPTIONS
no options currently exist
"
echo "$help"
}
sndev__restart() {
shift
docker__compose restart "$@"
}
sndev__help_restart() {
help="
restart the sndev env
USAGE
$ sndev restart [OPTIONS] [SERVICE...]
OPTIONS"
echo "$help"
docker__compose restart --help | awk '/Options:/{y=1;next}y'
}
sndev__logs() {
shift
docker__compose logs "$@"
}
sndev__help_logs() {
help="
get logs from sndev env
USAGE
$ sndev logs [OPTIONS] [SERVICE...]
OPTIONS"
echo "$help"
docker__compose logs --help | awk '/Options:/{y=1;next}y'
}
sndev__status() {
shift
if [ $# -eq 0 ]; then
docker__compose ps -a --format 'table {{.Service}}\t{{.State}}\t{{.Status}}\t{{.Label "CONNECT"}}'
exit 0
fi
docker__compose ps "$@"
}
sndev__help_status() {
help="
show container status of sndev env
USAGE
$ sndev status [OPTIONS] [SERVICE...]
OPTIONS"
echo "$help"
docker__compose ps --help | awk '/Options:/{y=1;next}y'
}
sndev__delete() {
printf "this deletes containers, volumes, and orphans - are you sure? [y/N] "
read -r answer
if [ "$answer" = "y" ]; then
docker__compose down --volumes --remove-orphans
else
echo "delete cancelled"
fi
}
sndev__help_delete() {
help="
remove orphans and volumes from sndev env
equivalent to sndev stop --volumes --remove-orphans
USAGE
$ sndev delete
"
echo "$help"
}
sndev__fund() {
shift
docker__stacker_lnd -t payinvoice "$@"
}
sndev__help_fund() {
help="
pay a bolt11 for funding
USAGE
$ sndev fund <bolt11> [OPTIONS]
OPTIONS"
echo "$help"
docker__stacker_lnd payinvoice -h | awk '/OPTIONS:/{y=1;next}y' | awk '!/^[\t ]+--pay_req value/'
}
sndev__cln_fund() {
shift
docker__stacker_cln -t pay "$@"
}
sndev__help_cln_fund() {
help="
pay a bolt11 for funding with CLN
USAGE
$ sndev cln_fund <bolt11>"
echo "$help"
}
sndev__cln_withdraw() {
shift
label=$(date +%s)
docker__stacker_cln -t invoice "$1" "$label" sndev | jq -r '.bolt11'
}
sndev__help_cln_withdraw() {
help="
create a bolt11 for withdrawal with CLN
USAGE
$ sndev cln_withdraw <amount msats>"
echo "$help"
}
sndev__withdraw() {
shift
docker__stacker_lnd addinvoice --amt "$@" | jq -r '.payment_request'
}
sndev__help_withdraw() {
help="
create a bolt11 for withdrawal
USAGE
$ sndev withdraw <amount sats> [OPTIONS]
OPTIONS"
echo "$help"
docker__stacker_lnd addinvoice -h | awk '/OPTIONS:/{y=1;next}y' | awk '!/^[\t ]+(--amt|--amt_msat) value/'
}
sndev__psql() {
shift
docker__exec -t db psql "$@" -U sn -d stackernews
}
sndev__help_psql() {
help="
open psql on db
USAGE
$ sndev psql [OPTIONS]
OPTIONS"
echo "$help"
docker__exec db psql --help | awk '/General options:/{y=1;next}y' | sed -n '/Connection options:/q;p' |
awk '!/^([\t ]+-l, --list)|([\t ]+-d, --dbname)|([\t ]+-\?, --help)|([\t ]--help=)/'
}
sndev__prisma() {
shift
docker__exec -t -u apprunner app npx prisma "$@"
}
sndev__help_prisma() {
help="
run prisma commands
USAGE
$ sndev prisma [COMMAND]
COMMANDS"
echo "$help"
sndev__prisma --help | awk '/Commands/{y=1;next}y' | awk '!/^([\t ]+init)|([\t ]+studio)/' | sed -n '/Flags/q;p'
}
sndev__lint() {
shift
docker__exec -t -u apprunner app npm run lint
}
sndev__help_lint() {
help="
run linters
USAGE
$ sndev lint
"
echo "$help"
}
sndev__compose() {
shift
docker__compose "$@"
}
sndev__help_compose() {
docker__compose --help
}
sndev__sn_lndcli() {
shift
docker__sn_lnd -t "$@"
}
sndev__help_sn_lndcli() {
docker__sn_lnd --help
}
sndev__stacker_lndcli() {
shift
docker__stacker_lnd -t "$@"
}
sndev__help_stacker_lndcli() {
docker__stacker_lnd --help
}
sndev__stacker_clncli() {
shift
docker__stacker_cln -t "$@"
}
sndev__help_stacker_clncli() {
docker__stacker_cln help
}
sndev__stacker_litcli() {
shift
docker__exec -t litd litcli -n regtest --rpcserver localhost:8444 "$@"
}
sndev__help_stacker_litcli() {
docker__exec -t litd litcli -h
}
__sndev__pr_track() {
json=$(curl -fsSH "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/stackernews/stacker.news/pulls/$1")
case $(git config --get remote.origin.url) in
"http"*) url=$(echo "$json" | grep -e '"clone_url"' | head -n1 | sed -e 's/^.*"clone_url":[[:space:]]*"//; s/",[[:space:]]*$//') ;;
*) url=$(echo "$json" | grep -e '"ssh_url"' | head -n1 | sed -e 's/^.*"ssh_url":[[:space:]]*"//; s/",[[:space:]]*$//') ;;
esac
push=$(git remote -v | grep -e "$url .*push" | head -n1) || true
if [ -n "$push" ]; then
remote=$(echo "$push" | cut -f 1)
else
remote=$(echo "$json" | grep -e '"login"' | head -n1 | sed -e 's/^.*"login":[[:space:]]*"//; s/",[[:space:]]*$//')
git remote remove "$remote" 1>/dev/null 2>&1 || true
git remote add "$remote" "$url"
fi
ref=$(echo "$json" | grep -e '"ref"' | head -n1 | sed -e 's/^.*"ref":[[:space:]]*"//; s/",[[:space:]]*$//')
git fetch "$remote" "$ref"
git checkout -b "pr/$1" "$remote/$ref"
exit 0
}
__sndev__pr_detach() {
refspec="+refs/pull/$1/head:refs/remotes/pr/$1"
case $(git config --get remote.origin.url) in
"http"*) git fetch https://github.com/stackernews/stacker.news.git "$refspec" ;;
*) git fetch git@github.com:stackernews/stacker.news.git "$refspec" ;;
esac
git checkout "pr/$1"
exit 0
}
sndev__pr() {
shift
case $1 in
-t|--track)
call "__sndev__pr_track" "$2" ;;
*)
call "__sndev__pr_detach" "$1" ;;
esac
}
sndev__help_pr() {
help="
fetch and checkout a pr
USAGE
$ sndev pr [OPTIONS] <pr number>
OPTIONS
-t, --track track the pr in a new branch, creating a remote if necessary
defaults to checking out the pr in a detached state
"
echo "$help"
}
sndev__login() {
shift
if [ -z "$1" ]; then
echo "<nym> argument required"
sndev__help_login
exit 1
fi
# hardcode token for which is the hex digest of the sha256 of
# "SNDEV-TOKEN3_0W_PhDRZVanbeJsZZGIEljexkKoGbL6qGIqSwTjjI"
# next-auth concats the token with the secret from env and then sha256's it
token="d5fce54babffcb070c39f78d947761fd9ec37647fafcecb9734a3085a78e5c5e"
salt="202c90943c313b829e65e3f29164fb5dd7ea3370d7262c4159691c2f6493bb8b"
# upsert user with nym and nym@sndev.team
email="$1@sndev.team"
docker__exec db psql -U sn -d stackernews -q <<EOF
INSERT INTO users (name) VALUES ('$1') ON CONFLICT DO NOTHING;
UPDATE users SET email = '$email', "emailHash" = encode(digest(LOWER('$email')||'$salt', 'sha256'), 'hex') WHERE name = '$1';
INSERT INTO verification_requests (identifier, token, expires)
VALUES ('$email', '$token', NOW() + INTERVAL '1 day')
ON CONFLICT (token) DO UPDATE
SET identifier = '$email', expires = NOW() + INTERVAL '1 day';
EOF
echo
echo "open url in browser"
echo "http://localhost:3000/api/auth/callback/email?token=SNDEV-TOKEN&email=$1%40sndev.team"
echo
}
sndev__help_login() {
help="
login as a nym
USAGE
$ sndev login <nym>
"
echo "$help"
}
sndev__help() {
if [ $# -eq 2 ]; then
call "sndev__$1_$2" "$@"
exit 0
fi
help="
888
888
888
.d8888b 88888b. .d88888 .d88b. 888 888
88K 888 '88b d88' 888 d8P Y8b 888 888
'Y8888b. 888 888 888 888 88888888 Y88 88P
X88 888 888 Y88b 888 Y8b. Y8bd8P
88888P' 888 888 'Y88888 'Y8888 Y88P
manages a docker based stacker news development environment
USAGE
$ sndev [COMMAND]
$ sndev help [COMMAND]
COMMANDS
help show help
env:
start start env
stop stop env
restart restart env
status status of env
logs logs from env
delete delete env
sn:
login login as a nym
lnd:
fund pay a bolt11 for funding
withdraw create a bolt11 for withdrawal
cln:
cln_fund pay a bolt11 for funding with CLN
cln_withdraw create a bolt11 for withdrawal with CLN
db:
psql open psql on db
prisma run prisma commands
dev:
pr fetch and checkout a pr
lint run linters
open open container url in browser
other:
compose docker compose passthrough
sn_lndcli lncli passthrough on sn_lnd
stacker_lndcli lncli passthrough on stacker_lnd
stacker_clncli lightning-cli passthrough on stacker_cln
stacker_litcli litcli passthrough on litd
"
echo "$help"
}
call() {
func=$1
if type "$func" 1>/dev/null 2>&1; then
# if it's sndev COMMAND help, then call help for that command
case $3 in
-h|--help|help)
call "sndev__help_$2"
exit 0
;;
esac
shift # remove func from args
"$func" "$@" # invoke our named function w/ all remaining arguments
else
# if it's sndev -h COMMAND, then call help for that command
case $2 in
-h|--help)
call "sndev__help_$3"
exit 0
;;
esac
sndev__help
exit 1
fi
}
call "sndev__$1" "$@"