about summary refs log tree commit homepage
path: root/lib/PublicInbox/LeiEditSearch.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-09-19 12:50:33 +0000
committerEric Wong <e@80x24.org>2021-09-19 19:53:03 +0000
commit9d0f17840479508de4aaf76fe6c150e94a9f79c3 (patch)
tree9065f32b760acbb8e4285600e4be01ebe631476a /lib/PublicInbox/LeiEditSearch.pm
parent20c940a876728fe91892200dd874dd917cd677ac (diff)
downloadpublic-inbox-9d0f17840479508de4aaf76fe6c150e94a9f79c3.tar.gz
As with "lei edit-search", "lei config --edit" may
spawn an interactive editor which works best from
the terminal running script/lei.

So implement LeiConfig as a superclass of LeiEditSearch
so the two commands can share the same verification
hooks and retry logic.
Diffstat (limited to 'lib/PublicInbox/LeiEditSearch.pm')
-rw-r--r--lib/PublicInbox/LeiEditSearch.pm60
1 files changed, 20 insertions, 40 deletions
diff --git a/lib/PublicInbox/LeiEditSearch.pm b/lib/PublicInbox/LeiEditSearch.pm
index 47166ce7..bcf7c105 100644
--- a/lib/PublicInbox/LeiEditSearch.pm
+++ b/lib/PublicInbox/LeiEditSearch.pm
@@ -7,48 +7,32 @@ use strict;
 use v5.10.1;
 use PublicInbox::LeiSavedSearch;
 use PublicInbox::LeiUp;
+use parent qw(PublicInbox::LeiConfig);
 
-sub edit_begin {
-        my ($lss, $lei) = @_;
-        if (ref($lss->{-cfg}->{'lei.q.output'})) {
-                delete $lss->{-cfg}->{'lei.q.output'}; # invalid
-                $lei->pgr_err(<<EOM);
-$lss->{-f} has multiple values of lei.q.output
+sub cfg_edit_begin {
+        my ($self) = @_;
+        if (ref($self->{lss}->{-cfg}->{'lei.q.output'})) {
+                delete $self->{lss}->{-cfg}->{'lei.q.output'}; # invalid
+                $self->{lei}->pgr_err(<<EOM);
+$self->{lss}->{-f} has multiple values of lei.q.output
 please remove redundant ones
 EOM
         }
-        $lei->{-lss_for_edit} = $lss;
 }
 
-sub do_edit ($$;$) {
-        my ($lss, $lei, $reason) = @_;
-        $lei->pgr_err($reason) if defined $reason;
-        my @cmd = (qw(git config --edit -f), $lss->{'-f'});
-        $lei->qerr("# spawning @cmd");
-        edit_begin($lss, $lei);
-        # run in script/lei foreground
-        require PublicInbox::PktOp;
-        my ($op_c, $op_p) = PublicInbox::PktOp->pair;
-        # $op_p will EOF when $EDITOR is done
-        $op_c->{ops} = { '' => [\&op_edit_done, $lss, $lei] };
-        $lei->send_exec_cmd([ @$lei{qw(0 1 2)}, $op_p->{op_p} ], \@cmd, {});
-}
-
-sub _edit_done {
-        my ($lss, $lei) = @_;
-        my $cfg = $lss->can('cfg_dump')->($lei, $lss->{'-f'}) //
-                return do_edit($lss, $lei, <<EOM);
-$lss->{-f} is unparseable
-EOM
+sub cfg_verify {
+        my ($self, $cfg) = @_;
         my $new_out = $cfg->{'lei.q.output'} // '';
-        return do_edit($lss, $lei, <<EOM) if ref $new_out;
-$lss->{-f} has multiple values of lei.q.output
+        return $self->cfg_do_edit(<<EOM) if ref $new_out;
+$self->{-f} has multiple values of lei.q.output
 EOM
-        return do_edit($lss, $lei, <<EOM) if $new_out eq '';
-$lss->{-f} needs lei.q.output
+        return $self->cfg_do_edit(<<EOM) if $new_out eq '';
+$self->{-f} needs lei.q.output
 EOM
+        my $lss = $self->{lss};
         my $old_out = $lss->{-cfg}->{'lei.q.output'} // return;
         return if $old_out eq $new_out;
+        my $lei = $self->{lei};
         my $old_path = $old_out;
         my $new_path = $new_out;
         s!$PublicInbox::LeiSavedSearch::LOCAL_PFX!! for ($old_path, $new_path);
@@ -57,10 +41,10 @@ EOM
         return if $dir_new eq $dir_old;
 
         ($old_out =~ m!\Av2:!i || $new_out =~ m!\Av2:!) and
-                return do_edit($lss, $lei, <<EOM);
+                return $self->cfg_do_edit(<<EOM);
 conversions from/to v2 inboxes not supported at this time
 EOM
-        return do_edit($lss, $lei, <<EOM) if -e $dir_new;
+        return $self->cfg_do_edit(<<EOM) if -e $dir_new;
 lei.q.output changed from `$old_out' to `$new_out'
 However, $dir_new exists
 EOM
@@ -79,16 +63,12 @@ E: rename($dir_old, $dir_new) error: $!
 EOM
 }
 
-sub op_edit_done { # PktOp
-        my ($lss, $lei) = @_;
-        eval { _edit_done($lss, $lei) };
-        $lei->fail($@) if $@;
-}
-
 sub lei_edit_search {
         my ($lei, $out) = @_;
         my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
-        do_edit($lss, $lei);
+        my $f = $lss->{-f};
+        my $self = bless { lei => $lei, lss => $lss, -f => $f }, __PACKAGE__;
+        $self->cfg_do_edit;
 }
 
 *_complete_edit_search = \&PublicInbox::LeiUp::_complete_up;