mirror of
https://github.com/m-housh/dotfiles.git
synced 2026-02-13 22:02:34 +00:00
WIP: Moving shorten-url into seperate scripts / utils for gum spinner to work.
This commit is contained in:
48
env/.local/scripts/shorten-url
vendored
48
env/.local/scripts/shorten-url
vendored
@@ -10,15 +10,6 @@ LOG_LABEL=$(basename "$THIS_FILE")
|
||||
THIS=${THIS:-$LOG_LABEL}
|
||||
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
|
||||
|
||||
API_KEY=$(/bin/gopass show --password Keys/shlink/api-key)
|
||||
BASE_URL="http://roguemini.housh.dev:8880/rest/v3"
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
FIX ME!!!
|
||||
EOF
|
||||
}
|
||||
|
||||
# Logging utility function, use in place of echo.
|
||||
log() {
|
||||
logging log --source "$THIS_FILE" "$@"
|
||||
@@ -32,40 +23,7 @@ log() {
|
||||
source "$SCRIPTS/hypr/logging"
|
||||
setup-logging "$LOG_FILE" "$LOG_LABEL"
|
||||
|
||||
declare url shortCode tagsJson json
|
||||
declare -a tags
|
||||
shortCode=""
|
||||
script="$SCRIPTS/utils/shorten-url/create"
|
||||
title="Generating short url..."
|
||||
|
||||
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")
|
||||
else
|
||||
url="$1"
|
||||
fi
|
||||
shift
|
||||
done
|
||||
|
||||
[[ -z $url ]] &&
|
||||
log --error "Url not supplied." &&
|
||||
exit 1
|
||||
|
||||
tagsJson=$(printf '%s\n' "${tags[@]}" | jq -R . | jq -s .)
|
||||
json=$(
|
||||
jq -n --arg longUrl "$url" --arg shortCode "$shortCode" --argjson tags "$tagsJson" \
|
||||
'{longUrl: $longUrl, shortCode: $shortCode, tags: $tags, findIfExists: true}'
|
||||
)
|
||||
log "Creating shortend url with json: $json"
|
||||
|
||||
echo "$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 @- | jq '.shortUrl'
|
||||
gum spin --show-output --title="$title" -- bash -c "$script $*"
|
||||
|
||||
81
env/.local/scripts/utils/shorten-url/create
vendored
Executable file
81
env/.local/scripts/utils/shorten-url/create
vendored
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
|
||||
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
|
||||
THIS_FILE=${BASH_SOURCE[0]}
|
||||
LOG_LABEL=$(basename "$THIS_FILE")
|
||||
THIS=${THIS:-$LOG_LABEL}
|
||||
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
|
||||
|
||||
API_KEY=$(/bin/gopass show --password Keys/shlink/api-key)
|
||||
BASE_URL="https://l.housh.dev/rest/v3"
|
||||
|
||||
declare url shortCode
|
||||
declare -a tags
|
||||
shortCode=""
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
FIX ME!!!
|
||||
EOF
|
||||
}
|
||||
|
||||
# Logging utility function, use in place of echo.
|
||||
log() {
|
||||
logging log --source "$THIS_FILE" "$@"
|
||||
}
|
||||
|
||||
generate_json() {
|
||||
local tagsJson
|
||||
tagsJson=$(printf '%s\n' "${tags[@]}" | jq -R . | jq -s .)
|
||||
jq -n --arg longUrl "$url" --arg shortCode "$shortCode" --argjson tags "$tagsJson" \
|
||||
'{longUrl: $longUrl, shortCode: $shortCode, tags: $tags, findIfExists: true}'
|
||||
}
|
||||
|
||||
create_url() {
|
||||
local json
|
||||
|
||||
[[ -z $url ]] &&
|
||||
log --error "Url not supplied." &&
|
||||
exit 1
|
||||
|
||||
json="$(generate_json)"
|
||||
|
||||
log "Creating 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"
|
||||
|
||||
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")
|
||||
else
|
||||
url="$1"
|
||||
fi
|
||||
shift
|
||||
done
|
||||
|
||||
create_url
|
||||
Reference in New Issue
Block a user