Add hr call

This commit is contained in:
Sage 2026-01-21 15:01:33 +00:00
parent 814bcab514
commit 64f27448ea

View file

@ -7,6 +7,8 @@ _hr_usage() {
echo "Usage: hr <command>"
echo "Commands:"
echo " switch Switch PROJECT_ID between mk2-test and mk2-prod"
echo " call Call a Cloud Run service route"
echo " cf Call a Cloud Function"
echo " init py Initialize a python devenv environment (git-ignored)"
echo " freeze Freeze dependencies to requirements.txt"
}
@ -118,6 +120,112 @@ _hr_freeze() {
uv pip freeze --exclude-editable >>requirements.txt
}
_hr_call() {
local route_arg="$1"
shift
if [[ -z "$route_arg" ]]; then
echo "Usage: hr call <route-name>[/path] <options>"
return 1
fi
local service_name
local url_path
if [[ "$route_arg" == */* ]]; then
service_name="${route_arg%%/*}"
url_path="/${route_arg#*/}"
else
service_name="$route_arg"
url_path=""
fi
local project_number
if [[ "$PROJECT_ID" == "mk2-prod" ]]; then
project_number="1013087376822"
else
project_number="322048751601"
fi
local json_payload="{}"
while [[ $# -gt 0 ]]; do
if [[ "$1" == -* ]]; then
local key="${1#-}"
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: Missing value for option $key"
return 1
fi
local value="$2"
# Use jq to safely construct JSON
if command -v jq >/dev/null; then
json_payload=$(echo "$json_payload" | jq --arg k "$key" --arg v "$value" '.[$k] = $v')
else
echo "Error: jq is required but not installed."
return 1
fi
shift 2
else
echo "Error: Unexpected argument '$1'"
return 1
fi
done
local url="https://${service_name}-${project_number}.europe-west1.run.app${url_path}"
echo "Calling $url..."
curl "$url" \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-H "Content-Type: application/json" \
-d "$json_payload"
}
_hr_cf() {
local function_name="$1"
shift
if [[ -z "$function_name" ]]; then
echo "Usage: hr cf <function-name> <options>"
return 1
fi
local json_payload="{}"
while [[ $# -gt 0 ]]; do
if [[ "$1" == -* ]]; then
local key="${1#-}"
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: Missing value for option $key"
return 1
fi
local value="$2"
# Use jq to safely construct JSON
if command -v jq >/dev/null; then
json_payload=$(echo "$json_payload" | jq --arg k "$key" --arg v "$value" '.[$k] = $v')
else
echo "Error: jq is required but not installed."
return 1
fi
shift 2
else
echo "Error: Unexpected argument '$1'"
return 1
fi
done
local url="https://europe-west1-${PROJECT_ID}.cloudfunctions.net/${function_name}"
echo "Calling $url..."
curl "$url" \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-H "Content-Type: application/json" \
-d "$json_payload"
}
hr() {
if [[ $# -eq 0 ]]; then
_hr_usage
@ -160,6 +268,12 @@ hr() {
_hr_init_py
elif [ "$1" = "freeze" ]; then
_hr_freeze
elif [ "$1" = "call" ]; then
shift
_hr_call "$@"
elif [ "$1" = "cf" ]; then
shift
_hr_cf "$@"
else
_hr_usage
exit 1