1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # "lei up" - updates the result of "lei q --save"
5 package PublicInbox::LeiUp;
8 use PublicInbox::LeiSavedSearch;
9 use parent qw(PublicInbox::IPC);
13 my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
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';
21 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
23 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
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 if (defined(my $dd = $lss->{-cfg}->{'lei.q.dedupe'})) {
29 $lss->translate_dedupe($lei, $dd) or return;
30 $lei->{opt}->{dedupe} = $dd;
32 for my $k (qw(only include exclude)) {
33 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
34 $lei->{opt}->{$k} = $v;
36 for my $k (qw(external local remote
37 import-remote import-before threads)) {
39 my $v = $lss->{-cfg}->{$c} // next;
40 ref($v) and return $lei->fail("multiple values of $c in $f");
41 $lei->{opt}->{$k} = $v;
43 $lei->{lss} = $lss; # for LeiOverview->new
44 my $lxs = $lei->lxs_prepare or return;
45 $lei->ale->refresh_externals($lxs, $lei);
50 my ($lei, $out, $op_p) = @_;
51 require PublicInbox::LeiFinmsg;
52 $lei->{fmsg} //= PublicInbox::LeiFinmsg->new($lei->{2});
53 my $l = bless { %$lei }, ref($lei);
54 $l->{opt} = { %{$l->{opt}} };
56 $l->{''} = $op_p; # daemon only
58 $l->qerr("# updating $out");
60 $l->qerr("# $out done");
62 $l->child_error(1 << 8, $@) if $@;
66 my ($lei, @outs) = @_;
67 $lei->{lse} = $lei->_lei_store(1)->search;
68 my $opt = $lei->{opt};
70 if (defined $opt->{all}) {
71 return $lei->fail("--all and @outs incompatible") if @outs;
72 length($opt->{mua}//'') and return
73 $lei->fail('--all and --mua= are incompatible');
75 # supporting IMAP outputs is more involved due to
76 # git-credential prompts. TODO: add this in 1.8
77 $opt->{all} eq 'local' or return
78 $lei->fail('only --all=local works at the moment');
79 my @all = PublicInbox::LeiSavedSearch::list($lei);
80 @local = grep(!m!\Aimaps?://!i, @all);
84 if (scalar(@outs) > 1) {
85 length($opt->{mua}//'') and return $lei->fail(<<EOM);
86 multiple outputs and --mua= are incompatible
89 return $lei->fail(<<EOM) if grep(m!\Aimaps?://!i, @outs);
90 multiple destinations only supported for local outputs (FIXME)
93 if (scalar(@local) > 1) {
94 $lei->_lei_store->write_prepare($lei); # share early
95 # daemon mode, re-dispatch into our event loop w/o
96 # creating an extra fork-level
97 require PublicInbox::DS;
98 require PublicInbox::PktOp;
99 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
101 PublicInbox::DS::requeue(sub {
102 up1_redispatch($lei, $o, $op_p);
105 $lei->event_step_init;
106 $op_c->{ops} = { '' => [$lei->can('dclose'), $lei] };
108 up1($lei, $local[0]);
113 my ($lei, @argv) = @_;
114 my $match_cb = $lei->complete_url_prepare(\@argv);
115 map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);