-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubectl-switch_ws
executable file
·113 lines (92 loc) · 2.94 KB
/
kubectl-switch_ws
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash
set -eu
_switchws_inline_fzf='fzf --height=50% --reverse -0 --inline-info --border --header-lines=1'
_exit='[exit]'
_parent='..'
_version='0.2.1'
help () {
# Display Help
echo "Usage: $0 [-h|-v]"
echo "Interactive workspace switcher for kcp."
echo
echo "Flags:"
echo "-h print this help."
echo "-v print version and exit."
}
print_workspace () {
echo -e "Workspace: \033[1;32m$1\033[0m"
}
printn () {
if [ ! -z "${1:-}" ]; then
echo "${1:-}\n"
fi
}
print_parent () {
if [[ ${1:-} == *":"* ]] || [[ ${1:-} == *"/"* ]]; then
printn "$_parent"
fi
}
# ensure prerequisites are present.
startup_check () {
if ! command -v kubectl-ws &> /dev/null; then
echo "error: could not find kubectl-ws. See https://docs.kcp.io/kcp/latest/setup/kubectl-plugin/ for installation instructions"
exit 1
fi
if ! command -v fzf &> /dev/null; then
echo "error: could not find fzf. See https://github.com/junegunn/fzf#installation for installation instructions"
exit 1
fi
if ! kubectl api-resources | grep -e '^workspaces' >/dev/null; then
echo "error: target does not expose workspace resource type. Is this a kcp environment?"
exit 1
fi
}
main () {
# print current workspace once when starting switch-ws.
local current_workspace=$(kubectl ws current --short)
print_workspace "$current_workspace"
# start main loop of plugin.
while true; do
# get child workspaces in current workspace.
output=$(kubectl get workspace -o custom-columns=NAME:.metadata.name,TYPE:.spec.type.name,SHARD:".metadata.annotations.internal\.tenancy\.kcp\.io/shard",PHASE:.status.phase)
header=$(echo -e "$output" | head -n1)
workspaces=$(echo -e "$output" | tail -n+2)
# construct list by injecting parent pseudo element into list and pipe into fzf for selection.
workspace=$(echo -e "$(printn "$header")$(print_parent "$current_workspace")$(printn "$workspaces")$_exit" | $_switchws_inline_fzf | awk '{print $1}')
if [ "$workspace" == "$_exit" ] || [ "$workspace" == "" ]; then
# if [exit] was selected (or nothing at all, corresponds to pressing ESC), the plugin should exit.
exit 0
else
# switch to selected workspace.
current_workspace=$(switch_workspace "$workspace")
fi
# delete previous output of workspace and print the new "current workspace".
echo -e "\e[1A\e[K$(print_workspace "$current_workspace")"
done
}
switch_workspace () {
local stderr=""
local stdout=""
{
IFS=$'\n' read -r -d '' stderr;
IFS=$'\n' read -r -d '' stdout;
} < <((printf '\0%s\0' "$(kubectl ws use --short "$1")" 1>&2) 2>&1)
local exitcode=$?
if [ $exitcode -ne 0 ]; then
echo "$stderr"
exit $exitcode
fi
echo "$stdout"
}
while getopts "hv" option; do
case $option in
h) # display help
help
exit 0;;
v)
echo "$_version"
exit 0;;
esac
done
startup_check
main