]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiSavedSearch.pm
lei ls-search: command to list saved searches
[public-inbox.git] / lib / PublicInbox / LeiSavedSearch.pm
index 3076d14c1b4ae3959ddc363e08b2e69325503ebf..d67622c9b08ce6bfc30fa75cef75cb9206ba06ea 100644 (file)
@@ -21,6 +21,11 @@ sub cquote_val ($) { # cf. git-config(1)
        $val;
 }
 
+sub ARRAY_FIELDS () { qw(only include exclude) }
+sub BOOL_FIELDS () {
+       qw(external local remote import-remote import-before threads)
+}
+
 sub lss_dir_for ($$) {
        my ($lei, $dstref) = @_;
        my @n;
@@ -39,6 +44,31 @@ sub lss_dir_for ($$) {
        $lei->share_path . '/saved-searches/' . join('-', @n);
 }
 
+sub list {
+       my ($lei, $pfx) = @_;
+       my $lss_dir = $lei->share_path.'/saved-searches/';
+       return () unless -d $lss_dir;
+       # TODO: persist the cache?  Use another format?
+       my $f = $lei->cache_dir."/saved-tmp.$$.".time.'.config';
+       open my $fh, '>', $f or die "open $f: $!";
+       print $fh "[include]\n";
+       for my $p (glob("$lss_dir/*/lei.saved-search")) {
+               print $fh "\tpath = ", cquote_val($p), "\n";
+       }
+       close $fh or die "close $f: $!";
+       my $cfg = PublicInbox::Config::git_config_dump($f);
+       unlink($f);
+       bless $cfg, 'PublicInbox::Config';
+       my $out = $cfg->get_all('lei.q.output') or return ();
+       map {;
+               if (s!\A(?:maildir|mh|mbox.+|mmdf):!!i) {
+                       -e $_ ? $_ : (); # TODO auto-prune somewhere?
+               } else { # IMAP, maybe JMAP
+                       $_;
+               }
+       } @$out
+}
+
 sub new {
        my ($cls, $lei, $dst) = @_;
        my $self = bless { ale => $lei->ale }, $cls;
@@ -74,16 +104,15 @@ $q
 [lei "q"]
        output = $dst
 EOM
-               for my $k (qw(only include exclude)) {
+               for my $k (ARRAY_FIELDS) {
                        my $ary = $lei->{opt}->{$k} // next;
                        for my $x (@$ary) {
                                print $fh "\t$k = ".cquote_val($x)."\n";
                        }
                }
-               for my $k (qw(external local remote import-remote
-                               import-before threads)) {
+               for my $k (BOOL_FIELDS) {
                        my $val = $lei->{opt}->{$k} // next;
-                       print $fh "\t$k = ".cquote_val($val)."\n";
+                       print $fh "\t$k = ".($val ? 1 : 0)."\n";
                }
                close($fh) or return $lei->fail("close $f: $!");
        }