]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: revamp API and implement lookup
[public-inbox.git] / lib / PublicInbox / Config.pm
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::Config;
4 use strict;
5 use warnings;
6
7 # returns key-value pairs of config directives in a hash
8 sub new {
9         my ($class, $file) = @_;
10
11         local $ENV{GIT_CONFIG} = $file;
12
13         my @cfg = `git config -l`;
14         $? == 0 or die "git config -l failed: $?\n";
15         chomp @cfg;
16         my %rv = map { split(/=/, $_, 2) } @cfg;
17         bless \%rv, $class;
18 }
19
20 sub lookup {
21         my ($self, $recipient) = @_;
22         my $addr = lc($recipient);
23         my $pfx;
24
25         foreach my $k (keys %$self) {
26                 $k =~ /\A(publicinbox\.[A-Z0-9a-z-]+)\.address\z/ or next;
27                 (lc($self->{$k}) eq $addr) or next;
28                 $pfx = $1;
29                 last;
30         }
31
32         defined $pfx or return;
33
34         my %rv = map {
35                 $_ => $self->{"$pfx.$_"}
36         } (qw(mainrepo failrepo description address));
37         \%rv;
38 }
39
40 1;