diff --git a/pdsadmin/account.sh b/pdsadmin/account.sh index e9994e9..cd81058 100644 --- a/pdsadmin/account.sh +++ b/pdsadmin/account.sh @@ -6,66 +6,92 @@ set -o pipefail PDS_ENV_FILE="/pds/pds.env" source "${PDS_ENV_FILE}" -curl_cmd() { +# curl a URL and fail if the request fails. +function curl_cmd_get { curl --fail --silent --show-error "$@" } -curl_cmd_post() { +# curl a URL and fail if the request fails. +function curl_cmd_post { curl --fail --silent --show-error --request POST --header "Content-Type: application/json" "$@" } -curl_cmd_post_nofail() { +# curl a URL but do not fail if the request fails. +function curl_cmd_post_nofail { curl --silent --show-error --request POST --header "Content-Type: application/json" "$@" } +# The subcommand to run. SUBCOMMAND="${1:-}" +# +# account list +# if [[ "${SUBCOMMAND}" == "list" ]]; then - DIDS=$(curl_cmd \ + DIDS="$(curl_cmd \ "https://${PDS_HOSTNAME}/xrpc/com.atproto.sync.listRepos?limit=100" | jq --raw-output '.repos[].did' - ) + )" OUTPUT='[{"handle":"Handle","email":"Email","did":"DID"}' - for did in $DIDS; do - ITEM=$(curl_cmd \ + for did in ${DIDS}; do + ITEM="$(curl_cmd \ --user "admin:${PDS_ADMIN_PASSWORD}" \ - "https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.getAccountInfo?did=$did" - ) + "https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.getAccountInfo?did=${did}" + )" OUTPUT="${OUTPUT},${ITEM}" done OUTPUT="${OUTPUT}]" - echo $OUTPUT | jq --raw-output '.[] | [.handle, .email, .did] | @tsv' | column -t + echo "${OUTPUT}" | jq --raw-output '.[] | [.handle, .email, .did] | @tsv' | column --table + +# +# account create +# elif [[ "${SUBCOMMAND}" == "create" ]]; then EMAIL="${2:-}" HANDLE="${3:-}" + if [[ "${EMAIL}" == "" ]]; then + read -p "Enter an email address (e.g. alice@${PDS_HOSTNAME}): " EMAIL + fi + if [[ "${HANDLE}" == "" ]]; then + read -p "Enter a handle (e.g. alice.${PDS_HOSTNAME}): " HANDLE + fi + if [[ "${EMAIL}" == "" || "${HANDLE}" == "" ]]; then echo "ERROR: missing EMAIL and/or HANDLE parameters." >/dev/stderr echo "Usage: $0 ${SUBCOMMAND} " >/dev/stderr exit 1 fi - PASSWORD=$(openssl rand -base64 30 | tr -d "=+/" | cut -c1-24) - INVITE_CODE=$(curl_cmd_post \ + PASSWORD="$(openssl rand -base64 30 | tr -d "=+/" | cut -c1-24)" + INVITE_CODE="$(curl_cmd_post \ --user "admin:${PDS_ADMIN_PASSWORD}" \ --data '{"useCount": 1}' \ - https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createInviteCode | jq --raw-output '.code' - ) - RESULT=$(curl_cmd_post_nofail \ + "https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createInviteCode" | jq --raw-output '.code' + )" + RESULT="$(curl_cmd_post_nofail \ --data "{\"email\":\"${EMAIL}\", \"handle\":\"${HANDLE}\", \"password\":\"${PASSWORD}\", \"inviteCode\":\"${INVITE_CODE}\"}" \ - https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createAccount - ) + "https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createAccount" + )" - DID=$(echo $RESULT | jq --raw-output '.did') + DID="$(echo $RESULT | jq --raw-output '.did')" if [[ "${DID}" != did:* ]]; then - ERR=$(echo $RESULT | jq --raw-output '.message') + ERR="$(echo ${RESULT} | jq --raw-output '.message')" echo "ERROR: ${ERR}" >/dev/stderr echo "Usage: $0 ${SUBCOMMAND} " >/dev/stderr exit 1 fi - echo "Account created for ${HANDLE}.\nYour password is below, which we'll only show you once.\n" - echo "DID: ${DID}" - echo "Password: ${PASSWORD}" + echo "Account created successfully!" + echo "-----------------------------" + echo "Handle : ${HANDLE}" + echo "DID : ${DID}" + echo "Password : ${PASSWORD}" + echo "-----------------------------" + echo "Save this password, it will not be displayed again." + +# +# account delete +# elif [[ "${SUBCOMMAND}" == "delete" ]]; then DID="${2:-}" @@ -83,16 +109,20 @@ elif [[ "${SUBCOMMAND}" == "delete" ]]; then echo "This action is permanent." read -r -p "Are you sure you'd like to delete ${DID}? [y/N] " response - if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then + if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])$ ]]; then exit 0 fi curl_cmd_post \ - --user "admin:${PDS_ADMIN_PASSWORD}" \ - --data "{\"did\": \"${DID}\"}" \ - https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.deleteAccount >/dev/null + --user "admin:${PDS_ADMIN_PASSWORD}" \ + --data "{\"did\": \"${DID}\"}" \ + "https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.deleteAccount" >/dev/null echo "${DID} deleted" + +# +# account takedown +# elif [[ "${SUBCOMMAND}" == "takedown" ]]; then DID="${2:-}" TAKEDOWN_REF="$(date +%s)" @@ -109,7 +139,7 @@ elif [[ "${SUBCOMMAND}" == "takedown" ]]; then exit 1 fi - PAYLOAD=$(cat </dev/null + --user "admin:${PDS_ADMIN_PASSWORD}" \ + --data "${PAYLOAD}" \ + "https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.updateSubjectStatus" >/dev/null echo "${DID} taken down" + +# +# account untakedown +# elif [[ "${SUBCOMMAND}" == "untakedown" ]]; then DID="${2:-}" @@ -158,9 +192,9 @@ EOF ) curl_cmd_post \ - --user "admin:${PDS_ADMIN_PASSWORD}" \ - --data "${PAYLOAD}" \ - https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.updateSubjectStatus >/dev/null + --user "admin:${PDS_ADMIN_PASSWORD}" \ + --data "${PAYLOAD}" \ + "https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.updateSubjectStatus" >/dev/null echo "${DID} untaken down" else diff --git a/pdsadmin/create-invite-code.sh b/pdsadmin/create-invite-code.sh index 79d49e4..16df7ed 100644 --- a/pdsadmin/create-invite-code.sh +++ b/pdsadmin/create-invite-code.sh @@ -15,4 +15,4 @@ curl \ --user "admin:${PDS_ADMIN_PASSWORD}" \ --header "Content-Type: application/json" \ --data '{"useCount": 1}' \ - https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createInviteCode | jq --raw-output '.code' + "https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createInviteCode" | jq --raw-output '.code' diff --git a/pdsadmin/request-crawl.sh b/pdsadmin/request-crawl.sh index d7ee391..6181586 100644 --- a/pdsadmin/request-crawl.sh +++ b/pdsadmin/request-crawl.sh @@ -27,7 +27,7 @@ for host in ${RELAY_HOSTS//,/ }; do --user "admin:${PDS_ADMIN_PASSWORD}" \ --header "Content-Type: application/json" \ --data "{\"hostname\": \"${PDS_HOSTNAME}\"}" \ - https://${host}/xrpc/com.atproto.sync.requestCrawl >/dev/null + "https://${host}/xrpc/com.atproto.sync.requestCrawl" >/dev/null done echo "done" diff --git a/pdsadmin/update.sh b/pdsadmin/update.sh index 44b6887..97f816f 100644 --- a/pdsadmin/update.sh +++ b/pdsadmin/update.sh @@ -14,15 +14,15 @@ COMPOSE_TEMP_FILE="${COMPOSE_FILE}.tmp" echo "* Downloading PDS compose file" curl \ ---silent \ ---show-error \ ---fail \ ---output "${COMPOSE_TEMP_FILE}" \ -"${COMPOSE_URL}" + --silent \ + --show-error \ + --fail \ + --output "${COMPOSE_TEMP_FILE}" \ + "${COMPOSE_URL}" -if cmp -s "${COMPOSE_FILE}" "${COMPOSE_TEMP_FILE}"; then +if cmp --quiet "${COMPOSE_FILE}" "${COMPOSE_TEMP_FILE}"; then echo "PDS is already up to date" - rm "${COMPOSE_TEMP_FILE}" + rm --force "${COMPOSE_TEMP_FILE}" exit 0 fi