repotool/repotool.zsh
2025-05-12 12:23:17 -05:00

52 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env zsh
[[ -z "$REPOTOOL_PATH" ]] && export REPOTOOL_PATH="$HOME/repo"
_activate_hook() {
if [[ -f "$REPOTOOL_PATH/.hooks/$1.zsh" ]]; then
. "$REPOTOOL_PATH/.hooks/$1.zsh" $2
return 0
fi
if [[ -f "$REPOTOOL_PATH/.hooks/$1.sh" ]]; then
. "$REPOTOOL_PATH/.hooks/$1.sh" $2
return 0
fi
if [[ -f "$REPOTOOL_PATH/.hooks/$1" ]]; then
. "$REPOTOOL_PATH/.hooks/$1" $2
return 0
fi
return 1
}
TOOL_BIN="$REPOTOOL_PATH/.bin/repotool"
case "$1" in
get | g)
shift;
response=$($TOOL_BIN get $@)
if [[ $? != 0 ]] then;
echo "failed to get repo with args $@"
return $?
fi
declare -A obj
for item in ${(z)response}; do
parts=(${(s[=])item})
# NOTE: zsh is 1 indexed arrays
obj[${parts[1]}]=${parts[2]}
done
_activate_hook "before_cd" $response
cd ${obj[dir]}
_activate_hook "after_cd" $response
;;
'help' | "-h"| "-help" | "--help")
echo <<EOF
usage:
repo get <repo-name>
EOF
;;
"open")
raw_url=$(git ls-remote --get-url | cut -d '@' -f2 | sed 's/:/\//1')
xdg-open "https://${raw_url}"
;;
esac