]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei_overview: start implementing format detection
authorEric Wong <e@80x24.org>
Tue, 19 Jan 2021 09:34:35 +0000 (09:34 +0000)
committerEric Wong <e@80x24.org>
Thu, 21 Jan 2021 03:29:14 +0000 (03:29 +0000)
We'll need it for IMAP support, at least.  Proper mbox family
detection will be expensive, so deal with it later.

lib/PublicInbox/LeiOverview.pm
t/lei.t

index a7021b03d9246da2c4b584a0353ec73f58f17b40..dcc3088ba8f35e4b45fdf8eefffbb8b6c6f5bb51 100644 (file)
@@ -38,6 +38,21 @@ sub ovv_out_lk_cancel ($) {
                unlink(delete($self->{lock_path}));
 }
 
+sub detect_fmt ($$) {
+       my ($lei, $dst) = @_;
+       if ($dst =~ m!\A([:/]+://)!) {
+               $lei->fail("$1 support not implemented, yet\n");
+       } elsif (!-e $dst) {
+               'maildir'; # the default
+       } elsif (-f _ || -p _) {
+               $lei->fail("unable to determine mbox family of $dst\n");
+       } elsif (-d _) { # TODO: MH?
+               'maildir';
+       } else {
+               $lei->fail("unable to determine format of $dst\n");
+       }
+}
+
 sub new {
        my ($class, $lei) = @_;
        my $opt = $lei->{opt};
@@ -54,7 +69,7 @@ sub new {
 
        }
        $fmt //= 'json' if $dst eq '/dev/stdout';
-       $fmt //= 'maildir';
+       $fmt //= detect_fmt($lei, $dst) or return;
 
        if (index($dst, '://') < 0) { # not a URL, so assume path
                 $dst = File::Spec->canonpath($dst);
diff --git a/t/lei.t b/t/lei.t
index 8bb4e439bfadbbc98171155e10a5317ab2528c20..64cb5f0ef55378d4e4f9f5fb4ef689d304a3cf0e 100644 (file)
--- a/t/lei.t
+++ b/t/lei.t
@@ -232,6 +232,8 @@ my $test_external = sub {
                my @res = $cat->();
                is_deeply(\@res, [], "clobber w/o --augment $sfx");
        }
+       ok(!$lei->('q', '-o', "$home/mbox", 's:nope'),
+                       'fails if mbox format unspecified');
 };
 
 my $test_lei_common = sub {