-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrf
135 lines (114 loc) · 2.05 KB
/
krf
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
#!/usr/bin/env bash
set -e
. functions
usage() {
cat <<END
Fuzzy find (fzf) the name of a Kubenetes resource(s) and execute a command with it.
All instances of '{}' are replaced by the resource name, or if absent, appended at the end.
Usage:
$0 [options] <resource type> <command> [<arg> ...]
Options:
--context, -c <STRING>
Name of the Kubernetes context
--namespace, -n <IDENTIFIER>
Namespace of target resource
--type, -t
Use resource type name
--table, -T
Use table view for selection
--query, -q
Initial fuzzy query
--exact, -e
Exact match query
--single, -s
Auto select on single match
--help, -h
Display this help
END
exit 2
}
context=
namespace=
type=
table=
query=
exact=
single=
while [[ $# -gt 0 ]]; do
case $1 in
--context | -c)
context=$2
shift 2 || usage
;;
--namespace | -n)
namespace=$2
shift 2 || usage
;;
--type | -t)
type=type
shift
;;
--table | -T)
table=table
shift
;;
--query | -q)
query=$2
shift 2 || usage
;;
--exact | -e)
exact=exact
shift
;;
--single | -s)
single=single
shift
;;
--help | -h)
usage
;;
--)
shift
break
;;
-*)
error "Unknown option: $1"
;;
*)
break
;;
esac
done
resource=$1
shift || usage
[[ $# -gt 0 ]] || usage
require_command fzf
kube_args=(get "$resource")
[[ $context ]] && kube_args+=(--context "$context")
[[ $namespace ]] && kube_args+=(--namespace "$namespace")
[[ $type ]] && kube_args+=(--show-kind)
[[ $table ]] || kube_args+=(-o name)
output=$(kubectl "${kube_args[@]}")
if [[ ! $table && ! $type ]]; then
output=$(<<< "$output" xargs basename -a)
fi
fz_args=(--tac -0)
[[ $query ]] && fz_args+=(-q "$query")
[[ $exact ]] && fz_args+=(-e)
[[ $single ]] && fz_args+=(-1)
names=($(<<< "$output" fzf "${fz_args[@]}" | awk '{print $1}'))
[[ ${#names} -eq 0 ]] && exit
[[ $type ]] || names=($(basename -a "${names[@]}"))
for name in "${names[@]}"; do
args=()
found=
for arg in "$@"; do
if [[ $arg == *{}* ]]; then
arg=${arg//\{\}/$name}
found=found
fi
args+=("$arg")
done
[[ $found ]] || args+=($name)
run "${args[@]}"
done