stacker.news/sndev

351 lines
6.5 KiB
Plaintext
Raw Normal View History

2024-03-07 16:02:59 +00:00
#!/bin/sh
2024-03-11 20:30:35 +00:00
set -e
docker__compose() {
2024-03-11 20:27:18 +00:00
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
CURRENT_UID=$(id -u) CURRENT_GID=$(id -g) command docker compose --env-file .env.development "$@"
}
2024-03-10 21:29:10 +00:00
docker__exec() {
2024-03-11 20:27:18 +00:00
if [ ! -x "$(command -v docker)" ]; then
echo "docker is not installed"
echo "installation instructions are here: https://docs.docker.com/desktop/"
exit 0
fi
2024-03-10 23:22:54 +00:00
command docker exec -i "$@"
2024-03-10 21:29:10 +00:00
}
docker__sn_lnd() {
docker__exec -u lnd sn_lnd lncli "$@"
2024-03-10 21:42:16 +00:00
}
docker__stacker_lnd() {
docker__exec -u lnd stacker_lnd lncli "$@"
2024-03-10 21:42:16 +00:00
}
2024-03-11 00:42:07 +00:00
docker__sn_lnd() {
t=$1
if [ "$t" = "-t" ]; then
shift
else
t=""
fi
2024-03-11 20:27:18 +00:00
docker__exec "$t" -u lnd sn_lnd lncli "$@"
2024-03-11 00:42:07 +00:00
}
docker__stacker_lnd() {
t=$1
if [ "$t" = "-t" ]; then
shift
else
t=""
fi
2024-03-11 20:27:18 +00:00
docker__exec "$t" -u lnd stacker_lnd lncli "$@"
2024-03-11 00:42:07 +00:00
}
sndev__start() {
2024-03-11 20:27:18 +00:00
shift
if ! [ -f .env.development ]; then
echo ".env.development does not exist ... creating from .env.sample"
cp .env.sample .env.development
2024-03-13 01:04:12 +00:00
elif ! git diff --exit-code --diff-algorithm=histogram .env.sample .env.development; then
echo ".env.development is different from .env.sample ..."
echo "do you want to merge .env.sample into .env.development? [y/N]"
read -r answer
if [ "$answer" = "y" ]; then
# merge .env.sample into .env.development in a posix compliant way
git merge-file --theirs .env.development /dev/fd/3 3<<-EOF /dev/fd/4 4<<-EOF
$(git show HEAD:.env.sample)
EOF
$(cat .env.sample)
EOF
else
echo "merge cancelled"
fi
fi
2024-03-11 20:27:18 +00:00
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]
OPTIONS"
echo "$help"
docker__compose up --help | awk '/Options:/{y=1;next}y'
}
sndev__stop() {
2024-03-11 20:27:18 +00:00
shift
docker__compose down "$@"
}
sndev__help_stop() {
help="
stop the sndev env
USAGE
$ sndev stop [OPTIONS]
OPTIONS"
echo "$help"
docker__compose down --help | awk '/Options:/{y=1;next}y'
}
sndev__restart() {
2024-03-11 20:27:18 +00:00
shift
docker__compose restart "$@"
}
sndev__help_restart() {
help="
restart the sndev env
USAGE
$ sndev restart [OPTIONS]
OPTIONS"
echo "$help"
docker__compose restart --help | awk '/Options:/{y=1;next}y'
}
2024-03-10 22:10:38 +00:00
sndev__status() {
shift
2024-03-11 20:27:18 +00:00
if [ $# -eq 0 ]; then
docker__compose ps -a --format 'table {{.Service}}\t{{.State}}\t{{.Status}}\t{{.Label "CONNECT"}}'
2024-03-11 20:27:18 +00:00
exit 0
fi
docker__compose ps "$@"
}
sndev__help_status() {
help="
show container status of sndev env
USAGE
$ sndev status [OPTIONS]
OPTIONS"
echo "$help"
docker__compose ps --help | awk '/Options:/{y=1;next}y'
2024-03-10 22:10:38 +00:00
}
sndev__delete() {
echo "this will delete the 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
}
2024-03-11 20:27:18 +00:00
sndev__help_delete() {
help="
remove orphans and volumes from sndev env
equivalent to sndev stop --volumes --remove-orphans
USAGE
$ sndev delete
"
echo "$help"
}
2024-03-10 21:42:16 +00:00
sndev__fund() {
shift
2024-03-11 00:42:07 +00:00
docker__stacker_lnd -t payinvoice "$@"
2024-03-10 21:42:16 +00:00
}
2024-03-11 20:27:18 +00:00
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/'
}
2024-03-10 21:42:16 +00:00
sndev__withdraw() {
2024-03-10 21:29:10 +00:00
shift
docker__stacker_lnd addinvoice --amt "$@" | jq -r '.payment_request'
}
2024-03-11 20:27:18 +00:00
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/'
}
2024-03-10 23:22:54 +00:00
sndev__psql() {
2024-03-11 20:27:18 +00:00
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=)/'
2024-03-10 23:22:54 +00:00
}
2024-03-11 13:54:38 +00:00
sndev__prisma() {
2024-03-11 20:27:18 +00:00
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'
2024-03-11 13:54:38 +00:00
}
sndev__compose() {
shift
docker__compose "$@"
}
2024-03-11 20:27:18 +00:00
sndev__help_compose() {
docker__compose --help
}
sndev__sn_lncli() {
shift
2024-03-10 23:22:54 +00:00
docker__sn_lnd -t "$@"
}
2024-03-11 20:27:18 +00:00
sndev__help_sn_lncli() {
docker__sn_lnd --help
}
sndev__stacker_lncli() {
shift
2024-03-10 23:22:54 +00:00
docker__stacker_lnd -t "$@"
2024-03-10 21:29:10 +00:00
}
2024-03-11 20:27:18 +00:00
sndev__help_stacker_lncli() {
docker__stacker_lnd --help
}
sndev__help() {
2024-03-11 20:27:18 +00:00
if [ $# -eq 2 ]; then
call "sndev__$1_$2" "$@"
exit 0
fi
help="
2024-03-10 21:59:15 +00:00
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]
2024-03-11 20:27:18 +00:00
$ sndev help [COMMAND]
COMMANDS
2024-03-10 23:41:21 +00:00
help show help
env:
start start env
stop stop env
restart restart env
status status of env
delete delete env
lnd:
fund pay a bolt11 for funding
2024-03-10 23:41:21 +00:00
withdraw create a bolt11 for withdrawal
2024-03-10 23:22:54 +00:00
db:
psql open psql on db
2024-03-11 13:54:38 +00:00
prisma run prisma commands
2024-03-10 23:22:54 +00:00
2024-03-11 13:54:38 +00:00
other:
2024-03-10 23:41:21 +00:00
compose docker compose passthrough
sn_lncli lncli passthrough on sn_lnd
stacker_lncli lncli passthrough on stacker_lnd
"
2024-03-11 20:27:18 +00:00
echo "$help"
}
call() {
func=$1
if type "$func" 1>/dev/null 2>&1; then
2024-03-11 20:27:18 +00:00
# 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
2024-03-11 20:27:18 +00:00
# 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" "$@"