| 1 | + | #!/bin/bash |
| 2 | + | # crash.fyi CLI |
| 3 | + | # description: Script to access crash.fyi REST API version 1 |
| 4 | + | # Author: James Hillyerd |
| 5 | + | # Porting: from inbucket by crash.fyi |
| 6 | + | # License: MIT |
| 7 | + | |
| 8 | + | API_HOST="crash.fyi" |
| 9 | + | URL_ROOT="https://$API_HOST:443/api/v1" |
| 10 | + | |
| 11 | + | set -eo pipefail |
| 12 | + | [ $TRACE ] && set -x |
| 13 | + | |
| 14 | + | usage() { |
| 15 | + | echo "Usage: $0 <command> [argument1 [argument2 [..]]]" >&2 |
| 16 | + | echo >&2 |
| 17 | + | echo "Options:" >&2 |
| 18 | + | echo " -h - show this help" >&2 |
| 19 | + | echo " -i - show HTTP headers" >&2 |
| 20 | + | echo >&2 |
| 21 | + | echo "Commands:" >&2 |
| 22 | + | echo " list <mailbox> - list mailbox contents" >&2 |
| 23 | + | echo " body <mailbox> <id> - print message body" >&2 |
| 24 | + | echo " source <mailbox> <id> - print message source" >&2 |
| 25 | + | echo " delete <mailbox> <id> - delete message" >&2 |
| 26 | + | echo " purge <mailbox> - delete all messages in mailbox" >&2 |
| 27 | + | } |
| 28 | + | |
| 29 | + | arg_check() { |
| 30 | + | declare command="$1" expected="$2" received="$3" |
| 31 | + | if [ $expected != $received ]; then |
| 32 | + | echo "Error: Command '$command' requires $expected arguments, but received $received" >&2 |
| 33 | + | echo >&2 |
| 34 | + | usage |
| 35 | + | exit 1 |
| 36 | + | fi |
| 37 | + | } |
| 38 | + | |
| 39 | + | main() { |
| 40 | + | # Process options |
| 41 | + | local curl_opts="" |
| 42 | + | local pretty="true" |
| 43 | + | for arg in $*; do |
| 44 | + | if [[ $arg == -* ]]; then |
| 45 | + | case "$arg" in |
| 46 | + | -h) |
| 47 | + | usage |
| 48 | + | exit |
| 49 | + | ;; |
| 50 | + | -i) |
| 51 | + | curl_opts="$curl_opts -i" |
| 52 | + | pretty="" |
| 53 | + | ;; |
| 54 | + | **) |
| 55 | + | echo "Unknown option: $arg" >&2 |
| 56 | + | echo |
| 57 | + | usage |
| 58 | + | exit 1 |
| 59 | + | ;; |
| 60 | + | esac |
| 61 | + | shift |
| 62 | + | else |
| 63 | + | break |
| 64 | + | fi |
| 65 | + | done |
| 66 | + | |
| 67 | + | # Store command |
| 68 | + | declare command="$1" |
| 69 | + | shift |
| 70 | + | |
| 71 | + | local url="" |
| 72 | + | local method="GET" |
| 73 | + | local is_json="" |
| 74 | + | |
| 75 | + | case "$command" in |
| 76 | + | body) |
| 77 | + | arg_check "$command" 2 $# |
| 78 | + | url="$URL_ROOT/mailbox/$1/$2" |
| 79 | + | is_json="true" |
| 80 | + | ;; |
| 81 | + | delete) |
| 82 | + | arg_check "$command" 2 $# |
| 83 | + | method=DELETE |
| 84 | + | url="$URL_ROOT/mailbox/$1/$2" |
| 85 | + | ;; |
| 86 | + | list) |
| 87 | + | arg_check "$command" 1 $# |
| 88 | + | url="$URL_ROOT/mailbox/$1" |
| 89 | + | is_json="true" |
| 90 | + | ;; |
| 91 | + | purge) |
| 92 | + | arg_check "$command" 1 $# |
| 93 | + | method=DELETE |
| 94 | + | url="$URL_ROOT/mailbox/$1" |
| 95 | + | ;; |
| 96 | + | source) |
| 97 | + | arg_check "$command" 2 $# |
| 98 | + | url="$URL_ROOT/mailbox/$1/$2/source" |
| 99 | + | ;; |
| 100 | + | *) |
| 101 | + | echo "Unknown command $command" >&2 |
| 102 | + | echo >&2 |
| 103 | + | usage |
| 104 | + | exit 1 |
| 105 | + | ;; |
| 106 | + | esac |
| 107 | + | |
| 108 | + | # Use jq to pretty-print if installed and we are expecting JSON output |
| 109 | + | if [ $pretty ] && [ $is_json ] && type -P jq >/dev/null; then |
| 110 | + | curl -s $curl_opts -H "Accept: application/json" --noproxy "$API_HOST" -X "$method" "$url" | jq . |
| 111 | + | else |
| 112 | + | curl -s $curl_opts -H "Accept: application/json" --noproxy "$API_HOST" -X "$method" "$url" |
| 113 | + | fi |
| 114 | + | } |
| 115 | + | |
| 116 | + | if [ $# -lt 1 ]; then |
| 117 | + | usage |
| 118 | + | exit 1 |
| 119 | + | fi |
| 120 | + | |
| 121 | + | main $* |
| 122 | + | |