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-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 C755A1FB0E for ; Wed, 17 Feb 2021 10:07:07 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 05/11] lei import: move check_input_format to lei Date: Wed, 17 Feb 2021 09:07:01 -0100 Message-Id: <20210217100707.6796-6-e@80x24.org> In-Reply-To: <20210217100707.6796-1-e@80x24.org> References: <20210217100707.6796-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We'll be supporting "lei convert" in a future change; so it makes sense to share a common internal API for common error messages. --- lib/PublicInbox/LEI.pm | 14 ++++++++++++++ lib/PublicInbox/LeiImport.pm | 17 ++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 12deedd8..1fa9f751 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -391,6 +391,20 @@ sub fail ($$;$) { undef; } +sub check_input_format ($;$) { + my ($self, $files) = @_; + my $fmt = $self->{opt}->{'format'}; + if (!$fmt) { + my $err = $files ? "regular file(s):\n@$files" : '--stdin'; + return fail($self, "--format unset for $err"); + } + return 1 if $fmt eq 'eml'; + # XXX: should this handle {gz,bz2,xz}? that's currently in LeiToMail + require PublicInbox::MboxReader; + PublicInbox::MboxReader->can($fmt) || + fail($self, "--format=$fmt unrecognized"); +} + sub out ($;@) { my $self = shift; return if print { $self->{1} // return } @_; # likely diff --git a/lib/PublicInbox/LeiImport.pm b/lib/PublicInbox/LeiImport.pm index b25d7e97..32f3a467 100644 --- a/lib/PublicInbox/LeiImport.pm +++ b/lib/PublicInbox/LeiImport.pm @@ -29,19 +29,6 @@ sub import_done { # EOF callback for main daemon $imp->wq_wait_old(\&import_done_wait, $lei); } -sub check_fmt ($;$) { - my ($lei, $f) = @_; - my $fmt = $lei->{opt}->{'format'}; - if (!$fmt) { - my $err = $f ? "regular file(s):\n@$f" : '--stdin'; - return $lei->fail("--format unset for $err"); - } - return 1 if $fmt eq 'eml'; - require PublicInbox::MboxReader; - PublicInbox::MboxReader->can($fmt) || - $lei->fail( "--format=$fmt unrecognized\n"); -} - sub do_import { my ($lei) = @_; my $ops = { @@ -82,7 +69,7 @@ sub call { # the main "lei import" method if ($lei->{opt}->{stdin}) { @argv and return $lei->fail("--stdin and locations (@argv) do not mix"); - check_fmt($lei) or return; + $lei->check_input_format or return; $self->{0} = $lei->{0}; } else { my @f; @@ -95,7 +82,7 @@ sub call { # the main "lei import" method $lei->{nrd}->add_url($x); } } - if (@f) { check_fmt($lei, \@f) or return } + if (@f) { $lei->check_input_format(\@f) or return } if ($lei->{nrd} && (my @err = $lei->{nrd}->errors)) { return $lei->fail(@err); }