]> 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 f40fe76bd0afef3cfe5f478f0496a91cd950256c..d67622c9b08ce6bfc30fa75cef75cb9206ba06ea 100644 (file)
@@ -13,8 +13,6 @@ use PublicInbox::Spawn qw(run_die);
 use PublicInbox::ContentHash qw(git_sha);
 use Digest::SHA qw(sha256_hex);
 
-*squote_maybe = \&PublicInbox::Config::squote_maybe;
-
 # move this to PublicInbox::Config if other things use it:
 my %cquote = ("\n" => '\\n', "\t" => '\\t', "\b" => '\\b');
 sub cquote_val ($) { # cf. git-config(1)
@@ -23,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;
@@ -41,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;
@@ -61,7 +89,7 @@ sub new {
                $self->{-cfg} = {};
                my $f = $self->{'-f'} = "$dir/lei.saved-search";
                open my $fh, '>', $f or return $lei->fail("open $f: $!");
-               my $sq_dst = squote_maybe($dst);
+               my $sq_dst = PublicInbox::Config::squote_maybe($dst);
                my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
                if (ref $q) {
                        $q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
@@ -76,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: $!");
        }