Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielFGray committed Jan 19, 2017
1 parent ae8b78f commit 232e532
Showing 1 changed file with 51 additions and 44 deletions.
95 changes: 51 additions & 44 deletions tekup
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
#!/usr/bin/env bash

declare verbose=0
declare processes=1
declare expire_length
declare request_deletion_key
declare log_file
declare -r c_red=$(tput setaf 1)
declare -r c_blue=$(tput setaf 4)
declare -r c_reset=$(tput sgr0)

info() {
printf "${c_blue}%s${c_reset}\n" "$1" >&2
}
export -f info
declare -A colors
colors[red]=$(tput setaf 1)
colors[reset]=$(tput sgr0)

err() {
printf "${c_red}%s${c_reset}\n" "$1" >&2
printf "%s%s%s\n" "${colors[red]}" "${colors[reset]}" "$*" >&2
}
export -f err

die() {
[[ -n "$1" ]] && err "$1"
exit 1
}
export -f err

usage() {
LESS=-FEXR less <<-'HELP'
Expand All @@ -32,48 +23,56 @@ Upload files to https://teknik.io
OPTIONS:
-h print this help
-v print verbose output. can be stacked. there are two levels of
verbosity (because this thing is over-engineered), the first
will print the JSON response from teknik, the second will also
pass the -v option to curl
-d request a deletion key for images
-e <STRING> expiration time. only has an effect on text pastes.
must be in the form of 'N UNITS' where N is a number
must be in the form of 'N UNIT' where N is a number
and UNIT is one of the following:
minute hour day month year
-v print verbose output. can be stacked. there are three levels of
verbosity (because this thing is over-engineered)
first will show progress from curl
second will print the json response
third will show verbose output from curl
if ~/.config/tekup/log is a writeable file tekup will always ask for a
deletion key and log it in that file
HELP
}

has() {
local verbose
local _verbose
if [[ $1 = '-v' ]]; then
verbose=1
_verbose=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( "$verbose" > 0 )) && err "$c not found"
(( _verbose > 0 )) && err "$c not found"
return 1
fi
done
}

upload_text() {
local file curl_opts
local file curl_opts expire_unit
file="$1"
shift
curl_opts=()
case "$verbose" in
0) curl_opts+=( -s ) ;;
2) curl_opts+=( -v ) ;;
1|2) curl_opts+=( ) ;;
*) curl_opts+=( -v ) ;;
esac

if [[ -n "$expire_length" ]]; then
printf -v expire_length '%d' "${expire_length% *}"
expire_unit="${expire_length#* }"
curl_opts+=( --data "expireLength=${expire_length}&expireUnit=${expire_unit/%s}" )
printf -v expire_l '%d' "${expire_length% *}"
expire_unit="${expire_l#* }"
curl_opts+=( --data "expireLength=${expire_l}&expireUnit=${expire_unit/%s}")
fi
curl_opts+=( --data "doNotTrack=true&title=${file##*/}" --data-urlencode "code=$(< "$file")" https://api.teknik.io/v1/Paste)
curl_opts+=(
--data "doNotTrack=true&title=${file##*/}"
--data-urlencode "code=$(< "$file")"
https://api.teknik.io/v1/Paste )
curl "${curl_opts[@]}" | parse_response
}

Expand All @@ -85,10 +84,11 @@ upload_file() {
curl_opts=()
case "$verbose" in
0) curl_opts+=( -s ) ;;
2) curl_opts+=( -v ) ;;
1|2) curl_opts+=( ) ;;
*) curl_opts+=( -v ) ;;
esac

[[ -n "$request_deletion_key" ]] &&
[[ -w "$log_file" || -n "$request_deletion_key" ]] &&
curl_opts+=( -F 'genDeletionKey=true' )
curl_opts+=( -F 'doNotTrack=true'
-F "contentType=${mime%%;*}"
Expand Down Expand Up @@ -116,19 +116,15 @@ upload_files() {
}

parse_response() {
local url deletion_key expire_date
local url deletion_key expire_date log_msg
read -r response

(( verbose > 0 )) && jq <<< "$response"
(( verbose > 1 )) && jq <<< "$response"

if [[ "$response" != *'http'* || -z "$response" || "$response" = *'error'* ]]; then
err "error uploading $file"
if (( verbose < 1 )); then
if [[ "$response" = *'error'* ]]; then
err "$(jq -r '.error.message' <<< "$response")"
elif [[ -n "$response" ]]; then
err "$response"
fi
err "$(jq -r '.error.message' <<< "$response")" || err "$response"
fi
die
fi
Expand All @@ -139,15 +135,26 @@ parse_response() {
url="${url/upload/u}"

printf '%s: %s\n' "$file" "$url"
if [[ -n "$request_deletion_key" ]]; then
deletion_key=$(jq -r '.result.deletion_key' <<< "$response")
# [[ "$deletion_key" != null ]] &&
printf 'deletion_key: %s/%s\n' "$url" "$deletion_key"
elif [[ -n "$expire_date" ]]; then
expire_date=$(jq -r '.result.expiration | match("[0-9]+").string' <<< "$response")
# [[ "$expire_date" = null ]] &&

if [[ -w "$log_file" || -n "$request_deletion_key" ]]; then
deletion_key=$(jq -r '.result.deletionKey' <<< "$response") || jq "$response"
[[ -n "$deletion_key" ]] && printf 'deletion key: %s/%s\n' "$url" "$deletion_key"
fi

if [[ -n "$expire_length" ]]; then
expire_date=$(jq -r '.result.expiration | match("[0-9]+").string' <<< "$response") || jq "$response"
[[ -n "$expire_date" ]] &&
printf 'expires at: %s\n' "$(date -d "@$expire_date")"
fi

if [[ -w "$log_file" ]]; then
log_msg="$(date +%s) | file: $file | url: $url"
[[ -n "$expire_date" ]] &&
log_msg+=" | expires: $expire_date"
[[ -n "$deletion_key" ]] &&
log_msg+=" | delete: $url/$deletion_key"
echo "$log_msg" >> "$log_file"
fi
}

has -v curl jq || die
Expand All @@ -162,7 +169,7 @@ while getopts 'hve:d' opt; do
esac
done
shift $(( OPTIND - 1 ))
unset OPTIND
unset opt OPTARG OPTIND

(( $# > 0 )) || die 'No files to upload.'

Expand Down

0 comments on commit 232e532

Please sign in to comment.