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 edit-search" edit a saved search following "lei q --save"
5 package PublicInbox::LeiEditSearch;
8 use PublicInbox::LeiSavedSearch;
9 use PublicInbox::LeiUp;
10 use parent qw(PublicInbox::LeiConfig);
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
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
29 return $self->cfg_do_edit(<<EOM) if $new_out eq '';
30 $self->{-f} needs lei.q.output
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;
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
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
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);
61 $lei->fail(<<EOM) if -e $dir_old && !rename($dir_old, $dir_new);
62 E: rename($dir_old, $dir_new) error: $!
68 my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
70 my $self = bless { lei => $lei, lss => $lss, -f => $f }, __PACKAGE__;
74 *_complete_edit_search = \&PublicInbox::LeiUp::_complete_up;