-
Notifications
You must be signed in to change notification settings - Fork 32
/
dkr
executable file
·77 lines (68 loc) · 1.6 KB
/
dkr
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
#!/usr/bin/env bash
declare -A aliases
declare -A helptext
has() {
local v c
if [[ $1 = '-v' ]]; then
v=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( v > 0 )) && err "$c not found"
return 1
fi
done
}
aliases[h]=help
aliases[-h]=help
aliases[--help]=help
helptext[help]='show this help'
subcmd_help() {
local formattedhelptext
formattedhelptext=$(for c in "${subcmds_avail[@]}"; do
printf " %s\n %s\n" "$c" "${helptext[$c]}"
done)
LESS=-FEXR less <<-HELP
$0 <COMMAND>
${formattedhelptext}
HELP
}
helptext[ps]='show a list of running processes'
subcmd_ps() {
fzf \
--bind='start:reload:docker ps' \
--bind='enter:execute:docker exec -it {1} sh' \
--bind='ctrl-d:execute-silent(docker stop {1})+reload:docker ps' \
--bind='?:toggle-preview' \
--bind='ctrl-l:clear-screen+reload:docker ps' \
--preview='docker logs -f {1}' \
--header='<Enter> opens sh inside container, <F9> to kill' \
--reverse \
--height=100% \
--header-lines=1 \
--preview-window=cycle,follow,80%:down
}
mapfile -t subcmds_avail < <(compgen -A function | awk '/^subcmd_/ { sub(/^subcmd_/, "", $0); print }')
nocmd() {
cmd=$(for c in "${subcmds_avail[@]}"; do
printf "$c\t${help}\t${helptext[$c]}\n"
done)
subcmd_$(column -t -s $'\t' <<< "$cmd" | fzf | awk '{print $1}')
}
if (( $# < 1 )); then
nocmd
exit 1
elif has "subcmd_$1"; then
subcmd="subcmd_$1"
shift
"$subcmd" "$@"
elif [[ -v aliases[$1] ]]; then
subcmd=subcmd_${aliases[$1]}
shift
"$subcmd" "$@"
else
echo 'unknown command'
subcmd_help
exit 1
fi