repotool/shell/repotool.zsh

76 lines
1.7 KiB
Bash
Raw Normal View History

2024-05-02 00:43:43 +00:00
#!/usr/bin/env zsh
[[ -z "$REPOTOOL_PATH" ]] && export REPOTOOL_PATH="$HOME/repo"
2025-05-12 17:09:06 +00:00
2024-05-02 00:43:43 +00:00
_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
}
2025-05-12 17:09:06 +00:00
2025-06-30 23:46:08 +00:00
_parse_json() {
local response="$1"
local key="$2"
echo "$response" | jq -r ".$key"
}
_handle_response() {
local response="$1"
2025-07-01 00:56:36 +00:00
2025-06-30 23:46:08 +00:00
# Validate JSON
if ! echo "$response" | jq . >/dev/null 2>&1; then
# Not valid JSON, write to stderr
echo "$response" >&2
return
fi
2025-07-01 00:56:36 +00:00
2025-06-30 23:46:08 +00:00
# Check if response has an echo field
local echo_msg=$(echo "$response" | jq -r '.echo // empty')
if [[ -n "$echo_msg" ]]; then
echo "$echo_msg"
fi
2025-07-01 00:56:36 +00:00
2025-06-30 23:46:08 +00:00
# Get hook name from response
local hook_name=$(echo "$response" | jq -r '.hook // empty')
2025-07-01 00:56:36 +00:00
2025-06-30 23:46:08 +00:00
# Handle before hook if hook name is provided
if [[ -n "$hook_name" ]]; then
_activate_hook "before_${hook_name}_cd" "$response"
fi
2025-07-01 00:56:36 +00:00
2025-06-30 23:46:08 +00:00
# Check if response has a cd field
local cd_path=$(echo "$response" | jq -r '.cd // empty')
if [[ -n "$cd_path" ]]; then
cd "$cd_path"
fi
2025-07-01 00:56:36 +00:00
2025-06-30 23:46:08 +00:00
# Handle after hook if hook name is provided
if [[ -n "$hook_name" ]]; then
_activate_hook "after_${hook_name}_cd" "$response"
fi
}
2025-05-12 17:09:06 +00:00
TOOL_BIN="$REPOTOOL_PATH/.bin/repotool"
2025-06-30 23:46:08 +00:00
# Pass all arguments to repotool and handle response
response=$($TOOL_BIN "$@")
exit_code=$?
if [[ $exit_code != 0 ]]; then
echo "Command failed with exit code $exit_code" >&2
return $exit_code
fi
2025-07-01 00:56:36 +00:00
_handle_response "$response"