2024-09-20 23:20:54 -07:00
|
|
|
#!/usr/bin/bash
|
2022-10-01 11:16:15 -07:00
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
set -e
|
|
|
|
trap 'exit 1' SIGINT
|
|
|
|
|
2022-10-01 12:38:55 -07:00
|
|
|
# Constants
|
2024-09-20 23:25:25 -07:00
|
|
|
VERSION=2.0.1
|
2022-10-01 13:00:09 -07:00
|
|
|
template_dir="$HOME/Templates"
|
|
|
|
fd_opts=(--type f --threads "$(nproc)")
|
2022-10-01 11:16:15 -07:00
|
|
|
|
|
|
|
# Helper functions
|
|
|
|
function help() {
|
2022-10-01 14:42:36 -07:00
|
|
|
cat << HELPMESSAGE
|
2022-10-01 12:38:55 -07:00
|
|
|
$(basename "$0") $VERSION
|
2022-10-01 11:16:15 -07:00
|
|
|
|
|
|
|
Usage: $(basename "$0") [-h] [-d DIR] [-f] dest
|
|
|
|
|
|
|
|
Positional arguments:
|
|
|
|
dest
|
|
|
|
|
|
|
|
options:
|
|
|
|
-h, --help show this help message and exit
|
|
|
|
-d DIR, --template-dir DIR
|
|
|
|
choose a template directory (default: ~/Templates)
|
|
|
|
-f, --force overwrite dest if it exists
|
|
|
|
HELPMESSAGE
|
|
|
|
}
|
|
|
|
|
|
|
|
while true; do
|
2022-10-01 14:42:36 -07:00
|
|
|
case "${1}" in
|
|
|
|
'-d' | '--dir')
|
|
|
|
template_dir="${2}"
|
|
|
|
case "${template_dir}" in
|
|
|
|
"")
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift 2
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
--dir=*)
|
|
|
|
template_dir="${1#*=}"
|
|
|
|
case "${template_dir}" in
|
|
|
|
"")
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
'-f' | '--force')
|
|
|
|
FORCE_OVERWRITE='--force'
|
|
|
|
shift
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
'-h' | '--help')
|
|
|
|
help
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
printf '%s\n' "Unknown option: ${1}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
2022-10-01 11:16:15 -07:00
|
|
|
done
|
|
|
|
|
|
|
|
# If no target specified
|
|
|
|
if [[ -z "$1" ]]; then
|
2022-10-01 14:42:36 -07:00
|
|
|
help
|
|
|
|
exit 1
|
2022-10-01 11:16:15 -07:00
|
|
|
fi
|
|
|
|
|
2022-10-01 13:00:09 -07:00
|
|
|
# Check if template directory exists
|
|
|
|
if ! [[ -d "$template_dir" ]]; then
|
2022-10-01 14:42:36 -07:00
|
|
|
printf '%s\n' "Template directory doesn't exist, exiting."
|
|
|
|
exit 2
|
2022-10-01 11:16:15 -07:00
|
|
|
fi
|
|
|
|
|
2022-10-01 14:42:36 -07:00
|
|
|
files="$(fd "${fd_opts[@]}" -- . "$template_dir")"
|
2022-10-01 12:36:18 -07:00
|
|
|
selected_file="$(fzf --select-1 --exit-0 <<< "$files")"
|
2022-10-01 11:16:15 -07:00
|
|
|
|
|
|
|
# Check if target exists
|
|
|
|
if [[ -f "$1" && -z "$FORCE_OVERWRITE" ]]; then
|
2022-10-01 14:42:36 -07:00
|
|
|
printf '%s\n' 'File already exists, exiting'
|
|
|
|
exit 1
|
2022-10-01 11:16:15 -07:00
|
|
|
elif [[ -f "$1" && -n "$FORCE_OVERWRITE" ]]; then
|
2022-10-01 14:42:36 -07:00
|
|
|
cp --verbose --force -- "$selected_file" "$1"
|
2022-10-01 11:16:15 -07:00
|
|
|
else
|
2022-10-01 14:42:36 -07:00
|
|
|
cp --verbose -- "$selected_file" "$1"
|
2022-10-01 11:16:15 -07:00
|
|
|
fi
|