]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: add shortcut for retrieving elements
[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 use File::Path::Expand qw/expand_filename/;
7
8 # returns key-value pairs of config directives in a hash
9 sub new {
10         my ($class, $file) = @_;
11
12         local $ENV{GIT_CONFIG} = defined $file ? $file : default_file();
13
14         my @cfg = `git config -l`;
15         $? == 0 or die "git config -l failed: $?\n";
16         chomp @cfg;
17         my %rv = map { split(/=/, $_, 2) } @cfg;
18         bless \%rv, $class;
19 }
20
21 sub lookup {
22         my ($self, $recipient) = @_;
23         my $addr = lc($recipient);
24         my $pfx;
25
26         foreach my $k (keys %$self) {
27                 $k =~ /\A(publicinbox\.[A-Z0-9a-z-]+)\.address\z/ or next;
28                 (lc($self->{$k}) eq $addr) or next;
29                 $pfx = $1;
30                 last;
31         }
32
33         defined $pfx or return;
34
35         my %rv = map {
36                 $_ => $self->{"$pfx.$_"}
37         } (qw(mainrepo failrepo description address));
38         \%rv;
39 }
40
41 sub get {
42         my ($self, $listname, $key) = @_;
43
44         $self->{"publicinbox.$listname.$key"};
45 }
46
47 sub default_file {
48         my $f = $ENV{PI_CONFIG};
49         return $f if defined $f;
50         my $pi_dir = $ENV{PI_DIR} || expand_filename('~/.public-inbox/');
51         "$pi_dir/config";
52 }
53
54 1;