#!/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 } _parse_json() { local response="$1" local key="$2" echo "$response" | jq -r ".$key" } _handle_response() { local response="$1" # Validate JSON if ! echo "$response" | jq . >/dev/null 2>&1; then # Not valid JSON, write to stderr echo "$response" >&2 return fi # 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 # Get hook name from response local hook_name=$(echo "$response" | jq -r '.hook // empty') # Handle before hook if hook name is provided if [[ -n "$hook_name" ]]; then _activate_hook "before_${hook_name}_cd" "$response" fi # 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 # Handle after hook if hook name is provided if [[ -n "$hook_name" ]]; then _activate_hook "after_${hook_name}_cd" "$response" fi } TOOL_BIN="$REPOTOOL_PATH/.bin/repotool" # 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 _handle_response "$response"