forked from OkusiAssociates/oknav
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoknav.bash_completion
More file actions
187 lines (172 loc) · 6.47 KB
/
oknav.bash_completion
File metadata and controls
187 lines (172 loc) · 6.47 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
# Bash completion for OKnav System
# Source this file or copy to /etc/bash_completion.d/oknav
#
# Provides tab completion for:
# - oknav (cluster orchestrator)
# - ok_master and all symlinks (individual server access)
# Get the directory where OKnav is installed
_oknav_get_script_dir() {
local script_path
script_path=$(command -v oknav 2>/dev/null) || return 1
script_path=$(realpath "$script_path" 2>/dev/null) || return 1
dirname "$script_path"
}
# Get list of server aliases from hosts.conf
# Search order: $OKNAV_HOSTS_CONF > /etc/oknav/ > script directory
_oknav_get_aliases() {
local script_dir hosts_file
if [[ -n "${OKNAV_HOSTS_CONF:-}" && -f "$OKNAV_HOSTS_CONF" ]]; then
hosts_file="$OKNAV_HOSTS_CONF"
elif [[ -f /etc/oknav/hosts.conf ]]; then
hosts_file=/etc/oknav/hosts.conf
else
script_dir=$(_oknav_get_script_dir) || return 1
hosts_file="$script_dir/hosts.conf"
fi
[[ -f "$hosts_file" ]] || return 1
# Parse hosts.conf: extract all aliases (skip FQDN, skip options)
while IFS= read -r line; do
# Skip comments and empty lines
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
# Remove options (stuff in parentheses)
line="${line%\(*}"
# Get all fields after the first (FQDN)
read -r _fqdn aliases <<< "$line"
echo "$aliases"
done < "$hosts_file"
}
# Completion for ok_master derivatives (ok0, ok1, srv1, etc.)
_ok_master_completion() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-r --root -u --user -c --connect-timeout -d --dir -R --relay --no-relay -D --debug -V --version -h --help"
case "$prev" in
-u|--user)
# Complete with system users
COMPREPLY=( $(compgen -u -- "$cur") )
return 0
;;
-c|--connect-timeout)
COMPREPLY=( $(compgen -W "3 5 10 15 30" -- "$cur") )
return 0
;;
-R|--relay)
# Complete with known hostnames
COMPREPLY=( $(compgen -A hostname -- "$cur") )
return 0
;;
*)
if [[ "$cur" == -* ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
else
# Complete with commands for remote execution
COMPREPLY=( $(compgen -c -- "$cur") )
fi
return 0
;;
esac
}
# Completion for oknav script (cluster orchestrator)
_oknav_completion() {
local cur prev opts subcommands aliases
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-p --parallel -d --dir -c --connect-timeout -t --timeout -x --exclude-host -D --debug -h --help -V --version"
subcommands="install add remove list"
case "$prev" in
-c|--connect-timeout)
COMPREPLY=( $(compgen -W "3 5 10 15 30" -- "$cur") )
return 0
;;
-t|--timeout)
COMPREPLY=( $(compgen -W "5 10 15 30 60 120 300" -- "$cur") )
return 0
;;
-x|--exclude-host)
# Complete with server aliases from hosts.conf
aliases=$(_oknav_get_aliases 2>/dev/null)
COMPREPLY=( $(compgen -W "$aliases" -- "$cur") )
return 0
;;
install)
# Install subcommand options
COMPREPLY=( $(compgen -W "-n --dry-run --remove-stale --clean-local -h --help" -- "$cur") )
return 0
;;
add)
# Add subcommand: complete with hostnames or options
if [[ "$cur" == -* ]]; then
COMPREPLY=( $(compgen -W "-n --dry-run -h --help" -- "$cur") )
else
# Complete with known hosts
COMPREPLY=( $(compgen -A hostname -- "$cur") )
fi
return 0
;;
remove)
# Remove subcommand: complete with server aliases
if [[ "$cur" == -* ]]; then
COMPREPLY=( $(compgen -W "-n --dry-run -h --help" -- "$cur") )
else
aliases=$(_oknav_get_aliases 2>/dev/null)
COMPREPLY=( $(compgen -W "$aliases" -- "$cur") )
fi
return 0
;;
list)
# List subcommand options
COMPREPLY=( $(compgen -W "-R --reachable -p --parallel -h --help" -- "$cur") )
return 0
;;
*)
if [[ "$cur" == -* ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
elif [[ "${COMP_WORDS[1]}" == "install" ]]; then
COMPREPLY=( $(compgen -W "-n --dry-run --remove-stale --clean-local -h --help" -- "$cur") )
elif [[ "${COMP_WORDS[1]}" == "add" ]]; then
COMPREPLY=( $(compgen -A hostname -- "$cur") )
elif [[ "${COMP_WORDS[1]}" == "remove" ]]; then
aliases=$(_oknav_get_aliases 2>/dev/null)
COMPREPLY=( $(compgen -W "$aliases" -- "$cur") )
elif [[ "${COMP_WORDS[1]}" == "list" ]]; then
COMPREPLY=( $(compgen -W "-R --reachable -p --parallel -h --help" -- "$cur") )
else
# Complete with subcommands and remote commands
COMPREPLY=( $(compgen -W "$subcommands" -- "$cur") $(compgen -c -- "$cur") )
fi
return 0
;;
esac
}
# Register completions
# Core scripts
complete -F _oknav_completion oknav
complete -F _ok_master_completion ok_master
# Dynamic registration: find all symlinks pointing to ok_master
_oknav_register_completions() {
local script_dir ok_master_path symlink name
script_dir=$(_oknav_get_script_dir) || return 1
ok_master_path="$script_dir/ok_master"
# Check /usr/local/bin for symlinks to ok_master
for symlink in /usr/local/bin/*; do
[[ -L "$symlink" ]] || continue
[[ "$(readlink "$symlink")" == "$ok_master_path" ]] || continue
name="${symlink##*/}"
complete -F _ok_master_completion "$name"
done
# Also check script directory for local symlinks
for symlink in "$script_dir"/*; do
[[ -L "$symlink" ]] || continue
name="${symlink##*/}"
[[ "$name" == "oknav" ]] && continue # Skip oknav script
[[ "$(readlink "$symlink")" == "ok_master" ]] || continue
complete -F _ok_master_completion "$name"
done
}
# Register completions for existing symlinks
_oknav_register_completions 2>/dev/null
#fin