#!/bin/sh set -e set -a # automatically export all variables . ./.env.development if [ -f .env.local ]; then . ./.env.local fi docker__compose() { if [ ! -x "$(command -v docker)" ]; then echo "docker compose is not installed" echo "installation instructions are here: https://docs.docker.com/desktop/" exit 0 fi ENV_LOCAL= if [ -f .env.local ]; then ENV_LOCAL='--env-file .env.local' fi CURRENT_UID=$(id -u) CURRENT_GID=$(id -g) 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 "$@" } 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 if [ $# -eq 1 ]; then docker__compose logs -t --tail=1000 -f "$@" exit 0 fi 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__set_balance() { shift if [ -z "$1" ]; then echo "NYM argument required" sndev__help_set_balance exit 1 fi if [ -z "$2" ]; then echo "MSATS argument required" sndev__help_set_balance exit 2 fi if ! echo "$2" | grep -qE "^[0-9]+$"; then echo "MSATS argument is not a positive integer" sndev__help_set_balance exit 3 fi docker__exec db psql -U sn -d stackernews -q </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 -t -b "pr/$1" "$remote/$ref" git config --local "remote.$remote.push" pr/$1:$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 </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" "$@"