mirror of
https://github.com/m-housh/dotfiles.git
synced 2026-02-13 22:02:34 +00:00
113 lines
2.8 KiB
Bash
Executable File
113 lines
2.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
set -o nounset
|
|
set -o pipefail
|
|
|
|
# Global variables.
|
|
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
|
|
THIS_FILE=${BASH_SOURCE[0]} && export THIS_FILE # Export for log function to work properly.
|
|
LOG_LABEL=$(basename "$THIS_FILE")
|
|
THIS=${THIS:-$LOG_LABEL}
|
|
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
|
|
# Setup environment
|
|
source "$SCRIPTS/utils/shorten-url/env"
|
|
|
|
# Local variables.
|
|
declare -a tags
|
|
url=""
|
|
shortCode=""
|
|
expire=""
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
Create a shortened url, returns / echo's the generated url.
|
|
|
|
USAGE:
|
|
$THIS <flags> <url>
|
|
|
|
FLAGS:
|
|
-c | --code: Set a custom short-code for the link.
|
|
-e | --expire <time>: Set the expiration for the link.
|
|
-t | --tag <tag>: Add tag(s) to the link (can be passed multiple times).
|
|
-h | --help: Show this help page.
|
|
|
|
EXMAPLES:
|
|
|
|
Generate a link and set the expiration for 30 days from now:
|
|
$ $THIS --expire 30 days https://example.com
|
|
|
|
Generate a link with multiple tags:
|
|
$ $THIS --tag consult --tag test https://example.com
|
|
|
|
EOF
|
|
}
|
|
|
|
generate_json() {
|
|
local tagsJson
|
|
tagsJson=$(printf '%s\n' "${tags[@]}" | jq -R . | jq -s .)
|
|
log "Generating json data..."
|
|
if [[ -n $expire ]]; then
|
|
expire="$(date --iso-8601=seconds -d "+ $expire")"
|
|
log "Set valid until date: $expire"
|
|
# NB: 'validUntil' can not be set to an empty string, or it immediately expires the link.
|
|
jq -n --arg longUrl "$url" --arg shortCode "$shortCode" --argjson tags "$tagsJson" \
|
|
--arg validUntil "$expire" \
|
|
'{longUrl: $longUrl, shortCode: $shortCode, tags: $tags, findIfExists: true, validUntil: $validUntil}'
|
|
else
|
|
jq -n --arg longUrl "$url" --arg shortCode "$shortCode" --argjson tags "$tagsJson" \
|
|
'{longUrl: $longUrl, shortCode: $shortCode, tags: $tags, findIfExists: true}'
|
|
fi
|
|
}
|
|
|
|
create_url() {
|
|
local json
|
|
|
|
[[ -z $url ]] &&
|
|
log --error "Url not supplied." &&
|
|
exit 1
|
|
|
|
json="$(generate_json)"
|
|
|
|
log "Creating url: '$url' with json: $json"
|
|
|
|
curl "$BASE_URL/short-urls" \
|
|
--request 'POST' \
|
|
--header 'Content-Type: application/json' \
|
|
--header 'accept: application/json' \
|
|
--header "X-Api-Key: $API_KEY" \
|
|
--no-progress-meter \
|
|
--data "$json" | jq '.shortUrl'
|
|
}
|
|
|
|
################################################################################
|
|
# MAIN
|
|
################################################################################
|
|
|
|
# Setup logging file and label.
|
|
source "$SCRIPTS/hypr/logging"
|
|
setup-logging "$LOG_FILE" "$LOG_LABEL"
|
|
|
|
log "Creating url with args: $*"
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
|
|
usage && exit 0
|
|
elif [[ $1 == "-c" ]] || [[ $1 == "--code" ]]; then
|
|
shift
|
|
shortCode="$1"
|
|
elif [[ $1 == "-t" ]] || [[ $1 == "--tag" ]]; then
|
|
shift
|
|
tags+=("$1")
|
|
elif [[ $1 == "-e" ]] || [[ $1 == "--expire" ]]; then
|
|
shift
|
|
expire="$1 $2"
|
|
shift
|
|
else
|
|
url="$1"
|
|
fi
|
|
shift
|
|
done
|
|
|
|
create_url
|