1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # *-external commands of lei
5 package PublicInbox::LeiExternal;
8 use PublicInbox::Config;
11 my ($self, $cb, @arg) = @_;
12 my $cfg = $self->_lei_cfg(0);
14 for my $sec (grep(/\Aexternal\./, @{$cfg->{-section_order}})) {
15 my $loc = substr($sec, length('external.'));
16 $boost{$loc} = $cfg->{"$sec.boost"};
18 return \%boost if !wantarray && !$cb;
20 # highest boost first, but stable for alphabetic tie break
22 my @order = sort { $boost{$b} <=> $boost{$a} } sort keys %boost;
23 if (ref($cb) eq 'CODE') {
24 for my $loc (@order) {
25 $cb->(@arg, $loc, $boost{$loc});
27 } elsif (ref($cb) eq 'HASH') {
30 @order; # scalar or array
33 sub ext_canonicalize {
35 if ($location !~ m!\Ahttps?://!) {
36 PublicInbox::Config::rel2abs_collapsed($location);
39 my $uri = URI->new($location)->canonical;
40 my $path = $uri->path . '/';
41 $path =~ tr!/!/!s; # squeeze redundant '/'
47 # TODO: we will probably extract glob2re into a separate module for
48 # PublicInbox::Filter::Base and maybe other places
49 my %re_map = ( '*' => '[^/]*?', '?' => '[^/]',
50 '[' => '[', ']' => ']', ',' => ',' );
57 my $changes = ($re =~ s!(.)!
58 $re_map{$p eq '\\' ? '' : do {
59 if ($1 eq '[') { ++$in_bracket }
60 elsif ($1 eq ']') { --$in_bracket }
64 ($p eq '-' && $in_bracket) ? $p : (++$qm, "\Q$p")
66 # bashism (also supported by curl): {a,b,c} => (a|b|c)
67 $re =~ s/([^\\]*)\\\{([^,]*?,[^\\]*?)\\\}/
68 (my $in_braces = $2) =~ tr!,!|!;
71 ($changes - $qm) ? $re : undef;
74 # get canonicalized externals list matching $loc
75 # $is_exclude denotes it's for --exclude
76 # otherwise it's for --only/--include is assumed
78 my ($self, $loc, $is_exclude) = @_;
79 return (ext_canonicalize($loc)) if -e $loc;
81 my @cur = externals_each($self);
82 my $do_glob = !$self->{opt}->{globoff}; # glob by default
83 if ($do_glob && (my $re = glob2re($loc))) {
84 @m = grep(m!$re!, @cur);
85 return @m if scalar(@m);
86 } elsif (index($loc, '/') < 0) { # exact basename match:
87 @m = grep(m!/\Q$loc\E/?\z!, @cur);
88 return @m if scalar(@m) == 1;
89 } elsif ($is_exclude) { # URL, maybe:
90 my $canon = ext_canonicalize($loc);
91 @m = grep(m!\A\Q$canon\E\z!, @cur);
92 return @m if scalar(@m) == 1;
94 return (ext_canonicalize($loc));
96 if (scalar(@m) == 0) {
97 $self->fail("`$loc' is unknown");
99 $self->fail("`$loc' is ambiguous:\n", map { "\t$_\n" } @m);
104 # TODO: does this need JSON output?
105 sub lei_ls_external {
106 my ($self, $filter) = @_;
107 my $opt = $self->{opt};
108 my $do_glob = !$opt->{globoff}; # glob by default
109 my ($OFS, $ORS) = $opt->{z} ? ("\0", "\0\0") : (" ", "\n");
111 my $re = $do_glob ? glob2re($filter) : undef;
112 $re //= index($filter, '/') < 0 ?
113 qr!/\Q$filter\E/?\z! : # exact basename match
114 qr/\Q$filter\E/; # grep -F semantics
115 my @ext = externals_each($self, my $boost = {});
116 @ext = $opt->{'invert-match'} ? grep(!/$re/, @ext)
118 if ($opt->{'local'} && !$opt->{remote}) {
119 @ext = grep(!m!\A[a-z\+]+://!, @ext);
120 } elsif ($opt->{remote} && !$opt->{'local'}) {
121 @ext = grep(m!\A[a-z\+]+://!, @ext);
124 $self->out($loc, $OFS, 'boost=', $boost->{$loc}, $ORS);
128 sub add_external_finish {
129 my ($self, $location) = @_;
130 my $cfg = $self->_lei_cfg(1);
131 my $new_boost = $self->{opt}->{boost} // 0;
132 my $key = "external.$location.boost";
133 my $cur_boost = $cfg->{$key};
134 return if defined($cur_boost) && $cur_boost == $new_boost; # idempotent
135 $self->lei_config($key, $new_boost);
138 sub lei_add_external {
139 my ($self, $location) = @_;
140 $self->_lei_store(1)->write_prepare($self);
141 my $opt = $self->{opt};
142 my $mirror = $opt->{mirror} // do {
144 for my $sw ($self->index_opt, $self->curl_opt,
145 qw(c no-torsocks torsocks inbox-version)) {
146 my ($f) = (split(/|/, $sw, 2))[0];
147 next unless defined $opt->{$f};
148 $f = length($f) == 1 ? "-$f" : "--$f";
151 if (scalar(@fail) == 1) {
152 return $self->("@fail requires --mirror");
154 my $last = pop @fail;
155 my $fail = join(', ', @fail);
156 return $self->("@fail and $last require --mirror");
160 my $new_boost = $opt->{boost} // 0;
161 $location = ext_canonicalize($location);
162 if (defined($mirror) && -d $location) {
163 $self->fail(<<""); # TODO: did you mean "update-external?"
164 --mirror destination `$location' already exists
167 if ($location !~ m!\Ahttps?://! && !-d $location) {
168 $mirror // return $self->fail("$location not a directory");
169 $mirror = ext_canonicalize($mirror);
170 require PublicInbox::LeiMirror;
171 PublicInbox::LeiMirror->start($self, $mirror => $location);
173 add_external_finish($self, $location);
177 sub lei_forget_external {
178 my ($self, @locations) = @_;
179 my $cfg = $self->_lei_cfg(1);
180 my $quiet = $self->{opt}->{quiet};
182 for my $loc (@locations) {
183 my (@unset, @not_found);
184 for my $l ($loc, ext_canonicalize($loc)) {
186 my $key = "external.$l.boost";
187 delete($cfg->{$key});
188 $self->_config('--unset', $key);
191 } elsif (($? >> 8) == 5) {
194 $self->err("# --unset $key error");
195 return $self->x_it($?);
200 $self->err("# $_ gone") for @unset;
201 } elsif (@not_found) {
202 $self->err("# $_ not found") for @not_found;
203 } # else { already exited
207 sub _complete_url_common ($) {
209 # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
210 # Maybe there's a better way to go about this in
211 # contrib/completion/lei-completion.bash
213 my $cur = pop @$argv;
216 if ($cur eq ':' && @x) {
220 while (@x > 2 && $x[0] !~ /\Ahttps?\z/ && $x[1] ne ':') {
223 if (@x >= 2) { # qw(https : hostname : 443) or qw(http :)
225 } else { # just filter out the flags and hope for the best
226 $re = join('', grep(!/^-/, @$argv));
228 $re = quotemeta($re);
233 # shell completion helper called by lei__complete
234 sub _complete_forget_external {
235 my ($self, @argv) = @_;
236 my $cfg = $self->_lei_cfg(0);
237 my ($cur, $re) = _complete_url_common(\@argv);
238 # FIXME: bash completion off "http:" or "https:" when the last
239 # character is a colon doesn't work properly even if we're
240 # returning "//$HTTP_HOST/$PATH_INFO/", not sure why, could
243 my $x = substr($_, length('external.'));
244 # only return the part specified on the CLI
245 # don't duplicate if already 100% completed
246 $x =~ /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
247 } grep(/\Aexternal\.$re\Q$cur/, @{$cfg->{-section_order}});
250 sub _complete_add_external { # for bash, this relies on "compopt -o nospace"
251 my ($self, @argv) = @_;
252 my $cfg = $self->_lei_cfg(0);
253 my ($cur, $re) = _complete_url_common(\@argv);
256 my $u = URI->new(substr($_, length('external.')));
257 my ($base) = ($u->path =~ m!((?:/?.*)?/)[^/]+/?\z!);
260 $u =~ /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
261 } grep(m!\Aexternal\.https?://!, @{$cfg->{-section_order}});