1
0
forked from a/repotool
repotool/fish-repotool/functions/repotool.fish

79 lines
2.0 KiB
Fish
Raw Normal View History

2025-07-01 22:07:16 +00:00
#!/usr/bin/env fish
# Set default REPOTOOL_PATH if not already set
if not set -q REPOTOOL_PATH
set -gx REPOTOOL_PATH "$HOME/repo"
end
function _activate_hook
set -l hook_name $argv[1]
set -l response $argv[2]
if test -f "$REPOTOOL_PATH/.hooks/$hook_name.fish"
source "$REPOTOOL_PATH/.hooks/$hook_name.fish" $response
return 0
else if test -f "$REPOTOOL_PATH/.hooks/$hook_name.sh"
source "$REPOTOOL_PATH/.hooks/$hook_name.sh" $response
return 0
else if test -f "$REPOTOOL_PATH/.hooks/$hook_name"
source "$REPOTOOL_PATH/.hooks/$hook_name" $response
return 0
end
return 1
end
function _parse_json
set -l response $argv[1]
set -l key $argv[2]
echo "$response" | jq -r ".$key"
end
function _handle_response
set -l response $argv[1]
# Validate JSON
if not echo "$response" | jq . >/dev/null 2>&1
# Not valid JSON, write to stderr
echo "$response" >&2
return
end
# Check if response has an echo field
set -l echo_msg (echo "$response" | jq -r '.echo // empty')
if test -n "$echo_msg"
echo "$echo_msg"
end
# Get hook name from response
set -l hook_name (echo "$response" | jq -r '.hook // empty')
# Handle before hook if hook name is provided
if test -n "$hook_name"
_activate_hook "before_{$hook_name}_cd" "$response"
end
# Check if response has a cd field
set -l cd_path (echo "$response" | jq -r '.cd // empty')
if test -n "$cd_path"
cd "$cd_path"
end
# Handle after hook if hook name is provided
if test -n "$hook_name"
_activate_hook "after_{$hook_name}_cd" "$response"
end
end
set TOOL_BIN "$REPOTOOL_PATH/.bin/repotool"
# Pass all arguments to repotool and handle response
set -l response ($TOOL_BIN $argv)
set -l exit_code $status
if test $exit_code -ne 0
echo "Command failed with exit code $exit_code" >&2
return $exit_code
end
_handle_response "$response"