Skip to content

Commit 3748088

Browse files
kit494wayxen0l
authored andcommitted
Add bash completion (#192)
1 parent ab800d8 commit 3748088

File tree

1 file changed

+227
-0
lines changed

1 file changed

+227
-0
lines changed

completions/bash/aws-gate

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#!/usr/bin/env bash
2+
3+
__aws_gate_complete_commands() {
4+
local commands="
5+
bootstrap
6+
list ls
7+
session
8+
ssh
9+
ssh-config
10+
ssh-proxy
11+
-h --help
12+
--verbose
13+
-v --version
14+
"
15+
COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
16+
}
17+
18+
__aws_gate_complete_bootstrap() {
19+
local args="
20+
-h --help
21+
-f --force
22+
"
23+
COMPREPLY=( $(compgen -W "${args}" -- "${cur}") )
24+
}
25+
26+
__aws_gate_complete_session() {
27+
case "${prev}" in
28+
-p|--profile)
29+
__aws_gate_complete_opt_profile
30+
;;
31+
-r|--region)
32+
__aws_gate_complete_opt_region
33+
;;
34+
*)
35+
local args="
36+
-h --help
37+
-p --profile
38+
-r --region
39+
"
40+
41+
__aws_gate_merge_hosts_to_args
42+
43+
COMPREPLY=( $(compgen -W "${args}" -- "${cur}") )
44+
;;
45+
esac
46+
}
47+
48+
__aws_gate_complete_ssh() {
49+
case "${prev}" in
50+
-p|--profile)
51+
__aws_gate_complete_opt_profile
52+
;;
53+
-r|--region)
54+
__aws_gate_complete_opt_region
55+
;;
56+
-l|--os-user)
57+
__aws_gate_complete_ssh_user
58+
;;
59+
*)
60+
local args="
61+
-h --help
62+
-p --profile
63+
-r --region
64+
-l --os-user
65+
-P --port
66+
"
67+
68+
__aws_gate_merge_hosts_to_args
69+
70+
COMPREPLY=( $(compgen -W "${args}" -- "${cur}") )
71+
;;
72+
esac
73+
}
74+
75+
__aws_gate_complete_list() {
76+
local args="
77+
-h --help
78+
-p --profile
79+
-r --region
80+
-f --format
81+
"
82+
83+
case "${prev}" in
84+
-p|--profile)
85+
__aws_gate_complete_opt_profile
86+
;;
87+
-r|--region)
88+
__aws_gate_complete_opt_region
89+
;;
90+
-f|--format)
91+
__aws_gate_complete_opt_format
92+
;;
93+
*)
94+
COMPREPLY=( $(compgen -W "${args}" -- "${cur}") )
95+
;;
96+
esac
97+
}
98+
99+
__aws_gate_complete_opt_profile() {
100+
local aws_profiles="
101+
$(grep '\[' ~/.aws/credentials | grep -v '#' | tr -d '[' | tr -d ']')
102+
$(grep '\[' ~/.aws/config | grep -v '#' | tr -d '[' | tr -d ']' | cut -d' ' -f 2)
103+
"
104+
COMPREPLY=( $(compgen -W "${aws_profiles}" -- "${cur}") )
105+
}
106+
107+
__aws_gate_complete_opt_region() {
108+
local aws_regions="
109+
ap-northeast-1 ap-northeast-2 ap-south-1 ap-southeast-1 ap-southeast-2 ca-central-1 eu-central-1
110+
eu-north-1 eu-west-1 eu-west-2 eu-west-3 sa-east-1 us-east-1 us-east-2 us-west-1 us-west-2
111+
"
112+
COMPREPLY=( $(compgen -W "${aws_regions}" -- "${cur}") )
113+
}
114+
115+
__aws_gate_complete_opt_format() {
116+
local formats="tsv csv human json"
117+
COMPREPLY=( $(compgen -W "${formats}" -- "${cur}") )
118+
}
119+
120+
__aws_gate_complete_ssh_user() {
121+
local ssh_users="ec2-user ubuntu"
122+
COMPREPLY=( $(compgen -W "${ssh_users}" -- "${cur}") )
123+
}
124+
125+
__aws_gate_hosts() {
126+
local aws_gate_config_files=(
127+
$(test -f ~/.aws-gate/config && echo "$HOME/.aws-gate/config")
128+
$(test -d ~/.aws-gate/config.d && find ~/.aws-gate/config.d -type f)
129+
)
130+
131+
local alias_filter_awk=$(cat<<'EOD'
132+
BEGIN {
133+
process_hosts = 0
134+
}
135+
136+
/^hosts:/ { process_hosts=1; next }
137+
138+
process_hosts && /^\S+:/ { exit }
139+
140+
process_hosts && /\s+alias:\s*/ {
141+
for (i=1; i<NF; i++) {
142+
if ($i == "alias:") {
143+
print $(i+1)
144+
}
145+
}
146+
}
147+
EOD
148+
)
149+
150+
for file in "${aws_gate_config_files[@]}"; do
151+
awk "${alias_filter_awk}" "${file}"
152+
done
153+
}
154+
155+
__aws_gate_merge_hosts_to_args() {
156+
local host
157+
while [ "${counter}" -lt "$cword" ]; do
158+
case "${words[$counter]}" in
159+
-p|--profile|-r|--region|-l|--os-user|-P|--port)
160+
(( counter++ ))
161+
;;
162+
-*)
163+
;;
164+
*)
165+
if [[ -z "$host" ]]; then
166+
host=${words[$counter]}
167+
break
168+
fi
169+
;;
170+
esac
171+
(( counter++ ))
172+
done
173+
174+
if [[ -z "${host}" ]]; then
175+
args="
176+
${args}
177+
$(__aws_gate_hosts)
178+
"
179+
fi
180+
}
181+
182+
__aws_gate_complete() {
183+
local cur prev words cword subcommand subcommand_pos
184+
_get_comp_words_by_ref -n : cur prev words cword
185+
186+
local counter=1
187+
while [ "$counter" -lt "$cword" ]; do
188+
case "${words[$counter]}" in
189+
-*)
190+
;;
191+
*)
192+
if [ -z "$subcommand" ]; then
193+
subcommand=${words[$counter]}
194+
subcommand_pos=$counter
195+
(( counter++ ))
196+
break
197+
fi
198+
;;
199+
esac
200+
(( counter++ ))
201+
done
202+
203+
COMPREPLY=()
204+
case "${cword}" in
205+
1)
206+
__aws_gate_complete_commands
207+
;;
208+
*)
209+
case "${subcommand}" in
210+
bootstrap)
211+
__aws_gate_complete_bootstrap
212+
;;
213+
session)
214+
__aws_gate_complete_session
215+
;;
216+
ssh|ssh-config|ssh-proxy)
217+
__aws_gate_complete_ssh
218+
;;
219+
ls|list)
220+
__aws_gate_complete_list
221+
;;
222+
esac
223+
;;
224+
esac
225+
}
226+
227+
complete -F __aws_gate_complete aws-gate

0 commit comments

Comments
 (0)