From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.6 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NUMERIC_HTTP_ADDR, WEIRD_PORT shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 6F8441F452 for ; Tue, 17 Oct 2023 10:11:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1697537467; bh=JkLKxLbMgp/pFzkKzABhT6QqoMM77ert5hcd4lskkUk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=aU6HpPsVAhpSempVsu/AAyv8TLx/e+76jKGCBN068Yrd6b0+a4EtPOUfYtXayKXxF uRDAlyrjiLkTzZ589wWM/K2jNit8pjqIebUA0Kwe0OuBH8PPUnBYeuCA1BbBjH9xuK lSLkU+3KeO6I4CRaYDgh0gN3zUkqvK3A0Gl9jeQU= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 1/3] lei: consolidate stdin slurp, fix warnings Date: Tue, 17 Oct 2023 10:11:04 +0000 Message-Id: <20231017101106.582556-2-e@80x24.org> In-Reply-To: <20231017101106.582556-1-e@80x24.org> References: <20231017101106.582556-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We can share more code amongst stdin slurper (not streaming) commands. This also fixes uninitialized variable warnings when feeding an empty stdin to these commands. --- lib/PublicInbox/LEI.pm | 13 +++++++++++++ lib/PublicInbox/LeiInspect.pm | 12 ++---------- lib/PublicInbox/LeiLcat.pm | 13 ++----------- lib/PublicInbox/LeiQuery.pm | 14 +++----------- t/lei.t | 5 +++++ 5 files changed, 25 insertions(+), 32 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index b00be1a1..1ff6d67f 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -1573,4 +1573,17 @@ sub request_umask { $u eq 'u' or warn "E: recv $v has no umask"; } +sub _stdin_cb { # PublicInbox::InputPipe::consume callback for --stdin + my ($lei, $cb) = @_; # $_[-1] = $rbuf + $_[1] // return $lei->fail("error reading stdin: $!"); + $lei->{stdin_buf} .= $_[-1]; + do_env($lei, $cb) if $_[-1] eq ''; +} + +sub slurp_stdin { + my ($lei, $cb) = @_; + require PublicInbox::InputPipe; + PublicInbox::InputPipe::consume($lei->{0}, \&_stdin_cb, $lei, $cb); +} + 1; diff --git a/lib/PublicInbox/LeiInspect.pm b/lib/PublicInbox/LeiInspect.pm index 65c64cf2..d4ad03eb 100644 --- a/lib/PublicInbox/LeiInspect.pm +++ b/lib/PublicInbox/LeiInspect.pm @@ -253,20 +253,13 @@ sub inspect_start ($$) { sub do_inspect { # lei->do_env cb my ($lei) = @_; - my $str = delete $lei->{istr}; + my $str = delete $lei->{stdin_buf}; PublicInbox::Eml::strip_from($str); my $eml = PublicInbox::Eml->new(\$str); inspect_start($lei, [ 'blob:'.$lei->git_oid($eml)->hexdigest, map { "mid:$_" } @{mids($eml)} ]); } -sub ins_add { # InputPipe->consume callback - my ($lei) = @_; # $_[1] = $rbuf - $_[1] // return $lei->fail("error reading stdin: $!"); - return $lei->{istr} .= $_[1] if $_[1] ne ''; - $lei->do_env(\&do_inspect); -} - sub lei_inspect { my ($lei, @argv) = @_; $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical; @@ -281,8 +274,7 @@ sub lei_inspect { return $lei->fail(<<'') if @argv; no args allowed on command-line with --stdin - require PublicInbox::InputPipe; - PublicInbox::InputPipe::consume($lei->{0}, \&ins_add, $lei); + $lei->slurp_stdin(\&do_inspect); } else { inspect_start($lei, \@argv); } diff --git a/lib/PublicInbox/LeiLcat.pm b/lib/PublicInbox/LeiLcat.pm index 72875dc6..274a9605 100644 --- a/lib/PublicInbox/LeiLcat.pm +++ b/lib/PublicInbox/LeiLcat.pm @@ -124,18 +124,11 @@ could not extract Message-ID from $x sub do_lcat { # lei->do_env cb my ($lei) = @_; - my @argv = split(/\s+/, $lei->{mset_opt}->{qstr}); + my @argv = split(/\s+/, delete($lei->{stdin_buf})); $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return; $lei->_start_query; } -sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin - my ($lei) = @_; # $_[1] = $rbuf - $_[1] // return $lei->fail("error reading stdin: $!"); - return $lei->{mset_opt}->{qstr} .= $_[1] if $_[1] ne ''; - $lei->do_env(\&do_lcat); -} - sub lei_lcat { my ($lei, @argv) = @_; my $lxs = $lei->lxs_prepare or return; @@ -152,9 +145,7 @@ sub lei_lcat { return $lei->fail(<<'') if @argv; no args allowed on command-line with --stdin - require PublicInbox::InputPipe; - PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei); - return; + return $lei->slurp_stdin(\&do_lcat); } $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return; $lei->_start_query; diff --git a/lib/PublicInbox/LeiQuery.pm b/lib/PublicInbox/LeiQuery.pm index e2d8a096..eadf811f 100644 --- a/lib/PublicInbox/LeiQuery.pm +++ b/lib/PublicInbox/LeiQuery.pm @@ -61,19 +61,13 @@ sub _start_query { # used by "lei q" and "lei up" sub do_qry { # do_env cb my ($lei) = @_; - $lei->{mset_opt}->{q_raw} = $lei->{mset_opt}->{qstr}; + $lei->{mset_opt}->{q_raw} = $lei->{mset_opt}->{qstr} + = delete $lei->{stdin_buf}; $lei->{lse}->query_approxidate($lei->{lse}->git, $lei->{mset_opt}->{qstr}); _start_query($lei); } -sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin - my ($lei) = @_; # $_[1] = $rbuf - $_[1] // $lei->fail("error reading stdin: $!"); - return $lei->{mset_opt}->{qstr} .= $_[1] if $_[1] ne ''; - $lei->do_env(\&do_qry); -} - # make the URI||PublicInbox::{Inbox,ExtSearch} a config-file friendly string sub cfg_ext ($) { my ($x) = @_; @@ -159,9 +153,7 @@ sub lei_q { return $self->fail(<<'') if @argv; no query allowed on command-line with --stdin - require PublicInbox::InputPipe; - PublicInbox::InputPipe::consume($self->{0}, \&qstr_add, $self); - return; + return $self->slurp_stdin(\&do_qry); } chomp(@argv) and $self->qerr("# trailing `\\n' removed"); $mset_opt{q_raw} = [ @argv ]; # copy diff --git a/t/lei.t b/t/lei.t index 3ac804a8..1dbc9d4c 100644 --- a/t/lei.t +++ b/t/lei.t @@ -182,6 +182,11 @@ my $test_fail = sub { } lei_ok('sucks', \'yes, but hopefully less every day'); like($lei_out, qr/loaded features/, 'loaded features shown'); + + lei_ok([qw(q --stdin -f text)], undef, { 0 => \'', %$lei_opt }); + is($lei_err, '', 'no errors on empty stdin'); + is($lei_out, '', 'no output on empty query'); + SKIP: { skip 'no curl', 3 unless require_cmd('curl', 1); lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));