scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Tue, 26 Feb 2019 18:19:49 +0100
branchv_0
changeset 236 a3ec71fa8e17
parent 222 5ffeb18b6f85
child 250 aae5009bd0af
permissions -rwxr-xr-x
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!“
     1 #!/usr/bin/perl
     2 
     3 # SQL-DK
     4 # Copyright © 2013 František Kučera (frantovo.cz)
     5 # 
     6 # This program is free software: you can redistribute it and/or modify
     7 # it under the terms of the GNU General Public License as published by
     8 # the Free Software Foundation, either version 3 of the License, or
     9 # (at your option) any later version.
    10 # 
    11 # This program is distributed in the hope that it will be useful,
    12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14 # GNU General Public License for more details.
    15 # 
    16 # You should have received a copy of the GNU General Public License
    17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
    18 
    19 
    20 # Parses Java source code from STDIN and generates script for BASH completion
    21 # Input (in this order):
    22 #	info/globalcode/sql/dk/Constants.java
    23 #	info/globalcode/sql/dk/formatting/*
    24 #	info/globalcode/sql/dk/CLIParser.java
    25 
    26 # TODO: support database/formatter names with spaces
    27 
    28 use strict;
    29 use warnings;
    30 
    31 my $configDir = "~";
    32 
    33 while (<>) {
    34 	if (/"(.*?)".*? \/\/\s*bash-completion:dir/) {
    35 		$configDir .= "/$1";
    36 		last;
    37 	}
    38 }
    39 
    40 my $databasesFile  = "$configDir/bash-completion/databases";
    41 my $formattersFile = "$configDir/bash-completion/formatters";
    42 my $defaultFormatterFile = "$configDir/bash-completion/default-formatter";
    43 my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties";
    44 
    45 print '#have sql-dk &&
    46 _sql_dk_bash_completion_find_formatter() {
    47 	local previous
    48 	for token in "$@"; do
    49 		if [ "x$previous" == "x--formatter" ]; then
    50 			echo -n "$token";
    51 			return 0;
    52 		fi
    53 		previous="$token";
    54 	done
    55 	
    56 	if [ -f '.$defaultFormatterFile.' ]; then
    57 		cat '.$defaultFormatterFile.'
    58 	fi
    59 }
    60 
    61 _sql_dk_bash_completion_formatter_property_name() {
    62 	if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
    63 		local formatter_dir='.$formatterPropertiesDir.'/$formatter
    64 		if [ -d  $formatter_dir ]; then
    65 			ls -1 $formatter_dir;
    66 		fi
    67 	fi
    68 }
    69 
    70 _sql_dk_bash_completion_formatter_property_choice() {
    71 	local property="${COMP_WORDS[COMP_CWORD-1]}";
    72 	local formatter_dir='.$formatterPropertiesDir.'/$formatter
    73 	local property_choices_file=$formatter_dir/$property/choices;
    74 	if [ -f $property_choices_file ]; then
    75 		cat $property_choices_file;
    76 	fi
    77 }
    78 
    79 _sql_dk_bash_completion() {
    80 	local cur prev formatter
    81 
    82 	COMPREPLY=()
    83 	cur=${COMP_WORDS[COMP_CWORD]}
    84 	prev=${COMP_WORDS[COMP_CWORD-1]}
    85 
    86 	case "$prev" in
    87 	--db | --test-connection | --list-jdbc-properties)
    88 		if [ -f '.$databasesFile.' ]; then
    89 			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
    90 			return 0
    91 		fi
    92 		;;
    93 	--formatter | --list-formatter-properties)
    94 		if [ -f '.$formattersFile.' ]; then
    95 			COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
    96 		else
    97 			COMPREPLY=( $( compgen -W "
    98 ';
    99 
   100 while (<>) {
   101 	if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
   102 		print "				$1\n";
   103 	}
   104 	last if (/\/\/\s*bash-completion:options/);
   105 }
   106 
   107 
   108 print '				" -- $cur ) );
   109 		fi
   110 		return 0
   111 		;;
   112 	--formatter-property)
   113 		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
   114 		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_name )" -- $cur ) ); 
   115 		return 0;
   116 		;;
   117 	esac;
   118 	
   119 	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
   120 		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
   121 		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
   122 		return 0;
   123 	fi
   124 
   125 	COMPREPLY=( $( compgen -W "
   126 ';
   127 
   128 while (<>) {
   129 	if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
   130 		print "			$1\n";
   131 	}
   132 }
   133 
   134 print '		" -- $cur ) )
   135 	return 0
   136 
   137 }
   138 
   139 complete -F _sql_dk_bash_completion sql-dk
   140 ';