Support format option provided by client

The format option on bean-query command-line is now an option that
the client can pass along.  The setting defaults to whatever
`bean-query` was going to default to.
This commit is contained in:
Bradley M. Kuhn 2020-06-15 20:37:49 -07:00
parent e9d0a8b857
commit 6ef1ba0422
No known key found for this signature in database
GPG key ID: F15E8BD6D05E26B3
2 changed files with 13 additions and 4 deletions

View file

@ -15,8 +15,8 @@ sub BeancountQueryInitialize {
die "BEANCOUNT_QUERY: tie failed: is the goffy beancount server running?\n";
}
sub BeancountQuerySubmit($) {
my($question) = @_;
sub BeancountQuerySubmit($;$) {
my($question, $format) = @_;
while (defined $BEANCOUNT_QUERY{fifoName} or defined $BEANCOUNT_QUERY{question}) { sleep 1; }
(tied %BEANCOUNT_QUERY)->shlock;
if (defined $BEANCOUNT_QUERY{fifoName} or defined $BEANCOUNT_QUERY{question}) {
@ -27,6 +27,7 @@ sub BeancountQuerySubmit($) {
"fifoName: \"$BEANCOUNT_QUERY{fifoName}\" question: \"$BEANCOUNT_QUERY{question}\"!");
}
$BEANCOUNT_QUERY{question} = $question;
$BEANCOUNT_QUERY{format} = $format if defined $format;
(tied %BEANCOUNT_QUERY)->shunlock;
while (not defined $BEANCOUNT_QUERY{fifoName}) { sleep 1; }
die "Ceci n'est pas une pipe: BEANCOUNT_QUERY{fifoName}, $BEANCOUNT_QUERY{fifoName}:$!"

View file

@ -87,11 +87,19 @@ while (1) {
} elsif ($query{question} !~ /^[\-\@\w.\s\"\'\_\(\)]+$/) {
print STDERR "Query string $query{question} looks suspicious, not running beancount query!\n";
(tied %query)->shlock;
$query{question} = undef;
$query{question} = $query{format} = undef;
$query{fifoName} = mktemp("REJECTED_beancount-query-fifo-this-file-does-not-exist_${$}_XXXXXXXXX");
(tied %query)->shunlock;
} elsif (defined $query{format} and $query{format} !~ /^(?:csv|text)$/) {
print STDERR "format string $query{format} is not text or csv, not running beancount query!\n";
(tied %query)->shlock;
$query{question} = $query{format} = undef;
$query{fifoName} = mktemp("REJECTED_beancount-query-fifo-this-file-does-not-exist_${$}_XXXXXXXXX");
(tied %query)->shunlock;
} elsif (not defined $query{fifoName}) {
my @cmd = ($BEANCOUNT_QUERY_CMD, '-f', 'text', $LOAD_FILE, $query{question});
my @cmd = ($BEANCOUNT_QUERY_CMD);
push(@cmd, '-f', $query{format}) if defined $query{format};
push(@cmd, $LOAD_FILE, $query{question});
print STDERR "Running query: $query{question}\n" if $VERBOSE > 0;
open(my $beancountFH, "-|", @cmd);
print STDERR "Running ", join(" ", @cmd), "\n" if $VERBOSE > 1;