Avoid reusing/rewriting the DB connection properties.
There was weird random errors while testing connection to multiple DB in parallel when one of them was meta connection to same DB connection.
Two kinds of exception: 1) missing password 2) „Passing DB password as CLI parameter is insecure!“
#!/usr/bin/perl
# SQL-DK
# Copyright © 2013 František Kučera (frantovo.cz)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Parses Java source code from STDIN and generates script for BASH completion
# Input (in this order):
# info/globalcode/sql/dk/Constants.java
# info/globalcode/sql/dk/formatting/*
# info/globalcode/sql/dk/CLIParser.java
# TODO: support database/formatter names with spaces
use strict;
use warnings;
my $configDir = "~";
while (<>) {
if (/"(.*?)".*? \/\/\s*bash-completion:dir/) {
$configDir .= "/$1";
last;
}
}
my $databasesFile = "$configDir/bash-completion/databases";
my $formattersFile = "$configDir/bash-completion/formatters";
my $defaultFormatterFile = "$configDir/bash-completion/default-formatter";
my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties";
print '#have sql-dk &&
_sql_dk_bash_completion_find_formatter() {
local previous
for token in "$@"; do
if [ "x$previous" == "x--formatter" ]; then
echo -n "$token";
return 0;
fi
previous="$token";
done
if [ -f '.$defaultFormatterFile.' ]; then
cat '.$defaultFormatterFile.'
fi
}
_sql_dk_bash_completion_formatter_property_name() {
if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
local formatter_dir='.$formatterPropertiesDir.'/$formatter
if [ -d $formatter_dir ]; then
ls -1 $formatter_dir;
fi
fi
}
_sql_dk_bash_completion_formatter_property_choice() {
local property="${COMP_WORDS[COMP_CWORD-1]}";
local formatter_dir='.$formatterPropertiesDir.'/$formatter
local property_choices_file=$formatter_dir/$property/choices;
if [ -f $property_choices_file ]; then
cat $property_choices_file;
fi
}
_sql_dk_bash_completion() {
local cur prev formatter
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
case "$prev" in
--db | --test-connection | --list-jdbc-properties)
if [ -f '.$databasesFile.' ]; then
COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
return 0
fi
;;
--formatter | --list-formatter-properties)
if [ -f '.$formattersFile.' ]; then
COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
else
COMPREPLY=( $( compgen -W "
';
while (<>) {
if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
print " $1\n";
}
last if (/\/\/\s*bash-completion:options/);
}
print ' " -- $cur ) );
fi
return 0
;;
--formatter-property)
formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_name )" -- $cur ) );
return 0;
;;
esac;
if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) );
return 0;
fi
COMPREPLY=( $( compgen -W "
';
while (<>) {
if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
print " $1\n";
}
}
print ' " -- $cur ) )
return 0
}
complete -F _sql_dk_bash_completion sql-dk
';