Skip to content

Instantly share code, notes, and snippets.

@cristiarg
Last active May 5, 2021 11:09
Show Gist options
  • Save cristiarg/31394f935f4ca401a7d315ef11b33280 to your computer and use it in GitHub Desktop.
Save cristiarg/31394f935f4ca401a7d315ef11b33280 to your computer and use it in GitHub Desktop.
Cubrid DBMS administration console bash autocompletion script
#
# Cubrid DBMS administration console bash autocompletion script
# Usage:
# - copy/rename as '/etc/bash_completion_d/cubrid' and it should be picked up by bash
#
# Capabilities/Limitations:
# - service and administrator utilities are hardcoded
# - service utilities 'server' and 'heartbeat' have limited support for listing databases from the
# $CUBRID_DATABASES/databases.txt file
# - administrator utilities' options are extracted by parsing that utility's usage/help output so,
# in this regard they should function maintenance-free when changes happen
#
# Caveats:
# - it seems that Cubrid console supports multiple languages; if that is the case, it should be
# trivial to adapt this script
_cubrid()
{
local curr prev opts_utilities_all
COMPREPLY=()
curr="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
#opts_utilities_service="--help service server broker manager heartbeat"
#opts_utilities_administrative="addvoldb alterdbhost backupdb checkdb compactdb copydb createdb deletedb diagdb installdb tranlist killtran loaddb lockdb optimizedb plandump renamedb restoredb restoreslave spacedb unloaddb paramdump statdump changemode applyinfo genlocale dumplocale synccolldb gen_tz dump_tz vacuumdb checksumdb"
opts_utilities_all="--help service server broker manager heartbeat hb addvoldb alterdbhost backupdb checkdb compactdb copydb createdb deletedb diagdb installdb tranlist killtran loaddb lockdb optimizedb plandump renamedb restoredb restoreslave spacedb unloaddb paramdump statdump changemode applyinfo genlocale dumplocale synccolldb gen_tz dump_tz vacuumdb checksumdb"
local opt_service="service"
local opt_server="server"
local opt_broker="broker"
local opt_manager="manager"
local opt_hb="hb"
local opt_heartbeat="heartbeat"
local opts_serv_service="start stop restart status"
local opts_serv_server="start stop restart status acl"
local opts_serv_broker="start stop restart info status on off reset acl getid test"
local opts_serv_manager="start stop status"
local opts_serv_heartbeat="start stop replication status reload"
# list existing databases from $CUBRID_DATABASES/databases.txt (if env variable existing)
#
if [[ ${COMP_CWORD} -ge 3 ]]; then
local anteprev="${COMP_WORDS[COMP_CWORD-2]}"
if [[ ( "_${anteprev}_" = "_${opt_server}_" && ${prev} =~ ^(start|stop|restart)$ ) || ( "_${anteprev}_" =~ ^_(hb|heartbeat)_$ && ${prev} =~ ^(start|stop|replication)$ ) ]]; then
# execute only if the environment is correct
if [[ ! -z ${CUBRID_DATABASES+y} && -f $CUBRID_DATABASES/databases.txt ]]; then
local opts_database_names=$( for opt_db_name in `cat $CUBRID_DATABASES/databases.txt | egrep "^[^#].+$" | awk 'BEGIN { FS = " " } { print $1; }'`; do echo ${opt_db_name}; done )
COMPREPLY=( $(compgen -W "${opts_database_names[*]}" -- ${curr}) )
fi
fi
fi
# regular options, some hard-coded, most extracted by executing cubrid command line utility as to
# obtain a usage/help output and parsing the output
#
if [[ ${COMP_CWORD} -ge 2 ]]; then
local opts_utility=${COMP_WORDS[1]}
# at index 1 either a service or an administrative command is expected
case "__${opts_utility}__" in
# service utilities (no more than one command is allowed)
#
__${opt_service}__)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $(compgen -W "${opts_serv_service}" -- ${curr}) )
fi
return 0;
;;
__${opt_server}__)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $(compgen -W "${opts_serv_server}" -- ${curr}) )
fi
return 0;
;;
__${opt_broker}__)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $(compgen -W "${opts_serv_broker}" -- ${curr}) )
fi
return 0;
;;
__${opt_manager}__)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $(compgen -W "${opts_serv_manager}" -- ${curr}) )
fi
return 0;
;;
__${opt_hb}__)
;&
__${opt_heartbeat}__)
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $(compgen -W "${opts_serv_heartbeat}" -- ${curr}) )
fi
return 0;
;;
# for all other - ie: administratoe utilities - execute "cubrid <utility>" and parse output for "^--.+$" to extract all the options
#
*)
# NOTE: the for loop is needed to properly construct an array out of the stringified output of the piped command
local opts_from_utility=$( for opt in `cubrid ${opts_utility} 2>&1 | awk 'BEGIN { FS = " " } { for(i=1; i<=NF; ++i) print $i; }'| egrep "^--.+$"`; do echo ${opt}; done )
declare -a opts_from_utility_ex
# strip value from "--<key>=<value>" options but leave the '=' as to suggest that the option expects a value
local re="(--[a-zA-Z0-9-]+=).+"
for opt in $opts_from_utility; do
if [[ $opt =~ $re ]]; then
opts_from_utility_ex+=(${BASH_REMATCH[1]})
else
opts_from_utility_ex+=($opt)
fi
done;
COMPREPLY=( $(compgen -W "${opts_from_utility_ex[*]}" -- ${curr}) )
return 0
;;
esac
return 0;
else
case "__${prev}__" in
# http://web.archive.org/web/20200507173259/https://debian-administration.org/article/317/An_introduction_to_bash_completion_part_2
__cubrid__)
COMPREPLY=( $(compgen -W "${opts_utilities_all}" -- ${curr}) )
return 0
;;
esac
fi
}
complete -F _cubrid cubrid
@cristiarg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment