From: Nicolas Boisselier Date: Mon, 29 Feb 2016 15:53:03 +0000 (+0100) Subject: move dbq to nb X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=46d375b33f8926bf82e3578308667199aab8b1eb;p=nb.git move dbq to nb --- diff --git a/bin/dbq b/bin/dbq new file mode 100755 index 00000000..e193af30 --- /dev/null +++ b/bin/dbq @@ -0,0 +1,342 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Encode; +################################################################################# +# +# VERSION +# +################################################################################# +my $VERSION = '0.0.1'; +# +# NB 28.02.16 +# - create script + +################################################################################# +# +# GLOBALS +# +################################################################################# +my ($NAME) = $0 =~ m,([^/]+)$,; +$_ = uc($NAME); +my %ACTION = ( + 'format' => $ENV{$_.'_FORMAT'} ? $ENV{$_.'_FORMAT'} : 'json', + 'action' => $ENV{$_.'_ACTION'} ? $ENV{$_.'_ACTION'} : 'rows', +); + +################################################################################# +# +# ARGS +# +################################################################################# +my $VERBOSE = $main::VERBOSE = 0; +my $DEBUG = $main::DEBUG = 0; + +csv2txt("\t",shift @ARGV) if @ARGV and $ARGV[0] eq '--csv2txt' and shift @ARGV; +curl_help() if @ARGV and $ARGV[0] eq '--curl_help' and shift @ARGV; + +my %Opt; get_options(\%Opt); +#use Data::Dumper; die Dumper(\%Opt); + +help() unless @ARGV; $main::_DATA_ = undef; + +################################################################################# +# +# BEGIN +# +################################################################################# +my @cmd = ( + #'echo', + 'curl', '-s', 'http://127.0.0.1', + '-H', 'Host: rent', + #'-D', '-', +); + +# +# Distinct arg from key=value +# +my %keys = (); +while ($_ = shift @ARGV) { + + if (/^([^=]+)=(.*)$/) { + $keys{$1} = $2; + } else { + push(@cmd,$_); + } + +} + +# +# Add args +# +push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-'; + +# +# Defaults Actions +# +while (my ($k,$v) = each %ACTION) { + next if $keys{$k}; + $keys{$k} = $v; +} + +$keys{header} = '0' if $keys{action} ne 'rows'; + +# +# Process specifics +# +#die if !$keys{table} or $keys{action} !~ /rows/; + +open(STDOUT,"|$0 --csv2txt ".( ( defined($keys{header}) ? $keys{header} : '') eq '0' ? '1' : '0')) + if $keys{format} eq 'nc' and $keys{format}='csv'; + +open(STDOUT,"|jq .") if $keys{format} eq 'jq' and $keys{format}='json'; + +# +# Push key=value +# +push(@cmd,map {('--data-raw',esc($_).'='.esc($keys{$_}))} sort keys %keys); + +#die Dumper(\@cmd,\%keys); +#push(@cmd,'--data-raw',esc($k).'='.esc($v)); +#warn 'Uri: '.join("&",@keys)."\n" if $VERBOSE>1; +warn 'Command:',map{(/^--data/?"\n ":" ").$_} @cmd,"\n" if $VERBOSE; +#die join(" ",@cmd); +################################################################################# +# +# END +# +################################################################################# +system @cmd; +close STDOUT; +exit 0; + +################################################################################# +# +# Functions +# +################################################################################# +sub csv2txt { + my $sep = shift; + my $noheader = shift; + my @len = (); + my @lines = (); + my $l; + + #open(STDOUT,">&2"); + # + # Store lines and lengths in memory + # + while (<>) { + #print $_; next; + chomp($_); + #warn $_; + my @F = split($sep,$_); + + for (my $i=0;$i<@F;$i++) { + #$F[$i] = '' unless defined $F[$i]; + $F[$i] = Encode::decode_utf8($F[$i]); + + #$len[$i] = $l if @len and ( $l = length($F[$i]) ) >= $len[$i]; + $len[$i] = length($F[$i]) unless defined $len[$i]; + $len[$i] = $l if ( $l = length($F[$i]) ) >= $len[$i]; + #$len[$i] = (@len>0 and ( $l = length($F[$i]) ) >= $len[$i]) ? $l : length($F[$i]); + } + + push @lines, [@F]; + } + + exit unless @lines; + + # + # Print pretty + # + binmode( STDOUT, "utf8:" ); + + my $i = 0; + my $t = -1; + my $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10); + my $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10); + + print $sep_line; + + no warnings; # sprintf missing argument warning message ???? + while ($_ = shift @lines) { + #warn $format,scalar(@$_); + #warn ">>$format: ".$_->[0]; + #warn "<<$format>>"; + #next unless @$_; + printf $format,@$_; + #printf("%s\n",join(' ',@$_)); + print $sep_line if !$noheader and !$i++; + } + + print $sep_line; + #close STDOUT; close STDERR; + #close STDIN; + exit; +} + +sub esc { + local $_=shift;s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + return $_; +}; + +sub help { +#------------------------------------------------------------------------------ +# Print help and exit +#------------------------------------------------------------------------------ + + require 'Pod/Usage.pm' unless $INC{'Pod/Usage.pm'}; + require 'Pod/Perldoc.pm' unless $INC{'Pod/Perldoc.pm'}; + + # Substitutions + sub pod_env { + my $v = ''; + eval '$v = ref(\\'.$_[0].') eq "ARRAY" ? join(" ",'.$_[0].') : '.$_[0].'; return defined $v ? $v : qq|UNDEF|;'; + return $v; + } + + $main::_DATA_ =~ s/([@\$][A-Z_]+)/pod_env($1)/eg; + + # Create tmp + my $in_file = (-e '/dev/shm' ? '/dev/shm' : '/tmp')."/$NAME.$$"; + my $in; + open($in,">$in_file") or die "$NAME: Can't write into $in_file: $!"; + print $in $main::_DATA_; + close $in; + + # Output + open(STDOUT,"|perl -pe 's/\.$$//g'".(($ENV{PAGER}||'') eq 'less' ? "|less -FRi" : "")); + my $opts = { + -input => $in_file, + -ouput => \*STDOUT, + -exitval => 'noexit', + -sections => [qw(SYNOPSIS DESCRIPTION OPTIONS)], + -verbose => ($Opt{'help'} ? 99 : 3), + }; + + Pod::Usage::pod2usage($opts); + close STDOUT; + unlink $in_file if $in_file and -e $in_file; + + exit 0; +} + +#------------------------------------------------------------------------------ +# Print version and exit +#------------------------------------------------------------------------------ +sub version { print "$NAME: version [$VERSION]\n"; exit 0; } + +#------------------------------------------------------------------------------ +# Get options from pod +#------------------------------------------------------------------------------ +sub get_options { + + use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); + + my @Opt = @_>1 ? $_[1] : (); + 0 and @Opt = ( + 'zaza', + 'dumper|D=s', + ); + + sub pod_opt { + local $_; + my $o = shift; + $o =~ s/(=.|[\+\-\!]$)//; + $o = join(", ",map{"-$_"} split(/[\|,:;]/,$o)); + return "$o"; + } + + while () { + s/option\[([^\]]+)\]/push(@Opt,$1) and pod_opt($1)/eg; + $main::_DATA_ .= $_; + } + + GetOptions($_[0],@Opt) || exit -1; + + help() if $_[0]{'help'} or $_[0]{'man'}; + version() if $_[0]{'version'}; + + $main::VERBOSE = $VERBOSE = $_[0]{'verbose'} if defined $_[0]{'verbose'}; + $main::DEBUG = $DEBUG = $_[0]{'debug'} if defined $_[0]{'debug'}; + +} + +sub curl_help { + open(CURL,'curl --help |') or die "Can't run curl"; + my $o = 'get|dump-header|cert-status'; + while () { + #print $_; + #/^\s*(?:-(\w+),\s*)?--([\w-]+)(.*?)\$/ or next; + @_ = /(?:-(\w+).*?)?--($o)\s+(.*?)$/ or next; + #warn $_; + push(@_,'') if @_<3; $_[0] = '' unless $_[0]; + $_[1] .= '|' if $_[0]; + + my $type = '!'; + $type = '=s' if $_[2] =~ /^[A-Z]{2,}/; + #print "=item B $_[2]\n\n"; + print " option[$_[1]$_[0]$type] $_[2]\n"; + } + close CURL; + exit; +} +__DATA__ + +=head1 NAME + +$NAME - Script to query http databases + +=head1 SYNOPSIS + +Quick usage: + +=over + +=item $NAME --verbose + +=item $NAME --help + +=back + +=head1 DESCRIPTION + +Curl wrapper + +=head1 OPTIONS + + option[verbose|v+] Verbose mode: increase the verbosity level. + option[debug+] Debug mode: increase the verbosity level. + option[version|V] Print version (default: $VERSION) + option[help|h|?] Print a brief help message and exits. + option[man] Print the manual page and exits. + + Curl OPTIONS BETA: + + ption[cert-status!] Verify the status of the server certificate (SSL) + ption[dump-header|D=s] FILE Write the headers to FILE + ption[get|G!] Send the -d data with a HTTP GET (H) + +=cut + +=head1 EXAMPLES + +... + +=head1 REQUIRES + +Getopt::Std, Pod::Usage + +=head1 COPYRIGHT AND LICENSE + + +=head1 SEE ALSO + +perl(1), http://perldoc.perl.org/perlpodstyle.html + +=head1 AUTHOR + + + +=cut