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 parent qw(Exporter);
9 our @EXPORT = qw(lei_ls_external lei_add_external lei_forget_external);
10 use PublicInbox::Config;
13 my ($self, $cb, @arg) = @_;
14 my $cfg = $self->_lei_cfg(0);
16 for my $sec (grep(/\Aexternal\./, @{$cfg->{-section_order}})) {
17 my $loc = substr($sec, length('external.'));
18 $boost{$loc} = $cfg->{"$sec.boost"};
20 return \%boost if !wantarray && !$cb;
22 # highest boost first, but stable for alphabetic tie break
24 my @order = sort { $boost{$b} <=> $boost{$a} } sort keys %boost;
25 return @order if !$cb;
26 for my $loc (@order) {
27 $cb->(@arg, $loc, $boost{$loc});
29 @order; # scalar or array
33 my ($self, @argv) = @_;
34 my ($OFS, $ORS) = $self->{opt}->{z} ? ("\0", "\0\0") : (" ", "\n");
35 externals_each($self, sub {
36 my ($loc, $boost_val) = @_;
37 $self->out($loc, $OFS, 'boost=', $boost_val, $ORS);
41 sub ext_canonicalize {
43 if ($location !~ m!\Ahttps?://!) {
44 PublicInbox::Config::rel2abs_collapsed($location);
47 my $uri = URI->new($location)->canonical;
48 my $path = $uri->path . '/';
49 $path =~ tr!/!/!s; # squeeze redundant '/'
55 my %patmap = ('*' => '[^/]*?', '?' => '[^/]', '[' => '[', ']' => ']');
58 $glob =~ s!(.)!$patmap{$1} || "\Q$1"!ge;
63 my ($self, $loc, $exclude) = @_;
64 return (ext_canonicalize($loc)) if -e $loc;
67 my @cur = externals_each($self);
68 my $do_glob = !$self->{opt}->{globoff}; # glob by default
69 if ($do_glob && ($loc =~ /[\*\?]/s || $loc =~ /\[.*\]/s)) {
70 my $re = glob2pat($loc);
71 @m = grep(m!$re!, @cur);
72 return @m if scalar(@m);
73 } elsif (index($loc, '/') < 0) { # exact basename match:
74 @m = grep(m!/\Q$loc\E/?\z!, @cur);
75 return @m if scalar(@m) == 1;
76 } elsif ($exclude) { # URL, maybe:
77 my $canon = ext_canonicalize($loc);
78 @m = grep(m!\A\Q$canon\E\z!, @cur);
79 return @m if scalar(@m) == 1;
81 return (ext_canonicalize($loc));
83 if (scalar(@m) == 0) {
84 $self->fail("`$loc' is unknown");
86 $self->fail("`$loc' is ambiguous:\n", map { "\t$_\n" } @m);
91 sub lei_add_external {
92 my ($self, $location) = @_;
93 my $cfg = $self->_lei_cfg(1);
94 my $new_boost = $self->{opt}->{boost} // 0;
95 $location = ext_canonicalize($location);
96 if ($location !~ m!\Ahttps?://! && !-d $location) {
97 return $self->fail("$location not a directory");
99 my $key = "external.$location.boost";
100 my $cur_boost = $cfg->{$key};
101 return if defined($cur_boost) && $cur_boost == $new_boost; # idempotent
102 $self->lei_config($key, $new_boost);
103 $self->_lei_store(1)->done; # just create the store
106 sub lei_forget_external {
107 my ($self, @locations) = @_;
108 my $cfg = $self->_lei_cfg(1);
109 my $quiet = $self->{opt}->{quiet};
111 for my $loc (@locations) {
112 my (@unset, @not_found);
113 for my $l ($loc, ext_canonicalize($loc)) {
115 my $key = "external.$l.boost";
116 delete($cfg->{$key});
117 $self->_config('--unset', $key);
120 } elsif (($? >> 8) == 5) {
123 $self->err("# --unset $key error");
124 return $self->x_it($?);
129 $self->err("# $_ gone") for @unset;
130 } elsif (@not_found) {
131 $self->err("# $_ not found") for @not_found;
132 } # else { already exited
136 # shell completion helper called by lei__complete
137 sub _complete_forget_external {
138 my ($self, @argv) = @_;
139 my $cfg = $self->_lei_cfg(0);
141 # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
142 # Maybe there's a better way to go about this in
143 # contrib/completion/lei-completion.bash
147 if ($cur eq ':' && @x) {
151 while (@x > 2 && $x[0] !~ /\Ahttps?\z/ && $x[1] ne ':') {
154 if (@x >= 2) { # qw(https : hostname : 443) or qw(http :)
156 } else { # just filter out the flags and hope for the best
157 $re = join('', grep(!/^-/, @argv));
159 $re = quotemeta($re);
161 # FIXME: bash completion off "http:" or "https:" when the last
162 # character is a colon doesn't work properly even if we're
163 # returning "//$HTTP_HOST/$PATH_INFO/", not sure why, could
166 my $x = substr($_, length('external.'));
167 # only return the part specified on the CLI
168 if ($x =~ /\A$re(\Q$cur\E.*)/) {
169 # don't duplicate if already 100% completed
170 $cur eq $1 ? () : $1;
174 } grep(/\Aexternal\.$re\Q$cur/, @{$cfg->{-section_order}});