From 7bc0b6b3d5392757cc197005f26efc35f5bc7525 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 29 Feb 2016 16:38:50 +0000 Subject: [PATCH] curl options --- bin/dbq | 56 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/bin/dbq b/bin/dbq index e193af30..1b630a34 100755 --- a/bin/dbq +++ b/bin/dbq @@ -18,10 +18,10 @@ my $VERSION = '0.0.1'; # ################################################################################# my ($NAME) = $0 =~ m,([^/]+)$,; -$_ = uc($NAME); +my $UC_NAME = uc($NAME); my %ACTION = ( - 'format' => $ENV{$_.'_FORMAT'} ? $ENV{$_.'_FORMAT'} : 'json', - 'action' => $ENV{$_.'_ACTION'} ? $ENV{$_.'_ACTION'} : 'rows', + 'format' => $ENV{$UC_NAME.'_FORMAT'} ? $ENV{$UC_NAME.'_FORMAT'} : 'json', + 'action' => $ENV{$UC_NAME.'_ACTION'} ? $ENV{$UC_NAME.'_ACTION'} : 'rows', ); ################################################################################# @@ -35,10 +35,23 @@ 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); +my @curl_opt = ( + 'dump-header|D=s', + 'get|G!', +); + +my %Opt; get_options(\%Opt,\@curl_opt); #use Data::Dumper; die Dumper(\%Opt); -help() unless @ARGV; $main::_DATA_ = undef; +for (@curl_opt) { + s/\|.*$//; + next unless $Opt{$_}; + if ($Opt{$_} ne '1') { + push @ARGV,"--$_",$Opt{$_} + } else { + push @ARGV,"--$_"; + } +} ################################################################################# # @@ -47,9 +60,8 @@ help() unless @ARGV; $main::_DATA_ = undef; ################################################################################# my @cmd = ( #'echo', - 'curl', '-s', 'http://127.0.0.1', - '-H', 'Host: rent', - #'-D', '-', + 'curl', '-s', ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : 'http://127.0.0.1'), + '-H', 'Host: '.($ENV{$UC_NAME.'_HOST'} ? $ENV{$UC_NAME.'_HOST'} : 'rent'), ); # @@ -94,10 +106,9 @@ 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); +push(@cmd,map {('--data-binary',esc($_).'='.esc($keys{$_}))} sort keys %keys); -#die Dumper(\@cmd,\%keys); -#push(@cmd,'--data-raw',esc($k).'='.esc($v)); +#use Data::Dumper; die Dumper(\@cmd,\%keys); #warn 'Uri: '.join("&",@keys)."\n" if $VERBOSE>1; warn 'Command:',map{(/^--data/?"\n ":" ").$_} @cmd,"\n" if $VERBOSE; #die join(" ",@cmd); @@ -156,6 +167,7 @@ sub csv2txt { my $t = -1; my $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10); my $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10); + my $tot = @lines - ($noheader ? 0 : 1); print $sep_line; @@ -171,6 +183,7 @@ sub csv2txt { } print $sep_line; + print "$tot Records\n"; #close STDOUT; close STDERR; #close STDIN; exit; @@ -232,14 +245,13 @@ sub version { print "$NAME: version [$VERSION]\n"; exit 0; } #------------------------------------------------------------------------------ sub get_options { + my $hash = shift @_; + my $default = shift @_; use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); - my @Opt = @_>1 ? $_[1] : (); - 0 and @Opt = ( - 'zaza', - 'dumper|D=s', - ); - + #my @Opt = @_>1 ? @{$_[1]} : (); + my @Opt = $default ? @$default : (); + #use Data::Dumper; die Dumper(@Opt); sub pod_opt { local $_; my $o = shift; @@ -253,13 +265,13 @@ sub get_options { $main::_DATA_ .= $_; } - GetOptions($_[0],@Opt) || exit -1; + GetOptions($hash,@Opt) || exit -1; - help() if $_[0]{'help'} or $_[0]{'man'}; - version() if $_[0]{'version'}; + help() if $hash->{'help'} or $_[0]{'man'}; + version() if $hash->{'version'}; - $main::VERBOSE = $VERBOSE = $_[0]{'verbose'} if defined $_[0]{'verbose'}; - $main::DEBUG = $DEBUG = $_[0]{'debug'} if defined $_[0]{'debug'}; + $main::VERBOSE = $VERBOSE = $hash->{'verbose'} if defined $_[0]{'verbose'}; + $main::DEBUG = $DEBUG = $hash->{'debug'} if defined $_[0]{'debug'}; } -- 2.47.3