#!/usr/bin/env bash # Parse options POSITIONAL=() while [[ $# -gt 0 ]]; do key="$1" LOG=false case $key in --token) TOKEN="$2" shift shift ;; --device) DEVICE="$2" shift shift ;; --volume-name) VOLUME_NAME="$2" shift shift ;; --volume-region) VOLUME_REGION="$2" shift shift ;; --buffer) BUFFER="$2" shift shift ;; --log) LOG=true shift shift ;; *) POSITIONAL+=("$1") shift ;; esac done set -- "${POSITIONAL[@]}" log () { prefix="" if [[ "$LOG" = true ]]; then prefix="$(date "+%Y-%m-%d %H:%M:%S") " fi echo $prefix$@ } log "DEBUG" log "=====" log "TOKEN = ${TOKEN}" log "DEVICE = ${DEVICE}" log "VOLUME_NAME = ${VOLUME_NAME}" log "VOLUME_REGION = ${VOLUME_REGION}" log "BUFFER = ${BUFFER}" log digital_ocean_api () { api="https://api.digitalocean.com/v2" endpoint=$1 curl --silent -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" "${api}/${endpoint}" } main () { # Get volume data log "Getting data for volume \"${VOLUME_NAME}\" in region \"${VOLUME_REGION}\"..." volume_json=$(digital_ocean_api "volumes?region=${VOLUME_REGION}" | jq -r --arg VOLUME_NAME "${VOLUME_NAME}" '.volumes | .[] | select(.name==$VOLUME_NAME)') volume_id=$(echo $volume_json | jq -r .id) volume_size=$(echo $volume_json | jq -r .size_gigabytes) log "Volume ID is \"${volume_id}\" and is currently ${volume_size}GB" } main