This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bash-completion-generator-ivpn-cli.sh
57 lines (47 loc) · 1.49 KB
/
bash-completion-generator-ivpn-cli.sh
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
#!/bin/bash
# Generates a bash completion file for the `ivpn` CLI.
#
# Usage:
# Install the ivpn CLI package first: https://www.ivpn.net/apps-linux/
#
# $ bash-completion-generator-ivpn-cli.sh > ivpn.bash
# $ source ivpn.bash
# $ sudo mv ivpn.bash /usr/share/bash-completion/completions/ivpn
# $ sudo chown root:root /usr/share/bash-completion/completions/ivpn
# print header
echo "# bash completion for ivpn"
echo ""
echo "_ivpn()"
echo "{"
echo ' local cur prev opts base'
echo ' COMPREPLY=()'
echo ' cur="${COMP_WORDS[COMP_CWORD]}"'
echo ' prev="${COMP_WORDS[COMP_CWORD-1]}"'
echo ""
# determine command list and print list
# first awk bit from: https://stackoverflow.com/a/22222219
COMMAND_LIST=$(ivpn -h | grep -Ev ^$ | awk '/Tips:/{f=0} f; /COMMANDS:/{f=1}' | awk '{ print $1 }')
echo " opts="'"'$COMMAND_LIST" -h"'"'
echo ""
echo ' case "${prev}" in'
# iterate over commands and print case entries for sub-commands
for i in $COMMAND_LIST; do
echo " "$i")"
SUB_LIST=$(ivpn $i -h | grep -E '^ -' | awk '{ print $1 }' | tr "|" " ")
echo " local "$i"_opts="'"'$SUB_LIST" -h"'"'
echo " COMPREPLY=( \$(compgen -W "'"'"\${"$i"_opts}"'"'" -- \${cur}) )"
echo " return 0"
echo " ;;"
done
# print footer
echo " -*)"
echo " return 0"
echo " ;;"
echo " esac"
echo ""
echo ' COMPREPLY=($(compgen -W "${opts}" -- ${cur}))'
echo " return 0"
echo "}"
echo "complete -F _ivpn ivpn"
echo ""
exit 0