mirror of
https://github.com/m-housh/dotfiles.git
synced 2026-02-15 22:52:37 +00:00
Compare commits
3 Commits
46186e2741
...
3446460827
| Author | SHA1 | Date | |
|---|---|---|---|
|
3446460827
|
|||
|
1e60f88ec4
|
|||
|
45666a5911
|
3
dev-env
3
dev-env
@@ -119,11 +119,12 @@ update_dirs $DEV_ENV/env/.local $HOME/.local
|
|||||||
|
|
||||||
# SCRIPTS
|
# SCRIPTS
|
||||||
mkdir -p ~/.local/scripts/{hypr,utils} >/dev/null 2>&1
|
mkdir -p ~/.local/scripts/{hypr,utils} >/dev/null 2>&1
|
||||||
mkdir -p ~/.local/scripts/utils/{kanatactl,hpa} >/dev/null 2>&1
|
mkdir -p ~/.local/scripts/utils/{kanatactl,hpa,shorten-url} >/dev/null 2>&1
|
||||||
update_dirs $DEV_ENV/env/.local/scripts/hypr $HOME/.local/scripts/hypr
|
update_dirs $DEV_ENV/env/.local/scripts/hypr $HOME/.local/scripts/hypr
|
||||||
copy_files $DEV_ENV/env/.local/scripts/hypr $HOME/.local/scripts/hypr
|
copy_files $DEV_ENV/env/.local/scripts/hypr $HOME/.local/scripts/hypr
|
||||||
copy_files "$DEV_ENV/env/.local/scripts/utils/kanatactl" "$HOME/.local/scripts/utils/kanatactl"
|
copy_files "$DEV_ENV/env/.local/scripts/utils/kanatactl" "$HOME/.local/scripts/utils/kanatactl"
|
||||||
copy_files "$DEV_ENV/env/.local/scripts/utils/hpa" "$HOME/.local/scripts/utils/hpa"
|
copy_files "$DEV_ENV/env/.local/scripts/utils/hpa" "$HOME/.local/scripts/utils/hpa"
|
||||||
|
copy_files "$DEV_ENV/env/.local/scripts/utils/shorten-url" "$HOME/.local/scripts/utils/shorten-url"
|
||||||
copy_files $DEV_ENV/env/.local/scripts $HOME/.local/scripts
|
copy_files $DEV_ENV/env/.local/scripts $HOME/.local/scripts
|
||||||
|
|
||||||
# SYSTEMD
|
# SYSTEMD
|
||||||
|
|||||||
68
env/.local/scripts/shorten-url
vendored
68
env/.local/scripts/shorten-url
vendored
@@ -4,14 +4,17 @@ set -e
|
|||||||
set -o nounset
|
set -o nounset
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
||||||
|
# Global variables
|
||||||
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
|
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
|
||||||
THIS_FILE=${BASH_SOURCE[0]}
|
THIS_FILE=${BASH_SOURCE[0]}
|
||||||
LOG_LABEL=$(basename "$THIS_FILE")
|
LOG_LABEL=$(basename "$THIS_FILE")
|
||||||
THIS=${THIS:-$LOG_LABEL}
|
THIS=${THIS:-$LOG_LABEL}
|
||||||
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
|
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"} && export LOG_FILE
|
||||||
|
|
||||||
API_KEY=$(/bin/gopass show --password Keys/shlink/api-key)
|
# Local variables
|
||||||
BASE_URL="http://roguemini.housh.dev:8880/rest/v3"
|
declare -a args
|
||||||
|
no_spin_flag="0"
|
||||||
|
create_flag="0"
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
@@ -20,8 +23,22 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Logging utility function, use in place of echo.
|
# Logging utility function, use in place of echo.
|
||||||
|
#
|
||||||
|
# This gets exported for subcommands to use.
|
||||||
log() {
|
log() {
|
||||||
logging log --source "$THIS_FILE" "$@"
|
logging log --source "$THIS_FILE" "$@"
|
||||||
|
} && export -f log
|
||||||
|
|
||||||
|
create() {
|
||||||
|
script="$SCRIPTS/utils/shorten-url/create"
|
||||||
|
export THIS="${THIS} create"
|
||||||
|
|
||||||
|
if [[ $no_spin_flag == "1" ]]; then
|
||||||
|
bash -c "$script ${args[*]}"
|
||||||
|
else
|
||||||
|
title="Generating short url..."
|
||||||
|
gum spin --show-output --title="$title" -- bash -c "$script ${args[*]}"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
@@ -32,40 +49,23 @@ log() {
|
|||||||
source "$SCRIPTS/hypr/logging"
|
source "$SCRIPTS/hypr/logging"
|
||||||
setup-logging "$LOG_FILE" "$LOG_LABEL"
|
setup-logging "$LOG_FILE" "$LOG_LABEL"
|
||||||
|
|
||||||
declare url shortCode tagsJson json
|
|
||||||
declare -a tags
|
|
||||||
shortCode=""
|
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
|
if [[ $1 == "--no-spin" ]]; then
|
||||||
usage && exit 0
|
no_spin_flag="1"
|
||||||
elif [[ $1 == "-c" ]] || [[ $1 == "--code" ]]; then
|
elif [[ $1 == "create" ]]; then
|
||||||
shift
|
create_flag="1"
|
||||||
shortCode="$1"
|
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
|
||||||
elif [[ $1 == "-t" ]] || [[ $1 == "--tag" ]]; then
|
no_spin_flag="1"
|
||||||
shift
|
args+=("$1")
|
||||||
tags+=("$1")
|
|
||||||
else
|
else
|
||||||
url="$1"
|
args+=("$1")
|
||||||
fi
|
fi
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
[[ -z $url ]] &&
|
if [[ $create_flag == "1" ]]; then
|
||||||
log --error "Url not supplied." &&
|
create
|
||||||
exit 1
|
else
|
||||||
|
# If we made it here, no subcommands were executed.
|
||||||
tagsJson=$(printf '%s\n' "${tags[@]}" | jq -R . | jq -s .)
|
usage
|
||||||
json=$(
|
fi
|
||||||
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'
|
|
||||||
|
|||||||
93
env/.local/scripts/utils/shorten-url/create
vendored
Executable file
93
env/.local/scripts/utils/shorten-url/create
vendored
Executable file
@@ -0,0 +1,93 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
set -o nounset
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
|
||||||
|
THIS_FILE=${BASH_SOURCE[0]} && export THIS_FILE
|
||||||
|
LOG_LABEL=$(basename "$THIS_FILE")
|
||||||
|
THIS=${THIS:-$LOG_LABEL}
|
||||||
|
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
|
||||||
|
|
||||||
|
# Setup environment
|
||||||
|
source "$SCRIPTS/utils/shorten-url/env"
|
||||||
|
|
||||||
|
declare -a tags
|
||||||
|
url=""
|
||||||
|
shortCode=""
|
||||||
|
expire=""
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
cat <<EOF
|
||||||
|
$THIS FIX ME!!!
|
||||||
|
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
|
||||||
7
env/.local/scripts/utils/shorten-url/env
vendored
Executable file
7
env/.local/scripts/utils/shorten-url/env
vendored
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
API_KEY=$(/bin/gopass show --password Keys/shlink/api-key)
|
||||||
|
BASE_URL="https://l.housh.dev/rest/v3"
|
||||||
|
|
||||||
|
export API_KEY
|
||||||
|
export BASE_URL
|
||||||
Reference in New Issue
Block a user