]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiUp.pm
lei up: support --all=local
[public-inbox.git] / lib / PublicInbox / LeiUp.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 up" - updates the result of "lei q --save"
5 package PublicInbox::LeiUp;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::LeiSavedSearch;
9 use parent qw(PublicInbox::IPC);
10
11 sub up1 ($$) {
12         my ($lei, $out) = @_;
13         my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
14         my $f = $lss->{'-f'};
15         my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
16         $mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
17         my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
18                                 return $lei->fail("lei.q unset in $f");
19         my $lse = $lei->{lse} // die 'BUG: {lse} missing';
20         if (ref($q)) {
21                 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
22         } else {
23                 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
24         }
25         my $o = $lei->{opt}->{output} = $lss->{-cfg}->{'lei.q.output'} //
26                 return $lei->fail("lei.q.output unset in $f");
27         ref($o) and return $lei->fail("multiple values of lei.q.output in $f");
28         for my $k (qw(only include exclude)) {
29                 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
30                 $lei->{opt}->{$k} = $v;
31         }
32         for my $k (qw(external local remote
33                         import-remote import-before threads)) {
34                 my $c = "lei.q.$k";
35                 my $v = $lss->{-cfg}->{$c} // next;
36                 ref($v) and return $lei->fail("multiple values of $c in $f");
37                 $lei->{opt}->{$k} = $v;
38         }
39         $lei->{lss} = $lss; # for LeiOverview->new
40         my $lxs = $lei->lxs_prepare or return;
41         $lei->ale->refresh_externals($lxs);
42         $lei->_start_query;
43 }
44
45 sub up1_redispatch {
46         my ($lei, $out, $op_p) = @_;
47         my $l = bless { %$lei }, ref($lei);
48         $l->{opt} = { %{$l->{opt}} };
49         delete $l->{sock};
50         $l->{''} = $op_p; # daemon only
51         eval {
52                 $l->qerr("# updating $out");
53                 up1($l, $out);
54                 $l->qerr("# $out done");
55         };
56         $l->err($@) if $@;
57 }
58
59 sub lei_up {
60         my ($lei, $out) = @_;
61         $lei->{lse} = $lei->_lei_store(1)->search;
62         my $opt = $lei->{opt};
63         $opt->{save} = -1;
64         if (defined $opt->{all}) {
65                 length($opt->{mua}//'') and return
66                         $lei->fail('--all and --mua= are incompatible');
67
68                 # supporting IMAP outputs is more involved due to
69                 # git-credential prompts.  TODO: add this in 1.8
70                 $opt->{all} eq 'local' or return
71                         $lei->fail('only --all=local works at the moment');
72                 my @all = PublicInbox::LeiSavedSearch::list($lei);
73                 my @local = grep(!m!\Aimaps?://!i, @all);
74                 $lei->_lei_store->write_prepare($lei); # share early
75                 if ($lei->{oneshot}) { # synchronous
76                         up1_redispatch($lei, $_) for @local;
77                 } else {
78                         # daemon mode, re-dispatch into our event loop w/o
79                         # creating an extra fork-level
80                         require PublicInbox::DS;
81                         require PublicInbox::PktOp;
82                         my ($op_c, $op_p) = PublicInbox::PktOp->pair;
83                         for my $o (@local) {
84                                 PublicInbox::DS::requeue(sub {
85                                         up1_redispatch($lei, $o, $op_p);
86                                 });
87                         }
88                         $lei->event_step_init;
89                         $op_c->{ops} = { '' => [$lei->can('dclose'), $lei] };
90                 }
91         } else {
92                 up1($lei, $out);
93         }
94 }
95
96 sub _complete_up {
97         my ($lei, @argv) = @_;
98         my ($cur, $re) = $lei->complete_url_common(\@argv);
99         grep(/\A$re\Q$cur/, PublicInbox::LeiSavedSearch::list($lei));
100 }
101
102 1;