feat: introduce chot

This commit is contained in:
inkch
2025-07-09 01:14:42 +09:00
parent 088009bfb8
commit acb9c4d7b4

74
bin/chot Normal file
View File

@@ -0,0 +1,74 @@
#!/bin/bash
set -euo pipefail
CHOT_SCRIPT="${CHOT_SCRIPT:-${XDG_DATA_HOME:-$HOME/.local/share}/chot/scripts}"
EDITOR="${EDITOR:-vi}"
usage() {
echo "Usage: chot [edit] <script-name> [args...]"
echo
echo " Run or edit one-off scripts stored in a user directory."
echo
echo " run <script> Run the script (default)"
echo " edit <script> Edit the script in \$EDITOR"
echo " help, --help Show this help message"
echo
echo "Scripts are stored in \$CHOT_SCRIPT, or by default:"
echo " \$XDG_DATA_HOME/chot/scripts or ~/.local/share/chot/scripts"
echo "The directory is created automatically when needed."
echo
[ -n "${CHOT_SCRIPT:-}" ] && echo "Current script directory: $CHOT_SCRIPT"
exit "${1:-1}"
}
# Intercept --help or help before evaluating $CHOT_SCRIPT
# (We don't want to mkdir or mutate the environment just to show help)
if [ "${1:-}" = "--help" ] || [ "${1:-}" = "help" ]; then
usage 0
fi
if [ $# -eq 0 ]; then
usage
fi
if [ ! -d "$CHOT_SCRIPT" ]; then
mkdir -p "$CHOT_SCRIPT"
echo "Created script directory: $CHOT_SCRIPT" >&2
fi
action="run"
if [ "$1" = "edit" ]; then
action="edit"
shift
fi
if [ $# -eq 0 ]; then
echo "Error: missing script name"
usage
fi
name="$1"
script="$CHOT_SCRIPT/$name"
shift || true # shift off the script name; remaining args go to the script
case "$action" in
run)
if [ ! -f "$script" ]; then
echo "No such script: $script"
echo "Try 'chot edit <name>' to create it,"
echo "or check CHOT_SCRIPT directory: $CHOT_SCRIPT"
exit 1
fi
if [ ! -x "$script" ]; then
chmod +x "$script"
fi
exec "$script" "$@"
;;
edit)
exec "$EDITOR" "$script"
;;
*)
usage
;;
esac