]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiUp.pm
lei up: more error checking for config loading
[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 PublicInbox::LeiOverview;
10
11 sub lei_up {
12         my ($lei, $out) = @_;
13         $lei->{lse} = $lei->_lei_store(1)->search;
14         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 $f = $lss->{'-f'};
18         my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
19                                 return $lei->fail("lei.q unset in $f");
20         my $lse = $lei->{lse} // die 'BUG: {lse} missing';
21         if (ref($q)) {
22                 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
23         } else {
24                 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
25         }
26         my $o = $lei->{opt}->{output} = $lss->{-cfg}->{'lei.q.output'} //
27                 return $lei->fail("lei.q.output unset in $f");
28         ref($o) and return $lei->fail("multiple values of lei.q.output in $f");
29         for my $k (qw(only include exclude)) {
30                 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
31                 $lei->{opt}->{$k} = $v;
32         }
33         for my $k (qw(external local remote
34                         import-remote import-before threads)) {
35                 my $c = "lei.q.$k";
36                 my $v = $lss->{-cfg}->{$c} // next;
37                 ref($v) and return $lei->fail("multiple values of $c in $f");
38                 $lei->{opt}->{$k} = $v;
39         }
40         $lei->{lss} = $lss; # for LeiOverview->new
41         my $lxs = $lei->lxs_prepare or return;
42         $lei->ale->refresh_externals($lxs);
43         $lei->{opt}->{save} = -1;
44         $lei->_start_query;
45 }
46
47 sub _complete_up {
48         my ($lei, @argv) = @_;
49         my ($cur, $re) = $lei->complete_url_common(\@argv);
50         grep(/\A$re\Q$cur/, PublicInbox::LeiSavedSearch::list($lei));
51 }
52
53 1;