user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
5eef206c761c1fc54e98ce1b0e0ce2ecff7ba9ec blob 2235 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
debug log:

solving 5eef206c761c ...
found 5eef206c761c in https://80x24.org/public-inbox.git

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).