]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInspect.pm
lei_mail_sync: args2folder: common folder lookup sub
[public-inbox.git] / lib / PublicInbox / LeiInspect.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # "lei inspect" general purpose inspector for stuff in SQLite and
5 # Xapian.  Will eventually be useful with plain public-inboxes,
6 # not just lei/store.  This is totally half-baked at the moment
7 # but useful for testing.
8 package PublicInbox::LeiInspect;
9 use strict;
10 use v5.10.1;
11 use PublicInbox::Config;
12
13 sub inspect_blob ($$) {
14         my ($lei, $oidhex) = @_;
15         my $ent = {};
16         if (my $lse = $lei->{lse}) {
17                 my @docids = $lse ? $lse->over->blob_exists($oidhex) : ();
18                 $ent->{'lei/store'} = \@docids if @docids;
19                 my $lms = $lse->lms;
20                 if (my $loc = $lms ? $lms->locations_for($oidhex) : undef) {
21                         $ent->{'mail-sync'} = $loc;
22                 }
23         }
24         $ent;
25 }
26
27 sub inspect_sync_folder ($$) {
28         my ($lei, $folder) = @_;
29         my $ent = {};
30         my $lse = $lei->{lse} or return $ent;
31         my $lms = $lse->lms or return $ent;
32         my $folders = [ $folder ];
33         my $err = $lms->arg2folder($lei, $folders);
34         if ($err) {
35                 if ($err->{fail}) {
36                         $lei->qerr("# no folders match $folder (non-fatal)");
37                         @$folders = ();
38                 }
39                 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
40         }
41         for my $f (@$folders) {
42                 $ent->{$f} = $lms->location_stats($f); # may be undef
43         }
44         $ent
45 }
46
47 sub inspect1 ($$$) {
48         my ($lei, $item, $more) = @_;
49         my $ent;
50         if ($item =~ /\Ablob:(.+)/) {
51                 $ent = inspect_blob($lei, $1);
52         } elsif ($item =~ m!\Aimaps?://!i ||
53                         $item =~ m!\A(?:maildir|mh):!i || -d $item) {
54                 $ent = inspect_sync_folder($lei, $item);
55         } else { # TODO: more things
56                 return $lei->fail("$item not understood");
57         }
58         $lei->out($lei->{json}->encode($ent));
59         $lei->out(',') if $more;
60         1;
61 }
62
63 sub lei_inspect {
64         my ($lei, @argv) = @_;
65         $lei->{1}->autoflush(0);
66         my $multi = scalar(@argv) > 1;
67         $lei->out('[') if $multi;
68         $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
69         $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
70                 my $sto = $lei->_lei_store;
71                 $sto ? $sto->search : undef;
72         } : undef;
73         if ($lei->{opt}->{pretty} || -t $lei->{1}) {
74                 $lei->{json}->pretty(1)->indent(2);
75         }
76         while (defined(my $x = shift @argv)) {
77                 inspect1($lei, $x, scalar(@argv)) or return;
78         }
79         $lei->out(']') if $multi;
80 }
81
82 1;