repotool/cli
2024-05-01 18:12:04 -05:00

515 lines
9.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# This script was generated by bashly 1.1.3 (https://bashly.dannyb.co)
# Modifying it manually is not recommended
# :wrapper.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n" >&2
exit 1
fi
# :command.master_script
# :command.version_command
version_command() {
echo "$version"
}
# :command.usage
cli_usage() {
if [[ -n $long_usage ]]; then
printf "cli - Sample application\n"
echo
else
printf "cli - Sample application\n"
echo
fi
printf "%s\n" "Usage:"
printf " cli COMMAND\n"
printf " cli [COMMAND] --help | -h\n"
printf " cli --version | -v\n"
echo
# :command.usage_commands
printf "%s\n" "Commands:"
printf " %s Download a file\n" "download"
printf " %s Upload a file\n" "upload "
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf " %s\n" "--version, -v"
printf " Show version number\n"
echo
# :command.usage_environment_variables
printf "%s\n" "Environment Variables:"
# :environment_variable.usage
printf " %s\n" "API_KEY"
printf " Set your API key\n"
echo
fi
}
# :command.usage
cli_download_usage() {
if [[ -n $long_usage ]]; then
printf "cli download - Download a file\n"
echo
else
printf "cli download - Download a file\n"
echo
fi
printf "Alias: d\n"
echo
printf "%s\n" "Usage:"
printf " cli download SOURCE [TARGET] [OPTIONS]\n"
printf " cli download --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_flags
# :flag.usage
printf " %s\n" "--force, -f"
printf " Overwrite existing files\n"
echo
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
# :command.usage_args
printf "%s\n" "Arguments:"
# :argument.usage
printf " %s\n" "SOURCE"
printf " URL to download from\n"
echo
# :argument.usage
printf " %s\n" "TARGET"
printf " Target filename (default: same as source)\n"
echo
# :command.usage_environment_variables
printf "%s\n" "Environment Variables:"
# :environment_variable.usage
printf " %s\n" "DEFAULT_TARGET_LOCATION"
printf " Set the default location to download to\n"
echo
# :command.usage_examples
printf "%s\n" "Examples:"
printf " cli download example.com\n"
printf " cli download example.com ./output -f\n"
echo
fi
}
# :command.usage
cli_upload_usage() {
if [[ -n $long_usage ]]; then
printf "cli upload - Upload a file\n"
echo
else
printf "cli upload - Upload a file\n"
echo
fi
printf "Alias: u\n"
echo
printf "%s\n" "Usage:"
printf " cli upload SOURCE [OPTIONS]\n"
printf " cli upload --help | -h\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_flags
# :flag.usage
printf " %s\n" "--user, -u USER (required)"
printf " Username to use for logging in\n"
echo
# :flag.usage
printf " %s\n" "--password, -p PASSWORD"
printf " Password to use for logging in\n"
echo
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
# :command.usage_args
printf "%s\n" "Arguments:"
# :argument.usage
printf " %s\n" "SOURCE"
printf " File to upload\n"
echo
fi
}
# :command.normalize_input
normalize_input() {
local arg flags
while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
flags="${BASH_REMATCH[1]}"
for ((i = 0; i < ${#flags}; i++)); do
input+=("-${flags:i:1}")
done
else
input+=("$arg")
fi
shift
done
}
# :command.inspect_args
inspect_args() {
if ((${#args[@]})); then
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
echo args:
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
else
echo args: none
fi
if ((${#other_args[@]})); then
echo
echo other_args:
echo "- \${other_args[*]} = ${other_args[*]}"
for i in "${!other_args[@]}"; do
echo "- \${other_args[$i]} = ${other_args[$i]}"
done
fi
if ((${#deps[@]})); then
readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
echo
echo deps:
for k in "${sorted_keys[@]}"; do echo "- \${deps[$k]} = ${deps[$k]}"; done
fi
}
# :command.command_functions
# :command.function
cli_download_command() {
# src/download_command.sh
echo "# this file is located in 'src/download_command.sh'"
echo "# code for 'cli download' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
}
# :command.function
cli_upload_command() {
# src/upload_command.sh
echo "# this file is located in 'src/upload_command.sh'"
echo "# code for 'cli upload' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
}
# :command.parse_requirements
parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--version | -v)
version_command
exit
;;
--help | -h)
long_usage=yes
cli_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action=${1:-}
case $action in
-*) ;;
download | d)
action="download"
shift
cli_download_parse_requirements "$@"
shift $#
;;
upload | u)
action="upload"
shift
cli_upload_parse_requirements "$@"
shift $#
;;
# :command.command_fallback
"")
cli_usage >&2
exit 1
;;
*)
printf "invalid command: %s\n" "$action" >&2
exit 1
;;
esac
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
printf "invalid argument: %s\n" "$key" >&2
exit 1
;;
esac
done
}
# :command.parse_requirements
cli_download_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h)
long_usage=yes
cli_download_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="download"
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--force | -f)
# :flag.case_no_arg
args['--force']=1
shift
;;
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
if [[ -z ${args['source']+x} ]]; then
args['source']=$1
shift
elif [[ -z ${args['target']+x} ]]; then
args['target']=$1
shift
else
printf "invalid argument: %s\n" "$key" >&2
exit 1
fi
;;
esac
done
# :command.required_args_filter
if [[ -z ${args['source']+x} ]]; then
printf "missing required argument: SOURCE\nusage: cli download SOURCE [TARGET] [OPTIONS]\n" >&2
exit 1
fi
}
# :command.parse_requirements
cli_upload_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h)
long_usage=yes
cli_upload_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="upload"
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--user | -u)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
args['--user']="$2"
shift
shift
else
printf "%s\n" "--user requires an argument: --user, -u USER" >&2
exit 1
fi
;;
# :flag.case
--password | -p)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
args['--password']="$2"
shift
shift
else
printf "%s\n" "--password requires an argument: --password, -p PASSWORD" >&2
exit 1
fi
;;
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
if [[ -z ${args['source']+x} ]]; then
args['source']=$1
shift
else
printf "invalid argument: %s\n" "$key" >&2
exit 1
fi
;;
esac
done
# :command.required_args_filter
if [[ -z ${args['source']+x} ]]; then
printf "missing required argument: SOURCE\nusage: cli upload SOURCE [OPTIONS]\n" >&2
exit 1
fi
# :command.required_flags_filter
if [[ -z ${args['--user']+x} ]]; then
printf "missing required flag: --user, -u USER\n" >&2
exit 1
fi
}
# :command.initialize
initialize() {
version="0.1.0"
long_usage=''
set -e
}
# :command.run
run() {
declare -A args=()
declare -A deps=()
declare -a other_args=()
declare -a input=()
normalize_input "$@"
parse_requirements "${input[@]}"
case "$action" in
"download") cli_download_command ;;
"upload") cli_upload_command ;;
esac
}
initialize
run "$@"