]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei import: move check_input_format to lei
authorEric Wong <e@80x24.org>
Wed, 17 Feb 2021 10:07:01 +0000 (09:07 -0100)
committerEric Wong <e@80x24.org>
Thu, 18 Feb 2021 07:57:17 +0000 (03:57 -0400)
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
lib/PublicInbox/LeiImport.pm

index 12deedd84ecface19a0f9620a0af75903a8f08fd..1fa9f7516633918a800418e775152c550c7b6356 100644 (file)
@@ -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
index b25d7e97987286a3b3f43e8a3b16ca5a02a1b214..32f3a4673d3f79c943bbec8f4d67fa708354955b 100644 (file)
@@ -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);
                }