about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-05-15 01:18:09 +0000
committerEric Wong <e@80x24.org>2019-05-15 06:15:34 +0000
commitd0e8bfd866ed1e924e8d9f551939eecbea4920ef (patch)
treef8d4ba5e37452e42d9851e793d5132484907e123 /lib
parentb5c64fc01d3e7ca6243fdd8b811b6291bdb12235 (diff)
downloadpublic-inbox-d0e8bfd866ed1e924e8d9f551939eecbea4920ef.tar.gz
Since we lazy-load Xapian now, some errors may become
more cryptic or buried.  Try to improve that by making
Admin show better errors.
Diffstat (limited to 'lib')
-rw-r--r--lib/PublicInbox/Admin.pm60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/PublicInbox/Admin.pm b/lib/PublicInbox/Admin.pm
index d0a8dd00..3eff5cde 100644
--- a/lib/PublicInbox/Admin.pm
+++ b/lib/PublicInbox/Admin.pm
@@ -41,4 +41,64 @@ sub resolve_repo_dir {
         }
 }
 
+# TODO: make Devel::Peek optional, only used for daemon
+my @base_mod = qw(Email::MIME Date::Parse Devel::Peek);
+my @over_mod = qw(DBD::SQLite DBI);
+my %mod_groups = (
+        -index => [ @base_mod, @over_mod ],
+        -base => \@base_mod,
+        -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
+);
+
+sub scan_ibx_modules ($$) {
+        my ($mods, $ibx) = @_;
+        if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
+                $mods->{'Search::Xapian'} = 1;
+        } else {
+                $mods->{$_} = 1 foreach @over_mod;
+        }
+}
+
+sub check_require {
+        my (@mods) = @_;
+        my $err = {};
+        while (my $mod = shift @mods) {
+                if (my $groups = $mod_groups{$mod}) {
+                        push @mods, @$groups;
+                } else {
+                        eval "require $mod";
+                        $err->{$mod} = $@ if $@;
+                }
+        }
+        scalar keys %$err ? $err : undef;
+}
+
+sub missing_mod_msg {
+        my ($err) = @_;
+        my @mods = map { "`$_'" } sort keys %$err;
+        my $last = pop @mods;
+        @mods ? (join(', ', @mods)."' and $last") : $last
+}
+
+sub require_or_die {
+        my $err = check_require(@_) or return;
+        die missing_mod_msg($err)." required for $0\n";
+}
+
+sub indexlevel_ok_or_die ($) {
+        my ($indexlevel) = @_;
+        my $req;
+        if ($indexlevel eq 'basic') {
+                $req = '-index';
+        } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
+                $req = '-search';
+        } else {
+                die <<"";
+invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
+
+        }
+        my $err = check_require($req) or return;
+        die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
+}
+
 1;