Files
dotfiles/env/.local/scripts/shorten-url

97 lines
1.9 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]}
LOG_LABEL=$(basename "$THIS_FILE")
THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"} && export LOG_FILE
# Local variables
declare -a args
no_spin_flag="0"
create_flag="0"
logs_flag="0"
usage() {
cat <<EOF
Shorten url utility script.
USAGE:
$THIS <flags> <command>
FLAGS:
--no-spin: Disable spinners for commands.
-h | --help: Show this help page.
COMMANDS:
create: Create a new shortened url.
logs: View or remove the logs.
Run '$THIS <command> --help' for more information on a command.
EOF
}
# Logging utility function, use in place of echo.
#
# This gets exported for subcommands to use.
log() {
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
}
logs() {
script="$SCRIPTS/utils/shorten-url/logs"
export THIS="$THIS logs"
source "$script" "${args[*]}"
}
################################################################################
# MAIN
################################################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL"
while [[ $# -gt 0 ]]; do
if [[ $1 == "--no-spin" ]]; then
no_spin_flag="1"
elif [[ $1 == "create" ]]; then
create_flag="1"
elif [[ $1 == "logs" ]]; then
logs_flag="1"
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
no_spin_flag="1"
args+=("$1")
else
args+=("$1")
fi
shift
done
if [[ $create_flag == "1" ]]; then
create
elif [[ $logs_flag == "1" ]]; then
logs
else
# If we made it here, no subcommands were executed.
usage
fi