forked from tilt-dev/tilt-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconcile_cancel_btn.sh
executable file
·83 lines (74 loc) · 2.19 KB
/
reconcile_cancel_btn.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
#
# Reconcilation for a cancel button.
#
# Given the name of a Cmd object, we check:
# 1) Does this need a cancel button attached?
# 2) If so, attach a cancel button now with the correct PID.
# 3) If not, delete the old cancel button.
set -euo pipefail
cmd_name="$1"
echo "reconciling $cmd_name"
cancel_cmd_name="$cmd_name:cancel"
cancel_button_name="$cmd_name:cancel"
cmd=$(tilt get cmd -o json --ignore-not-found -- "$cmd_name")
function delete_children {
tilt delete cmd --ignore-not-found -- "$cancel_cmd_name"
tilt delete uibutton --ignore-not-found -- "$cancel_button_name"
exit 0
}
# Check if the object has been deleted.
name_in_json=$(echo "$cmd" | jq -r '.metadata.name')
if [[ "$cmd" == "" || "$name_in_json" == "null" ]]; then
delete_children
fi
# Ignore commands that don't belong to a resource.
resource=$(echo "$cmd" | jq -r '.metadata.annotations["tilt.dev/resource"]')
if [[ "$resource" == "null" ]]; then
exit 0
fi
# Ignore cmds that don't belong to the Tiltfile or a local_resource.
owner_kind=$(echo "$cmd" | jq -r '.metadata.labels["tilt.dev/owner-kind"]')
if [[ "$owner_kind" == "null" ]]; then
owner_kind=$(echo "$cmd" | jq -r '.metadata.annotations["tilt.dev/owner-kind"]')
fi
if [[ "$owner_kind" == "null" ]]; then
owner_kind=$(echo "$cmd" | jq -r '.metadata.ownerReferences[0]["kind"]')
fi
if [[ "$owner_kind" != "Tiltfile" && "$owner_kind" != "CmdServer" ]]; then
exit 0
fi
# If the command isn't running or doesn't exist, disable the button
pid=$(echo "$cmd" | jq -r '.status.running.pid')
disabled="false"
if [[ "$pid" == "" || "$pid" == "null" ]]; then
disabled="true"
fi
# TODO(nick): Add Icons to the buttons
dir=$(realpath "$(dirname "$0")")
cat <<EOF | tilt apply -f -
apiVersion: tilt.dev/v1alpha1
kind: UIButton
metadata:
name: $cancel_button_name
spec:
disabled: $disabled
text: Cancel
location:
componentType: resource
componentID: $resource
---
apiVersion: tilt.dev/v1alpha1
kind: Cmd
metadata:
name: $cancel_cmd_name
annotations:
"tilt.dev/resource": "$resource"
"tilt.dev/log-span-id": "$cancel_cmd_name"
spec:
args: ["./kill_cmd.sh", "$cmd_name"]
dir: $dir
startOn:
uiButtons:
- $cancel_button_name
EOF