]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiEditSearch.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / LeiEditSearch.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 edit-search" edit a saved search following "lei q --save"
5 package PublicInbox::LeiEditSearch;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::LeiSavedSearch;
9 use PublicInbox::LeiUp;
10 use parent qw(PublicInbox::LeiConfig);
11
12 sub cfg_edit_begin {
13         my ($self) = @_;
14         if (ref($self->{lss}->{-cfg}->{'lei.q.output'})) {
15                 delete $self->{lss}->{-cfg}->{'lei.q.output'}; # invalid
16                 $self->{lei}->pgr_err(<<EOM);
17 $self->{lss}->{-f} has multiple values of lei.q.output
18 please remove redundant ones
19 EOM
20         }
21 }
22
23 sub cfg_verify {
24         my ($self, $cfg) = @_;
25         my $new_out = $cfg->{'lei.q.output'} // '';
26         return $self->cfg_do_edit(<<EOM) if ref $new_out;
27 $self->{-f} has multiple values of lei.q.output
28 EOM
29         return $self->cfg_do_edit(<<EOM) if $new_out eq '';
30 $self->{-f} needs lei.q.output
31 EOM
32         my $lss = $self->{lss};
33         my $old_out = $lss->{-cfg}->{'lei.q.output'} // return;
34         return if $old_out eq $new_out;
35         my $lei = $self->{lei};
36         my $old_path = $old_out;
37         my $new_path = $new_out;
38         s!$PublicInbox::LeiSavedSearch::LOCAL_PFX!! for ($old_path, $new_path);
39         my $dir_old = $lss->can('lss_dir_for')->($lei, \$old_path, 1);
40         my $dir_new = $lss->can('lss_dir_for')->($lei, \$new_path);
41         return if $dir_new eq $dir_old;
42
43         ($old_out =~ m!\Av2:!i || $new_out =~ m!\Av2:!) and
44                 return $self->cfg_do_edit(<<EOM);
45 conversions from/to v2 inboxes not supported at this time
46 EOM
47         return $self->cfg_do_edit(<<EOM) if -e $dir_new;
48 lei.q.output changed from `$old_out' to `$new_out'
49 However, $dir_new exists
50 EOM
51         # start the conversion asynchronously
52         my $old_sq = PublicInbox::Config::squote_maybe($old_out);
53         my $new_sq = PublicInbox::Config::squote_maybe($new_out);
54         $lei->puts("lei.q.output changed from $old_sq to $new_sq");
55         $lei->qerr("# lei convert $old_sq -o $new_sq");
56         my $v = !$lei->{opt}->{quiet};
57         $lei->{opt} = { output => $new_out, verbose => $v };
58         require PublicInbox::LeiConvert;
59         PublicInbox::LeiConvert::lei_convert($lei, $old_out);
60
61         $lei->fail(<<EOM) if -e $dir_old && !rename($dir_old, $dir_new);
62 E: rename($dir_old, $dir_new) error: $!
63 EOM
64 }
65
66 sub lei_edit_search {
67         my ($lei, $out) = @_;
68         my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
69         my $f = $lss->{-f};
70         my $self = bless { lei => $lei, lss => $lss, -f => $f }, __PACKAGE__;
71         $self->cfg_do_edit;
72 }
73
74 *_complete_edit_search = \&PublicInbox::LeiUp::_complete_up;
75
76 1;