]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei forget-search: support --prune=<local|remote>
authorEric Wong <e@80x24.org>
Fri, 22 Oct 2021 08:22:47 +0000 (08:22 +0000)
committerEric Wong <e@80x24.org>
Fri, 22 Oct 2021 19:07:39 +0000 (19:07 +0000)
Instead of:

lei forget-search $OUTPUT && rm -r $OUTPUT

we'll also allow a user to do:

rm -r $OUTPUT && lei forget-search --prune

This gives users flexibility to choose whatever flow
is most natural to them.

Documentation/lei-forget-search.pod
lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiForgetSearch.pm
lib/PublicInbox/LeiUp.pm
t/lei-q-save.t

index f3f043f93252340c7fd4fc18458c633385865202..3a7f493cee8e597a77c917474e2d6bb871b42c2d 100644 (file)
@@ -10,6 +10,19 @@ lei forget-search [OPTIONS] OUTPUT
 
 Forget a saved search at C<OUTPUT>.
 
 
 Forget a saved search at C<OUTPUT>.
 
+=head1 OPTIONS
+
+=over
+
+=item --prune[=<local|remote>]
+
+C<--prune> will forget saved searches if the C<OUTPUT> no longer
+exists.  C<--all=local> only prunes local mailboxes,
+C<--all=remote> only prunes remote mailboxes (currently
+C<imap://> and C<imaps://>).
+
+=back
+
 =head1 CONTACT
 
 Feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
 =head1 CONTACT
 
 Feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
index 43baeeb3d51c910f4bb1a61a7e3239dc994c1471..cb1e5433cc2dd07432af454970ba2427f0c8ed08 100644 (file)
@@ -222,8 +222,8 @@ our %CMD = ( # sorted in order of importance/use:
 
 'ls-search' => [ '[PREFIX]', 'list saved search queries',
                qw(format|f=s pretty l ascii z|0), @c_opt ],
 
 'ls-search' => [ '[PREFIX]', 'list saved search queries',
                qw(format|f=s pretty l ascii z|0), @c_opt ],
-'forget-search' => [ 'OUTPUT...', 'forget a saved search',
-               qw(verbose|v+), @c_opt ],
+'forget-search' => [ 'OUTPUT...|--prune', 'forget a saved search',
+               qw(verbose|v+ prune:s), @c_opt ],
 'edit-search' => [ 'OUTPUT', "edit saved search via `git config --edit'",
                        @c_opt ],
 'rm' => [ '--stdin|LOCATION...',
 'edit-search' => [ 'OUTPUT', "edit saved search via `git config --edit'",
                        @c_opt ],
 'rm' => [ '--stdin|LOCATION...',
index 0db9c75b8be3b2ae0c30d149d5c47b5c7c244098..f353fe52a95808cde176d113347c61db948afb61 100644 (file)
@@ -5,12 +5,12 @@
 package PublicInbox::LeiForgetSearch;
 use strict;
 use v5.10.1;
 package PublicInbox::LeiForgetSearch;
 use strict;
 use v5.10.1;
+use parent qw(PublicInbox::LeiUp);
 use PublicInbox::LeiSavedSearch;
 use PublicInbox::LeiSavedSearch;
-use PublicInbox::LeiUp;
 use File::Path ();
 use SelectSaver;
 
 use File::Path ();
 use SelectSaver;
 
-sub lei_forget_search {
+sub do_forget_search {
        my ($lei, @outs) = @_;
        my @dirs; # paths in ~/.local/share/lei/saved-search/
        my $cwd;
        my ($lei, @outs) = @_;
        my @dirs; # paths in ~/.local/share/lei/saved-search/
        my $cwd;
@@ -30,9 +30,55 @@ sub lei_forget_search {
                $save = SelectSaver->new($lei->{2});
        }
        File::Path::remove_tree(@dirs, $opt);
                $save = SelectSaver->new($lei->{2});
        }
        File::Path::remove_tree(@dirs, $opt);
-       $lei->fail if defined $cwd;
+       $lei->child_error(0) if defined $cwd;
+}
+
+sub lei_forget_search {
+       my ($lei, @outs) = @_;
+       my $prune = $lei->{opt}->{prune};
+       $prune // return do_forget_search($lei, @outs);
+       return $lei->fail("--prune and @outs incompatible") if @outs;
+       my @tmp = PublicInbox::LeiSavedSearch::list($lei);
+       my $self = bless { -mail_sync => 1 }, __PACKAGE__;
+       $self->filter_lss($lei, $prune) // return
+                       $lei->fail("only --prune=$prune not understood");
+       if ($self->{o_remote}) { # setup lei->{auth}
+               $self->prepare_inputs($lei, $self->{o_remote}) or return;
+       }
+       my $ops = {};
+       $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
+       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
+       $lei->{wq1} = $self;
+       net_merge_all_done($self) unless $lei->{auth};
+       $lei->wait_wq_events($op_c, $ops);
+}
+
+sub do_prune {
+       my ($self) = @_;
+       my $lei = $self->{lei};
+       for my $o (@{$self->{o_local} // []}) {
+               next if -e $o;
+               $lei->qerr("# pruning $o");
+               eval { do_forget_search($lei, $o) };
+               $lei->child_error(0, "E: $@") if $@;
+       }
+       for my $o (@{$self->{o_remote} // []}) {
+               my $uri = PublicInbox::URIimap->new($o);
+               next if $lei->{net}->mic_for_folder($uri);
+               $lei->qerr("# pruning $uri");
+               eval { do_forget_search($lei, $o) };
+               $lei->child_error(0, "E: $@") if $@;
+       }
+}
+
+# called in top-level lei-daemon when LeiAuth is done
+sub net_merge_all_done {
+       my ($self) = @_;
+       $self->wq_do('do_prune');
+       $self->wq_close;
 }
 
 }
 
+*_wq_done_wait = \&PublicInbox::LEI::wq_done_wait;
 *_complete_forget_search = \&PublicInbox::LeiUp::_complete_up;
 
 1;
 *_complete_forget_search = \&PublicInbox::LeiUp::_complete_up;
 
 1;
index dac0fc28788510d98894d05af6c213479202da07..79639d5e62a49fe8f2eb43bfd3f454483e619e4f 100644 (file)
@@ -93,6 +93,21 @@ sub redispatch_all ($$) {
        }
 }
 
        }
 }
 
+sub filter_lss {
+       my ($self, $lei, $all) = @_;
+       my @outs = PublicInbox::LeiSavedSearch::list($lei);
+       if ($all eq 'local') {
+               $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
+       } elsif ($all eq 'remote') {
+               $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
+       } elsif ($all eq '') {
+               $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
+               $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
+       } else {
+               undef;
+       }
+}
+
 sub lei_up {
        my ($lei, @outs) = @_;
        my $opt = $lei->{opt};
 sub lei_up {
        my ($lei, @outs) = @_;
        my $opt = $lei->{opt};
@@ -101,17 +116,8 @@ sub lei_up {
                return $lei->fail("--all and @outs incompatible") if @outs;
                defined($opt->{mua}) and return
                        $lei->fail('--all and --mua= are incompatible');
                return $lei->fail("--all and @outs incompatible") if @outs;
                defined($opt->{mua}) and return
                        $lei->fail('--all and --mua= are incompatible');
-               @outs = PublicInbox::LeiSavedSearch::list($lei);
-               if ($all eq 'local') {
-                       $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
-               } elsif ($all eq 'remote') {
-                       $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
-               } elsif ($all eq '') {
-                       $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
-                       $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
-               } else {
+               filter_lss($self, $lei, $all) // return
                        $lei->fail("only --all=$all not understood");
                        $lei->fail("only --all=$all not understood");
-               }
        } elsif ($lei->{lse}) { # redispatched
                scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
                return up1($lei, $outs[0]);
        } elsif ($lei->{lse}) { # redispatched
                scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
                return up1($lei, $outs[0]);
index 05d5d9f4436cd0bb5feeee68887641a19384b50d..cd35461ce1b7f1f880e1f307582a0832a72f4fb6 100644 (file)
@@ -4,6 +4,7 @@
 use strict; use v5.10.1; use PublicInbox::TestCommon;
 use PublicInbox::Smsg;
 use List::Util qw(sum);
 use strict; use v5.10.1; use PublicInbox::TestCommon;
 use PublicInbox::Smsg;
 use List::Util qw(sum);
+use File::Path qw(remove_tree);
 
 my $doc1 = eml_load('t/plack-qp.eml');
 $doc1->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 5)}));
 
 my $doc1 = eml_load('t/plack-qp.eml');
 $doc1->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 5)}));
@@ -233,5 +234,11 @@ test_lei(sub {
                and xbail "-ipe $lss[0]: $?";
        lei_ok qw(ls-search);
        is($lei_err, '', 'no errors w/ fixed config');
                and xbail "-ipe $lss[0]: $?";
        lei_ok qw(ls-search);
        is($lei_err, '', 'no errors w/ fixed config');
+
+       like($lei_out, qr!\Q$home/after\E!, "`after' in ls-search");
+       remove_tree("$home/after");
+       lei_ok qw(forget-search --prune);
+       lei_ok qw(ls-search);
+       unlike($lei_out, qr!\Q$home/after\E!, "`after' pruned");
 });
 done_testing;
 });
 done_testing;