-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkcmgr.zsh
162 lines (145 loc) · 3.61 KB
/
kcmgr.zsh
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
157
158
159
160
161
162
# init configs dir
_kubeconfigs_dir="${HOME}/.kube/configs"
mkdir -p "$_kubeconfigs_dir"
touch "${_kubeconfigs_dir}/.kcmgr"
# print kcmgr help message
function _kcmgr_help() {
echo "kcmgr: kubeconfig manager"
echo
echo "Usage:"
echo " kcmgr [command]"
echo
echo "Available Commands:"
echo " ls, list list kubeconfig"
echo " show show content of current or specified kubeconfig"
echo " set set KUBECONFIG environment variable"
echo " unset unset KUBECONFIG environment variable"
echo " edit edit current or specified kubeconfig"
echo " del, delete delete current or specified kubeconfig"
echo " help print this message"
}
# kubeconfig manager
function kcmgr() {
if [[ $# -lt 1 ]]; then
_kcmgr_help
return 1
fi
case $1 in
(ls|list) _kcmgr_list
;;
(show) _kcmgr_show ${@:2}
;;
(set) _kcmgr_set ${@:2}
;;
(unset) _kcmgr_unset
;;
(edit) _kcmgr_edit ${@:2}
;;
(del|delete) _kcmgr_delete ${@:2}
;;
(help) _kcmgr_help
;;
(*)
_kcmgr_help
return 1
;;
esac
}
# completion
compdef _kcmgr_comp kcmgr
function _kcmgr_comp() {
case ${words[2]} in
(show|set|edit|del|delete)
local -a kubeconfigs
kubeconfigs=("${(@f)$(_kcmgr_list --raw)}")
_describe 'kubeconfig' kubeconfigs
;;
esac
}
# kcmgr list
# list kubeconfig
function _kcmgr_list() {
for f in "${_kubeconfigs_dir}"/(*|.kcmgr); do
# ignore .kcmgr
if [[ "$f" == "${_kubeconfigs_dir}/.kcmgr" ]]; then
continue
fi
# trim prefix
f=${f#"${_kubeconfigs_dir}/"}
# add prefix, indent
# TODO: and color
if [[ ! "$@" =~ "--raw" ]]; then
if [[ "${_kubeconfigs_dir}/${f}" == "$KUBECONFIG" ]]; then # current kubeconfig
f="* ${f}"
else
f=" ${f}"
fi
fi
# print it
echo "$f"
done
}
# kcmgr show
# show content of current or specified kubeconfig
function _kcmgr_show() {
kubeconfig="$(_kubeconfig_path "$1")"
echo "# ${kubeconfig}"
if [[ -f "$kubeconfig" ]]; then
cat "$kubeconfig"
fi
}
# kcmgr set
# set KUBECONFIG environment variable
function _kcmgr_set() {
# no kubeconfig to set
if [[ $# -lt 1 ]]; then
_kcmgr_unset
return
fi
export KUBECONFIG="$(_kubeconfig_path "$1")"
}
# kcmgr unset
# unset KUBECONFIG environment variable
function _kcmgr_unset() {
unset KUBECONFIG
}
# kcmgr edit
# edit current or specified kubeconfig
function _kcmgr_edit() {
kubeconfig="$(_kubeconfig_path "$1")"
editor="$(_get_editor)" || return 1
"$editor" "$kubeconfig"
}
# kcmgr delete
# delete current or specified kubeconfig
function _kcmgr_delete() {
kubeconfig="$(_kubeconfig_path "$1")"
# double chekc
read ret"?Delete '${kubeconfig}'? (Y/n): "
if [[ "$ret" == "Y" ]]; then
rm "$kubeconfig"
echo "deleted"
else
echo "abort"
fi
}
# get editor
function _get_editor() {
if [[ -f =vim ]]; then
echo "vim"
elif [[ -f =vi ]]; then
echo "vi"
else
return 1
fi
}
# get kubeconfig path from name
function _kubeconfig_path() {
if [[ "$1" == "" ]]; then
echo "${KUBECONFIG:-${HOME}/.kube/config}"
elif [[ "${1:0:2}" == "./" || "${1:0:3}" == "../" || "${1:0:1}" == "/" || "${1:0:1}" == "~" ]]; then
echo "$1"
else
echo "${_kubeconfigs_dir}/${1}"
fi
}