-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkd
More file actions
executable file
·156 lines (136 loc) · 4.25 KB
/
kd
File metadata and controls
executable file
·156 lines (136 loc) · 4.25 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash
# kd - A Terminal User Interface wrapper for Kubernetes commands
#
# kd is a wrapper around common kubernetes commands to simplify running various
# k8s tasks without needing to remember commands or rely on bash-completion.
# It supplies a text UI (optionally using the 'dialog' tool) for prompts.
#
# Run 'kd' and select a command, or select DEFAULT to always use the defaults.
set -eu
[ "${DEBUG:-0}" = "1" ] && set -x
HAS_DIALOG=0
__cleanup () {
if [ -n "${tmpfile:-}" ] ; then rm -f "$tmpfile" ; fi
}
trap __cleanup EXIT
__err () { printf "%s: Error: %s\n" "$0" "$*" 1>&2 ; }
__errexit () { __err "$*" ; exit 1 ; }
__cmd_commands () {
if [ $# -lt 1 ] ; then
echo "gcloud_config_configurations"
echo "kubectx"
echo "kubens"
else
# Run any '__cmd_*' function name passed, with arguments
local cmd="$1"; shift; __cmd_"$cmd" "$@"
fi
}
__cmd_default () { __sel_output __cmd_commands __cmd_commands ; }
__cmd_kubectx () {
command -v kubectx >/dev/null || \
__kube_install_or_fail "kubectx" "ctx"
__sel_output kubectx kubectx "$(kubectx -c)"
}
__cmd_kubens () {
command -v kubectx >/dev/null || \
__kube_install_or_fail "kubens" "ns"
__sel_output kubens kubens "$(kubens -c)"
}
__cmd_gcloud_config_configurations () {
command -v gcloud >/dev/null || \
{ echo "Error: please install gcloud" ; exit 1 ; }
local current="$(gcloud config configurations list --format="csv(is_active,name)" | tail -n +2 | grep -E '^True,' | awk -F , '{print $2}')"
__sel_output \
"gcloud config configurations list --format=\"csv(name)\" | tail -n +2 | awk '{print \$1}'" \
"gcloud config configurations activate" \
"$current"
}
__kube_install_or_fail () {
cmd="$1" krewarg="$2"
kubectl krew version >/dev/null 2>&1 && \
kubectl krew install "$krewarg"
if [ $? -ne 0 ] ; then
__errexit "Please install command '$cmd'"
fi
}
__sel_output () {
local listcmd="$1"
local selcmd="$2"
shift 2
local current="${1:-}"
local output tmpoutput
# hack to stop special chars (pipe symbol) from being escaped by bash
eval "tmpoutput=\$($listcmd)"
declare -a sellist=( $tmpoutput )
declare -a execlist=()
if [ $HAS_DIALOG -eq 1 ] ; then
execlist=(dialog --menu "Select a context" 0 0 0 0 "(DEFAULT)")
else
execlist=("Select a context:")
fi
# Make the list of options, and identify the currently selected option
for i in `seq 1 ${#sellist[@]}` ; do
if [ -n "${current:-}" ] && [ "${sellist[$((i-1))]}" = "$current" ] ; then
execlist+=("$i" "${sellist[$((i-1))]} (SELECTED)")
else
execlist+=("$i" "${sellist[$((i-1))]}")
fi
done
if [ $HAS_DIALOG -eq 1 ] ; then
tmpfile="$(mktemp)"
"${execlist[@]}" 2>"$tmpfile"
output="$(cat "$tmpfile")"
else
printf "\n%s\n" "${execlist[0]}"
c=1
while [ $c -lt ${#execlist[@]} ] ; do
printf " %s %s\n" "${execlist[$c]}" "${execlist[$((c+1))]}"
c=$((c+2))
done
read -r -p "Choice: " output
fi
if [ "${output:-}" = "" ] ; then
output="0"
fi
if [ "$output" = "0" ] ; then
return 0
elif [ "$output" -lt 0 ] ; then
__errexit "Invalid output '$output'"
else
$selcmd "${sellist[$(($output-1))]}"
fi
}
__main () {
if command -v dialog >/dev/null ; then
HAS_DIALOG=1
fi
if [ "$_run_usage" = "1" ] ; then
__usage
elif [ $# -lt 1 ] ; then
__cmd_default
else
__cmd_commands "$@"
fi
}
__usage () {
cat <<EOUSAGE
Usage: $0 [OPTIONS] COMMAND
Displays a terminal dialog window to select from Kubernetes resources.
Commands:
gcloud_config_configurations
Display or select a GCloud configuration
kubectx Display or select a Kubernetes context
kubens Display or select a Kubernetes namespace
EOUSAGE
exit 1
}
_run_usage=0 _run_opt_version=0
while getopts "hHv" arg ; do
case "$arg" in
h) _run_usage=1 ;;
v) _run_opt_version=1 ;;
*) _die "Unknown argument '$arg'" ;;
esac
done
shift $((OPTIND-1))
__main "$@"