Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions bash-completion-generator-ivpn-cli.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,40 +1,62 @@
#!/bin/bash

# Generates a bash completion file for the `ivpn` CLI.
#
# Bash completion generator for ivpn CLI
# (generates a bash completion script for the `ivpn` CLI)
# https://github.com/jordan-ivpn/bash-completion-generator-ivpn-cli/
#
# Usage:
# Install the ivpn CLI package first: https://www.ivpn.net/apps-linux/
# 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
#
# $ 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
# Info:
# 1) The recommended directory is `completionsdir`, which you can get with `pkg-config --variable=completionsdir bash-completion`
# https://github.com/scop/bash-completion
#

# exit on error
set -e

# By default, the source command is `ivpn`.
# Also you can specify custom path to IVPN CLI binary in the first argument to this script (e.g. `bash-completion-generator-ivpn-cli.sh /usr/local/bin/ivpn`)
cli="ivpn"
if [ ! -z "$1" ]
then
cli="$1"
fi

# Check if the command/binary exists
if ! command -v "$cli" > /dev/null 2>&1; then
echo "Error: $cli not found or not executable"
exit 1
fi

# print header
echo "# bash completion for ivpn"
echo ""
echo "_ivpn()"
echo "{"
echo ' local cur prev opts base'
echo ' local cur opts'
echo ' COMPREPLY=()'
echo ' cur="${COMP_WORDS[COMP_CWORD]}"'
echo ' prev="${COMP_WORDS[COMP_CWORD-1]}"'
echo ' cur="${COMP_WORDS[COMP_CWORD]}" # current word'
echo ' cmd="${COMP_WORDS[1]}" # next word after "ivpn", e.g.: $ ivpn <cmd> ...'
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 }')
COMMAND_LIST=$( $cli -h | grep -Ev ^$ | awk '/Tips:/{f=0} f; /COMMANDS:/{f=1}' | awk '{ print $1 }')

echo " opts="'"'$COMMAND_LIST" -h"'"'
echo ""
echo ' case "${prev}" in'

echo ' case "${cmd}" 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 "|" " ")
SUB_LIST=$($cli $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"
Expand Down