-
Notifications
You must be signed in to change notification settings - Fork 0
/
.fzf.bash
122 lines (102 loc) · 3.3 KB
/
.fzf.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
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
# Setup fzf
# ---------
if [[ ! "$PATH" == */home/gabriel/.fzf/bin* ]]; then
export PATH="${PATH:+${PATH}:}/home/gabriel/.fzf/bin"
fi
# Auto-completion
# ---------------
[[ $- == *i* ]] && source "/home/gabriel/.fzf/shell/completion.bash" 2> /dev/null
# Key bindings
# ------------
source "/home/gabriel/.fzf/shell/key-bindings.bash"
# Kubectl
# ------------
source <(kubectl completion bash )
alias k=kubectl
_FZF_KUBE_HEIGHT=20
_fzf_complete_k_get_namespaced() {
export K_NAMESPACE_COLUMN=y
FZF_COMPLETION_TRIGGER='' _fzf_complete --height $_FZF_KUBE_HEIGHT -- "$@" < <(
kubectl get $1 --all-namespaces
)
}
_fzf_complete_k_get_namespaced_labels() {
export K_NAMESPACE_COLUMN=y
FZF_COMPLETION_TRIGGER='' _fzf_complete --no-sort --height $_FZF_KUBE_HEIGHT -- "$@" < <(
kubectl get $1 --all-namespaces -o json | \
jq -r '.items[] |
.metadata.namespace as $n |
.metadata.labels |
keys[] as $k |
"\($n) \($k)=\(.[$k])"' | \
sort | uniq
)
export K_NAMESPACE_COLUMN=
}
_fzf_complete_k_get_pod_containers() {
FZF_COMPLETION_TRIGGER='' _fzf_complete --no-sort -- "$@" < <(
kubectl get pods "$@" -o json | \
jq -r 'if .items == null then . else .items[] end | .spec.containers[].name' | \
sort | uniq
)
}
_fzf_complete_k() {
local cur prev words
_get_comp_words_by_ref -n = cur prev words
local subcommand=${words[1]}
case $subcommand in
get|describe)
case $prev in
get|describe)
FZF_COMPLETION_TRIGGER='' _fzf_complete --height $_FZF_KUBE_HEIGHT -- "$@" < <(
kubectl api-resources | awk '{print $1}'
)
;;
-l)
_fzf_complete_k_get_namespaced_labels ${words[2]}
;;
*)
_fzf_complete_k_get_namespaced ${words[2]}
;;
esac
;;
logs)
case $prev in
-l)
_fzf_complete_k_get_namespaced_labels pods
;;
-c)
local args=( "${words[@]:2:${#words[@]}-4}" )
_fzf_complete_k_get_pod_containers "${args[@]}"
;;
*)
_fzf_complete_k_get_namespaced pods
;;
esac
;;
exec)
case $prev in
-c)
local args=( "${words[@]:2:${#words[@]}-4}" )
local clean_args=()
for elem in "${args[@]}"; do
if [[ $elem != -* ]] || [[ $elem == -n ]]; then
clean_args+=($elem);
fi
done
_fzf_complete_k_get_pod_containers "${clean_args[@]}"
;;
*)
_fzf_complete_k_get_namespaced pods
;;
esac
;;
esac
}
_fzf_complete_k_get_namespaced_post() {
awk '{printf "%s -n %s", $2, $1}'
}
_fzf_complete_k_get_namespaced_labels_post() {
_fzf_complete_k_get_namespaced_post
}
complete -F _fzf_complete_k -o default -o bashdefault kubectl k