Convert to allow command line options instead of prompting.

Prompting code is left in place however if CLI options aren't given.

Also, added a check to verify supporter id is valid.
This commit is contained in:
Bradley M. Kuhn 2020-12-22 16:25:50 -08:00
parent 8dbc9b2ec6
commit 81352e70b4

View file

@ -3,17 +3,29 @@
use strict; use strict;
use warnings; use warnings;
use autodie qw(:all);
use Getopt::Long;
use File::Spec::Functions qw(rel2abs catfile);
use DBI; use DBI;
use Encode qw(encode decode); use Encode qw(encode decode);
use Supporters; use Supporters;
if (@ARGV != 1 and @ARGV !=2) { my($VERBOSE, $supporterId, $requestType, $requestConfig, $SUPPORTERS_SQLITE_DB_FILE) = (0, undef, undef, undef,
print STDERR "usage: $0 <SUPPORTERS_SQLITE_DB_FILE> <VERBOSITY_LEVEL>\n"; catfile($ENV{CONSERVANCY_REPOSITORY},
exit 1; 'Financial', 'Ledger', 'supporters.db'));
} GetOptions("verbose=i" => \$VERBOSE, "supporterDB=s" => \$SUPPORTERS_SQLITE_DB_FILE,
'supporterId=i' => \$supporterId, 'requestType=s' => \$requestType, 'requestConfig=s' => \$requestConfig, );
my($SUPPORTERS_SQLITE_DB_FILE, $VERBOSE) = @ARGV;
$VERBOSE = 0 if not defined $VERBOSE; sub UsageAndExit($) {
print STDERR "usage: $0 [ --supporterId=i --requestyType=STR --requestConfig=STR --supportersDB=PATH_TO_SUPPORTERS_SQLITE_DB_FILE --verbose=N ]\n";
print STDERR "\n $_[0]\n";
exit 2;
}
UsageAndExit("Cannot read supporters db file: $SUPPORTERS_SQLITE_DB_FILE") unless defined $SUPPORTERS_SQLITE_DB_FILE
and -r $SUPPORTERS_SQLITE_DB_FILE;
my $dbh = DBI->connect("dbi:SQLite:dbname=$SUPPORTERS_SQLITE_DB_FILE", "", "", my $dbh = DBI->connect("dbi:SQLite:dbname=$SUPPORTERS_SQLITE_DB_FILE", "", "",
{ RaiseError => 1, sqlite_unicode => 1 }) { RaiseError => 1, sqlite_unicode => 1 })
@ -21,19 +33,28 @@ my $dbh = DBI->connect("dbi:SQLite:dbname=$SUPPORTERS_SQLITE_DB_FILE", "", "",
my $sp = new Supporters($dbh, [ "none" ]); my $sp = new Supporters($dbh, [ "none" ]);
if (defined $supporterId) {
UsageAndExit("$supporterId is not a valid supporter id") unless $sp->_verifyId($supporterId);
} else {
print "Supporter Id: "; print "Supporter Id: ";
my $supporterId = <STDIN>; my $supporterId = <STDIN>;
chomp $supporterId; chomp $supporterId;
}
my @requestTypes = $sp->getRequestType(); my @requestTypes = $sp->getRequestType();
my %requestTypes; my %requestTypes;
@requestTypes{@requestTypes} = @requestTypes; @requestTypes{@requestTypes} = @requestTypes;
my $requestType = ""; if (defined $requestType) {
UsageAndExit("requestType must be one of the following: (". join(", ", @requestTypes) . ")\n")
unless defined $requestTypes{$requestType};
} else {
$requestType = "";
while (not defined $requestTypes{$requestType}) { while (not defined $requestTypes{$requestType}) {
print "Request Type (", join(", ", @requestTypes), "): "; print "Request Type (", join(", ", @requestTypes), "): ";
$requestType = <STDIN>; $requestType = <STDIN>;
chomp $requestType; chomp $requestType;
} }
}
my $configs = $sp->getRequestConfigurations($requestType); my $configs = $sp->getRequestConfigurations($requestType);
die "problematic on configs" if (keys %$configs != 1); die "problematic on configs" if (keys %$configs != 1);
@ -41,7 +62,13 @@ my $requestId = (keys(%$configs)) [0];
print "Using request id, $requestId\n"; print "Using request id, $requestId\n";
my $requestConfig; if (defined $requestConfig) {
UsageAndExit("requestType, $requestType does not have any valid config options yet you provided requestConfig of $requestConfig")
if (scalar keys(%{$configs->{$requestId}}) <= 0);
UsageAndExit("requestConfig must be one of the following: (" . join(", ",
keys(%{$configs->{$requestId}})) . ")\n")
unless defined $configs->{$requestId}{$requestConfig};
} else {
if (scalar keys(%{$configs->{$requestId}}) > 0) { if (scalar keys(%{$configs->{$requestId}}) > 0) {
while (not defined $requestConfig or not defined $configs->{$requestId}{$requestConfig}) { while (not defined $requestConfig or not defined $configs->{$requestId}{$requestConfig}) {
print "Request Config (", join(", ", keys(%{$configs->{$requestId}})), "): "; print "Request Config (", join(", ", keys(%{$configs->{$requestId}})), "): ";
@ -49,6 +76,7 @@ if (scalar keys(%{$configs->{$requestId}}) > 0) {
chomp $requestConfig; chomp $requestConfig;
} }
} }
}
if ($requestType) { if ($requestType) {
my $requestParamaters; my $requestParamaters;