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