#
#################################################################################
my $VERSION = '0.0.1';
-#
# NB 28.02.16
# - create script
my ($NAME) = $0 =~ m,([^/]+)$,;
my $UC_NAME = uc($NAME);
my %ACTION = (
- 'format' => $ENV{$UC_NAME.'_FORMAT'} ? $ENV{$UC_NAME.'_FORMAT'} : 'json',
+ 'format' => $ENV{$UC_NAME.'_FORMAT'} ? $ENV{$UC_NAME.'_FORMAT'} : 'nc',
'action' => $ENV{$UC_NAME.'_ACTION'} ? $ENV{$UC_NAME.'_ACTION'} : 'rows',
-);
-my @CURL_OPT = (
- 'dump-header|D=s',
- 'get|G!',
+ 'txt_errors' => $ENV{$UC_NAME.'_TXT_ERRORS'} ? $ENV{$UC_NAME.'_TXT_ERRORS'} : '1',
);
my %CMD_ALIASE = (
'l' => 'limit',
);
+my @CURL_OPT = (
+ 'dump-header|D=s',
+ 'get|G!',
+ 'compressed|z!',
+);
+my $CURL_OPT_EXP = join('|',map{ s/\|.*$//; $_ } @CURL_OPT);
+# NB 29.02.16 add_argv('--compressed');
+# NB 29.02.16 sub add_argv {
+# NB 29.02.16 #my $search = shift;
+# NB 29.02.16 my $found = 0;
+# NB 29.02.16 foreach my $search (@_) {
+# NB 29.02.16 foreach (@ARGV) { $found == 1 and last if $_ eq $search; }
+# NB 29.02.16 #die @ARGV,$search unless $found;
+# NB 29.02.16 #push @ARGV,$search unless $found;
+# NB 29.02.16 }
+# NB 29.02.16 }
#################################################################################
#
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;
+parse_debug() if @ARGV and $ARGV[0] eq '--parse_debug' and shift @ARGV;
+curl_help_hash() if @ARGV and $ARGV[0] eq '--curl_help_hash' and shift @ARGV;
my %Opt; get_options(\%Opt,\@CURL_OPT);
#use Data::Dumper; die Dumper(\%Opt);
+$Opt{compressed} = 1 if exists $Opt{compressed};
+
for (@CURL_OPT) {
s/\|.*$//;
next unless $Opt{$_};
# BEGIN
#
#################################################################################
+
+#################################################################################
+#
+# Build command
+#
my @cmd = (
#'echo',
'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'),
);
+#
+# Add args
+#
+push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
+
#
# Distinct arg from key=value
#
}
-#
-# Add args
-#
-push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
-
#
# Defaults Actions
#
$keys{header} = '0' if $keys{action} ne 'rows';
+#################################################################################
#
-# Process specifics
+# Parser
#
-#die if !$keys{table} or $keys{action} !~ /rows/;
+if ($keys{format} eq 'nc') {
+ $keys{format} = 'csv';
+ open(STDOUT,"|$0 --parse_debug|$0 --csv2txt ".( ( defined($keys{header}) ? $keys{header} : '') eq '0' ? '1' : '0'))
-open(STDOUT,"|$0 --csv2txt ".( ( defined($keys{header}) ? $keys{header} : '') eq '0' ? '1' : '0'))
- if $keys{format} eq 'nc' and $keys{format}='csv';
+} elsif ( $keys{format} eq 'jq' and $keys{format}='json' ) {
+ open(STDOUT,"|$0 --parse_debug|jq .")
-open(STDOUT,"|jq .") if $keys{format} eq 'jq' and $keys{format}='json';
-
-#
-# Push key=value
-#
-push(@cmd,map {('--data-binary',esc($_).'='.esc($keys{$_}))} sort keys %keys);
+} elsif ( $keys{debug} ) {
+ open(STDOUT,"|$0 --parse_debug")
+}
#use Data::Dumper; die Dumper(\@cmd,\%keys);
#warn 'Uri: '.join("&",@keys)."\n" if $VERBOSE>1;
-warn 'Command:',map{(/^--data/?"\n ":" ").$_} @cmd,"\n" if $VERBOSE;
+#warn 'Command:',map{(/^--data/?"\n ":" ").$_} @cmd,"\n" if $VERBOSE;
#die join(" ",@cmd);
#################################################################################
#
-# END
+# END - Exec command
#
#################################################################################
+
+# Push key=value
+push(@cmd,map {('--data-binary',esc($_).'='.esc($keys{$_}))} sort keys %keys);
+
+warn "$NAME: Command: ",join(" ",map{/\s+/ ? '"'.$_.'"' : $_} @cmd),"\n" if $VERBOSE;
system @cmd;
close STDOUT;
exit 0;
# Functions
#
#################################################################################
+sub parse_debug {
+ while (<>) {
+
+ if (/DEBUG: /) { print STDERR $_ }
+ elsif ( /^(<\w+>)?Fatal error/ .. /^\s+thrown in/) { s/<[\w\/ ]+>//g; print STDERR $_ }
+ else { print $_ }
+
+ }
+ exit;
+}
+
sub csv2txt {
my $sep = shift;
my $noheader = shift;
my @lines = ();
my $l;
+ # sprintf missing argument warning message ????
+ # Can't open XXX: No such file or directory
+ no warnings;
+
#open(STDOUT,">&2");
#
# Store lines and lengths in memory
print $sep_line;
- no warnings; # sprintf missing argument warning message ????
while ($_ = shift @lines) {
#warn $format,scalar(@$_);
#warn ">>$format: ".$_->[0];
#------------------------------------------------------------------------------
sub get_options {
- my $hash = shift @_;
- my $default = shift @_;
+ my $hash = $_[0]; #shift @_;
+ my $default = $_[1]; #shift @_;
use Getopt::Long qw(:config no_ignore_case no_auto_abbrev);
#my @Opt = @_>1 ? @{$_[1]} : ();
}
+sub curl_help_hash {
+ open(CURL,'curl --help |') or die "Can't run curl";
+ my $o = $CURL_OPT_EXP;
+ while (<CURL>) {
+ #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<option[$_[1]$_[0]$type]> $_[2]\n\n";
+ print "'$_[1]$_[0]$type', # $_[2]\n";
+ }
+ close CURL;
+ exit;
+}
sub curl_help {
open(CURL,'curl --help |') or die "Can't run curl";
- my $o = 'get|dump-header|cert-status';
+ my $o = $CURL_OPT_EXP;
while (<CURL>) {
#print $_;
#/^\s*(?:-(\w+),\s*)?--([\w-]+)(.*?)\$/ or next;