user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/3] lei add-external: split into separate file
Date: Sat, 25 Sep 2021 08:49:44 +0000	[thread overview]
Message-ID: <20210925084945.15899-3-e@80x24.org> (raw)
In-Reply-To: <20210925084945.15899-1-e@80x24.org>

Also was written before we had auto-loading and rarely used.
---
 MANIFEST                          |  1 +
 lib/PublicInbox/LeiAddExternal.pm | 72 +++++++++++++++++++++++++++++++
 lib/PublicInbox/LeiExternal.pm    | 66 ----------------------------
 lib/PublicInbox/LeiMirror.pm      |  3 +-
 4 files changed, 75 insertions(+), 67 deletions(-)
 create mode 100644 lib/PublicInbox/LeiAddExternal.pm

diff --git a/MANIFEST b/MANIFEST
index a39afe24aa87..8db9cdc791bb 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -207,6 +207,7 @@ lib/PublicInbox/KQNotify.pm
 lib/PublicInbox/LEI.pm
 lib/PublicInbox/LI2Wrap.pm
 lib/PublicInbox/LeiALE.pm
+lib/PublicInbox/LeiAddExternal.pm
 lib/PublicInbox/LeiAddWatch.pm
 lib/PublicInbox/LeiAuth.pm
 lib/PublicInbox/LeiBlob.pm
diff --git a/lib/PublicInbox/LeiAddExternal.pm b/lib/PublicInbox/LeiAddExternal.pm
new file mode 100644
index 000000000000..5eef206c761c
--- /dev/null
+++ b/lib/PublicInbox/LeiAddExternal.pm
@@ -0,0 +1,72 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# "lei add-external" command
+package PublicInbox::LeiAddExternal;
+use strict;
+use v5.10.1;
+
+sub _finish_add_external {
+	my ($lei, $location) = @_;
+	my $new_boost = $lei->{opt}->{boost} // 0;
+	my $key = "external.$location.boost";
+	my $cur_boost = $lei->_lei_cfg(1)->{$key};
+	return if defined($cur_boost) && $cur_boost == $new_boost; # idempotent
+	$lei->_config($key, $new_boost);
+}
+
+sub lei_add_external {
+	my ($lei, $location) = @_;
+	my $mirror = $lei->{opt}->{mirror} // do {
+		my @fail;
+		for my $sw ($lei->index_opt, $lei->curl_opt,
+				qw(no-torsocks torsocks inbox-version)) {
+			my ($f) = (split(/|/, $sw, 2))[0];
+			next unless defined $lei->{opt}->{$f};
+			$f = length($f) == 1 ? "-$f" : "--$f";
+			push @fail, $f;
+		}
+		if (scalar(@fail) == 1) {
+			return $lei->("@fail requires --mirror");
+		} elsif (@fail) {
+			my $last = pop @fail;
+			my $fail = join(', ', @fail);
+			return $lei->("@fail and $last require --mirror");
+		}
+		undef;
+	};
+	$location = $lei->ext_canonicalize($location);
+	if (defined($mirror) && -d $location) {
+		$lei->fail(<<""); # TODO: did you mean "update-external?"
+--mirror destination `$location' already exists
+
+	} elsif (-d $location) {
+		index($location, "\n") >= 0 and
+			return $lei->fail("`\\n' not allowed in `$location'");
+	}
+	if ($location !~ m!\Ahttps?://! && !-d $location) {
+		$mirror // return $lei->fail("$location not a directory");
+		index($location, "\n") >= 0 and
+			return $lei->fail("`\\n' not allowed in `$location'");
+		$mirror = $lei->ext_canonicalize($mirror);
+		require PublicInbox::LeiMirror;
+		PublicInbox::LeiMirror->start($lei, $mirror => $location);
+	} else {
+		_finish_add_external($lei, $location);
+	}
+}
+
+sub _complete_add_external { # for bash, this relies on "compopt -o nospace"
+	my ($lei, @argv) = @_;
+	my $cfg = $lei->_lei_cfg or return ();
+	my $match_cb = $lei->complete_url_prepare(\@argv);
+	require URI;
+	map {
+		my $u = URI->new(substr($_, length('external.')));
+		my ($base) = ($u->path =~ m!((?:/?.*)?/)[^/]+/?\z!);
+		$u->path($base);
+		$match_cb->($u->as_string);
+	} grep(m!\Aexternal\.https?://!, @{$cfg->{-section_order}});
+}
+
+1;
diff --git a/lib/PublicInbox/LeiExternal.pm b/lib/PublicInbox/LeiExternal.pm
index 35a7d68a17b5..5a54cc19281e 100644
--- a/lib/PublicInbox/LeiExternal.pm
+++ b/lib/PublicInbox/LeiExternal.pm
@@ -132,59 +132,6 @@ sub lei_ls_external {
 	}
 }
 
-sub add_external_finish {
-	my ($self, $location) = @_;
-	my $cfg = $self->_lei_cfg(1);
-	my $new_boost = $self->{opt}->{boost} // 0;
-	my $key = "external.$location.boost";
-	my $cur_boost = $cfg->{$key};
-	return if defined($cur_boost) && $cur_boost == $new_boost; # idempotent
-	$self->_config($key, $new_boost);
-}
-
-sub lei_add_external {
-	my ($self, $location) = @_;
-	my $opt = $self->{opt};
-	my $mirror = $opt->{mirror} // do {
-		my @fail;
-		for my $sw ($self->index_opt, $self->curl_opt,
-				qw(no-torsocks torsocks inbox-version)) {
-			my ($f) = (split(/|/, $sw, 2))[0];
-			next unless defined $opt->{$f};
-			$f = length($f) == 1 ? "-$f" : "--$f";
-			push @fail, $f;
-		}
-		if (scalar(@fail) == 1) {
-			return $self->("@fail requires --mirror");
-		} elsif (@fail) {
-			my $last = pop @fail;
-			my $fail = join(', ', @fail);
-			return $self->("@fail and $last require --mirror");
-		}
-		undef;
-	};
-	my $new_boost = $opt->{boost} // 0;
-	$location = ext_canonicalize($location);
-	if (defined($mirror) && -d $location) {
-		$self->fail(<<""); # TODO: did you mean "update-external?"
---mirror destination `$location' already exists
-
-	} elsif (-d $location) {
-		index($location, "\n") >= 0 and
-			return $self->fail("`\\n' not allowed in `$location'");
-	}
-	if ($location !~ m!\Ahttps?://! && !-d $location) {
-		$mirror // return $self->fail("$location not a directory");
-		index($location, "\n") >= 0 and
-			return $self->fail("`\\n' not allowed in `$location'");
-		$mirror = ext_canonicalize($mirror);
-		require PublicInbox::LeiMirror;
-		PublicInbox::LeiMirror->start($self, $mirror => $location);
-	} else {
-		add_external_finish($self, $location);
-	}
-}
-
 # returns an anonymous sub which returns an array of potential results
 sub complete_url_prepare {
 	my $argv = $_[-1]; # $_[0] may be $lei
@@ -223,17 +170,4 @@ sub complete_url_prepare {
 	wantarray ? ($re, $cur, $match_cb) : $match_cb;
 }
 
-sub _complete_add_external { # for bash, this relies on "compopt -o nospace"
-	my ($self, @argv) = @_;
-	my $cfg = $self->_lei_cfg;
-	my $match_cb = complete_url_prepare(\@argv);
-	require URI;
-	map {
-		my $u = URI->new(substr($_, length('external.')));
-		my ($base) = ($u->path =~ m!((?:/?.*)?/)[^/]+/?\z!);
-		$u->path($base);
-		$match_cb->($u->as_string);
-	} grep(m!\Aexternal\.https?://!, @{$cfg->{-section_order}});
-}
-
 1;
diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 1ab5e0d898d2..5cfa6fea851c 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -23,7 +23,8 @@ sub _wq_done_wait { # dwaitpid callback (via wq_eof)
 		$lei->err("unlink($f): $!") unless $!{ENOENT};
 	} else {
 		if ($lei->{cmd} ne 'public-inbox-clone') {
-			$lei->add_external_finish($mrr->{dst});
+			$lei->lazy_cb('add-external', '_finish_'
+					)->($lei, $mrr->{dst});
 		}
 		$lei->qerr("# mirrored $mrr->{src} => $mrr->{dst}");
 	}

  parent reply	other threads:[~2021-09-25  8:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-25  8:49 [PATCH 0/3] lei *-external: split off into separate files Eric Wong
2021-09-25  8:49 ` [PATCH 1/3] lei forget-external: split into separate file Eric Wong
2021-09-25  8:49 ` Eric Wong [this message]
2021-09-25  8:49 ` [PATCH 3/3] lei ls-external: " Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210925084945.15899-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH 2/3] lei add-external: split into separate file' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).