-
-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathkubetail.bash
60 lines (57 loc) · 1.63 KB
/
kubetail.bash
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
_findnamespace(){
local next="0"
local namespace="$KUBETAIL_NAMESPACE";
for wo in "${COMP_WORDS[@]}"
do
if [ "$next" = "0" ]; then
if [ "$wo" = "-n" ] || [ "$wo" = "--namespace" ]; then
next="1"
fi
else
namespace="$wo"
break
fi
done
if [ "$namespace" != "" ]; then
printf "%s" " --namespace $namespace"
else
printf "%s" " --all-namespaces"
fi
}
_findcontext(){
local next="0"
local context="";
for wo in "${COMP_WORDS[@]}"
do
if [ "$next" = "0" ]; then
if [ "$wo" = "-t" ] || [ "$wo" = "--context" ]; then
next="1"
fi
else
context="$wo"
break
fi
done
if [ "$context" != "" ]; then
printf "%s" " --context=$context"
fi
}
_kubetail()
{
local curr_arg;
curr_arg=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
command=${COMP_WORDS[1]}
case $prev in
-t|--context)
COMPREPLY=( $(compgen -W "$(kubectl config get-contexts -o=name | awk '{print $1}')" -- $curr_arg ) );
;;
-n|--namespace)
COMPREPLY=( $(compgen -W "$(kubectl $(_findcontext) get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}' | awk '{print $1}')" -- $curr_arg ) );
;;
*)
COMPREPLY=( $(compgen -W "$(kubectl $(_findcontext) get pods $(_findnamespace) -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}' --no-headers | awk '{print $1}')" -- $curr_arg ) );
;;
esac
}
complete -F _kubetail kubetail kt