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 warnings;
use autodie qw(:all);
use Getopt::Long;
use File::Spec::Functions qw(rel2abs catfile);
use DBI;
use Encode qw(encode decode);
use Supporters;
if (@ARGV != 1 and @ARGV !=2) {
print STDERR "usage: $0 <SUPPORTERS_SQLITE_DB_FILE> <VERBOSITY_LEVEL>\n";
exit 1;
}
my($VERBOSE, $supporterId, $requestType, $requestConfig, $SUPPORTERS_SQLITE_DB_FILE) = (0, undef, undef, undef,
catfile($ENV{CONSERVANCY_REPOSITORY},
'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", "", "",
{ RaiseError => 1, sqlite_unicode => 1 })
@ -21,18 +33,27 @@ my $dbh = DBI->connect("dbi:SQLite:dbname=$SUPPORTERS_SQLITE_DB_FILE", "", "",
my $sp = new Supporters($dbh, [ "none" ]);
print "Supporter Id: ";
my $supporterId = <STDIN>;
chomp $supporterId;
if (defined $supporterId) {
UsageAndExit("$supporterId is not a valid supporter id") unless $sp->_verifyId($supporterId);
} else {
print "Supporter Id: ";
my $supporterId = <STDIN>;
chomp $supporterId;
}
my @requestTypes = $sp->getRequestType();
my %requestTypes;
@requestTypes{@requestTypes} = @requestTypes;
my $requestType = "";
while (not defined $requestTypes{$requestType}) {
print "Request Type (", join(", ", @requestTypes), "): ";
$requestType = <STDIN>;
chomp $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}) {
print "Request Type (", join(", ", @requestTypes), "): ";
$requestType = <STDIN>;
chomp $requestType;
}
}
my $configs = $sp->getRequestConfigurations($requestType);
@ -41,12 +62,19 @@ my $requestId = (keys(%$configs)) [0];
print "Using request id, $requestId\n";
my $requestConfig;
if (scalar keys(%{$configs->{$requestId}}) > 0) {
while (not defined $requestConfig or not defined $configs->{$requestId}{$requestConfig}) {
print "Request Config (", join(", ", keys(%{$configs->{$requestId}})), "): ";
$requestConfig = <STDIN>;
chomp $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) {
while (not defined $requestConfig or not defined $configs->{$requestId}{$requestConfig}) {
print "Request Config (", join(", ", keys(%{$configs->{$requestId}})), "): ";
$requestConfig = <STDIN>;
chomp $requestConfig;
}
}
}