about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-02-17 09:07:01 -0100
committerEric Wong <e@80x24.org>2021-02-18 03:57:17 -0400
commit6c163ca32e21cc0b85792a6c3adbcd5ce19393a5 (patch)
treeb56a91dd41774b11b14e9f3d59f9c2c53d8fc1ef
parentf72a415fd2594b2975cea2275dd67904e1f65a0c (diff)
downloadpublic-inbox-6c163ca32e21cc0b85792a6c3adbcd5ce19393a5.tar.gz
We'll be supporting "lei convert" in a future change; so it
makes sense to share a common internal API for common error
messages.
-rw-r--r--lib/PublicInbox/LEI.pm14
-rw-r--r--lib/PublicInbox/LeiImport.pm17
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);
                 }