Skip to content

Commit 82c473a

Browse files
committed
chore: update dev-env.zsh for scoped tasks
Updates `cp-follow` task to support host-scoped tasks. PLAT-347
1 parent 67a8b01 commit 82c473a

File tree

1 file changed

+59
-16
lines changed

1 file changed

+59
-16
lines changed

hack/dev-env.zsh

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,33 @@ _use-test-config() {
6969
done
7070
}
7171

72+
_choose-scope() {
73+
# (j:\n:) joins the array with newlines
74+
local scope_choice=$(echo "host\ndatabase" | sk)
75+
76+
if [[ -z "${scope_choice}" ]]; then
77+
return 1
78+
fi
79+
80+
local scope=$(jq -r '.id' <<<"${scope_choice}")
81+
echo "using scope ${scope}" >&2
82+
echo "${scope_choice}"
83+
}
84+
85+
_choose-host() {
86+
local host_choice=$(restish host-1 list-hosts \
87+
| jq -c '.hosts[]? | { id, state: .status.state, hostname, ipv4_address }' \
88+
| sk --preview 'echo {} | jq')
89+
90+
if [[ -z "${host_choice}" ]]; then
91+
return 1
92+
fi
93+
94+
local host_id=$(jq -r '.id' <<<"${host_choice}")
95+
echo "using database ${host_id}" >&2
96+
echo "${host_choice}"
97+
}
98+
7299
_choose-database() {
73100
local database_choice=$(restish host-1 list-databases \
74101
| jq -c '.databases[]? | { id, state, created_at, updated_at }' \
@@ -112,7 +139,11 @@ _choose-user() {
112139
}
113140

114141
_choose-task() {
115-
local task_choice=$(restish host-1 list-database-tasks $1 \
142+
local scope="$1"
143+
local entity_id="$2"
144+
local task_choice=$(restish host-1 list-tasks \
145+
--scope "${scope}" \
146+
--entity-id "${entity_id}" \
116147
| jq -c '.tasks[]?' \
117148
| sk --preview 'echo {} | jq')
118149

@@ -486,12 +517,14 @@ EOF
486517
}
487518

488519
cp-follow-task() {
489-
local o_database_id
520+
local o_scope
521+
local o_entity_id
490522
local o_task_id
491523
local o_help
492524

493525
zparseopts -D -F -K -- \
494-
{d,-database-id}:=o_database_id \
526+
{s,-scope}:=o_scope \
527+
{e,-entity-id}:=o_entity_id \
495528
{t,-task-id}:=o_task_id \
496529
{h,-help}=o_help || return
497530

@@ -500,7 +533,8 @@ cp-follow-task() {
500533
return
501534
fi
502535

503-
local database_id="${o_database_id[-1]}"
536+
local scope="${o_scope[-1]}"
537+
local entity_id="${o_entity_id[-1]}"
504538
local task_id="${o_task_id[-1]}"
505539

506540
# If we have an api response on stdin, we'll try to extract the database ID
@@ -511,27 +545,36 @@ cp-follow-task() {
511545
# echo the input for visibility
512546
echo "${input}"
513547

514-
database_id=$(<<<"${input}" jq -r '.task.database_id')
548+
scope=$(<<<"${input}" jq -r '.task.scope')
549+
entity_id=$(<<<"${input}" jq -r '.task.entity_id')
515550
task_id=$(<<<"${input}" jq -r '.task.task_id')
516551

517-
if [[ -z "${database_id}" || -z "${task_id}" ]]; then
552+
if [[ -z "${scope}" || -z "${entity_id}" || -z "${task_id}" ]]; then
518553
echo "no task object found on stdin" >&2
519554
return 1
520555
fi
521556
fi
522557

523-
if [[ -z "${database_id}" ]]; then
524-
local database=$(_choose-database)
558+
if [[ -z "${scope}" ]]; then
559+
local scope=$(_choose-scope)
560+
561+
if [[ -z "${scope}" ]]; then
562+
return 1
563+
fi
564+
fi
565+
566+
if [[ -z "${entity_id}" ]]; then
567+
local entity=$(_choose-${scope})
525568

526-
if [[ -z "${database}" ]]; then
569+
if [[ -z "${entity}" ]]; then
527570
return 1
528571
fi
529572

530-
database_id=$(<<<"${database}" jq -r '.id')
573+
entity_id=$(<<<"${entity}" jq -r '.id')
531574
fi
532575

533576
if [[ -z "${task_id}" ]]; then
534-
local task=$(_choose-task "${database_id}")
577+
local task=$(_choose-task "${scope}" "${entity_id}")
535578

536579
if [[ -z "${task}" ]]; then
537580
return 1
@@ -548,8 +591,8 @@ cp-follow-task() {
548591
while :; do
549592
# Get next set of entries
550593
resp=$(restish host-1 \
551-
get-database-task-log \
552-
${database_id} \
594+
get-${scope}-task-log \
595+
${entity_id} \
553596
${task_id} \
554597
--after-entry-id "${last_entry_id}")
555598

@@ -564,15 +607,15 @@ cp-follow-task() {
564607
last_entry_id=${next_last_entry_id}
565608
fi
566609

567-
sleep 1s
568-
569610
[[ "${task_status}" != "completed" && \
570611
"${task_status}" != "failed" && \
571612
"${task_status}" != "canceled" ]] || break
613+
614+
sleep 1s
572615
done
573616

574617
echo
575-
echo "database ${database_id} task ${task_id} ${task_status}"
618+
echo "${scope} entity ${entity_id} task ${task_id} ${task_status}"
576619
}
577620

578621
#########

0 commit comments

Comments
 (0)