From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id C843C1FB07 for ; Fri, 3 Sep 2021 08:54:27 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 6/8] lei inspect: support reading eml from --stdin Date: Fri, 3 Sep 2021 08:54:25 +0000 Message-Id: <20210903085427.5541-7-e@80x24.org> In-Reply-To: <20210903085427.5541-1-e@80x24.org> References: <20210903085427.5541-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This can be useful inside mutt since I was diagnosing why a label ("L:$FOO") search was giving me a false-positive search result... --- lib/PublicInbox/LEI.pm | 4 ++-- lib/PublicInbox/LeiInspect.pm | 40 ++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 8b6c1d36..098a45ba 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -279,8 +279,8 @@ our %CMD = ( # sorted in order of importance/use: 'git-config(1) wrapper for '._config_path($_[0]); }, qw(config-file|system|global|file|f=s), # for conflict detection qw(c=s@ C=s@), pass_through('git config') ], -'inspect' => [ 'ITEMS...', 'inspect lei/store and/or local external', - qw(pretty ascii dir=s), @c_opt ], +'inspect' => [ 'ITEMS...|--stdin', 'inspect lei/store and/or local external', + qw(stdin| pretty ascii dir=s), @c_opt ], 'init' => [ '[DIRNAME]', sub { "initialize storage, default: ".store_path($_[0]); diff --git a/lib/PublicInbox/LeiInspect.pm b/lib/PublicInbox/LeiInspect.pm index 2ade17af..25bd47e7 100644 --- a/lib/PublicInbox/LeiInspect.pm +++ b/lib/PublicInbox/LeiInspect.pm @@ -9,6 +9,7 @@ package PublicInbox::LeiInspect; use strict; use v5.10.1; use PublicInbox::Config; +use PublicInbox::MID qw(mids); sub inspect_blob ($$) { my ($lei, $oidhex) = @_; @@ -184,6 +185,32 @@ sub inspect1 ($$$) { 1; } +sub _inspect_argv ($$) { + my ($lei, $argv) = @_; + my $multi = scalar(@$argv) > 1; + $lei->out('[') if $multi; + while (defined(my $x = shift @$argv)) { + inspect1($lei, $x, scalar(@$argv)) or return; + } + $lei->out(']') if $multi; +} + +sub ins_add { # InputPipe->consume callback + my ($lei) = @_; # $_[1] = $rbuf + if (defined $_[1]) { + $_[1] eq '' and return eval { + my $str = delete $lei->{istr}; + $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s; + my $eml = PublicInbox::Eml->new(\$str); + _inspect_argv($lei, [ 'blob:'.$lei->git_blob_id($eml), + map { "mid:$_" } @{mids($eml)} ]); + }; + $lei->{istr} .= $_[1]; + } else { + $lei->fail("error reading stdin: $!"); + } +} + sub lei_inspect { my ($lei, @argv) = @_; $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical; @@ -196,12 +223,15 @@ sub lei_inspect { } $lei->start_pager if -t $lei->{1}; $lei->{1}->autoflush(0); - my $multi = scalar(@argv) > 1; - $lei->out('[') if $multi; - while (defined(my $x = shift @argv)) { - inspect1($lei, $x, scalar(@argv)) or return; + if ($lei->{opt}->{stdin}) { + return $lei->fail(<<'') if @argv; +no args allowed on command-line with --stdin + + require PublicInbox::InputPipe; + PublicInbox::InputPipe::consume($lei->{0}, \&ins_add, $lei); + return; } - $lei->out(']') if $multi; + _inspect_argv($lei, \@argv); } sub _complete_inspect {