]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: use description file for gitweb
[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 # if keys may be multi-value, the value is an array ref containing all values
10 sub new {
11         my ($class, $file) = @_;
12
13         local $ENV{GIT_CONFIG} = defined $file ? $file : default_file();
14
15         my @cfg = `git config -l`;
16         $? == 0 or die "git config -l failed: $?\n";
17         chomp @cfg;
18         my %rv;
19         foreach my $line (@cfg) {
20                 my ($k, $v) = split(/=/, $line, 2);
21                 my $cur = $rv{$k};
22
23                 if (defined $cur) {
24                         if (ref($cur) eq "ARRAY") {
25                                 push @$cur, $v;
26                         } else {
27                                 $rv{$k} = [ $cur, $v ];
28                         }
29                 } else {
30                         $rv{$k} = $v;
31                 }
32         }
33         bless \%rv, $class;
34 }
35
36 sub lookup {
37         my ($self, $recipient) = @_;
38         my $addr = lc($recipient);
39         my $pfx;
40
41         foreach my $k (keys %$self) {
42                 $k =~ /\A(publicinbox\.[A-Z0-9a-z-]+)\.address\z/ or next;
43                 my $v = $self->{$k};
44                 if (ref($v) eq "ARRAY") {
45                         foreach my $alias (@$v) {
46                                 (lc($alias) eq $addr) or next;
47                                 $pfx = $1;
48                                 last;
49                         }
50                 } else {
51                         (lc($v) eq $addr) or next;
52                         $pfx = $1;
53                         last;
54                 }
55         }
56
57         defined $pfx or return;
58
59         my %rv;
60         foreach my $k (qw(mainrepo address)) {
61                 my $v = $self->{"$pfx.$k"};
62                 $rv{$k} = $v if defined $v;
63         }
64         my $listname = $pfx;
65         $listname =~ s/\Apublicinbox\.//;
66         $rv{listname} = $listname;
67         my $v = $rv{address};
68         $rv{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
69         \%rv;
70 }
71
72 sub get {
73         my ($self, $listname, $key) = @_;
74
75         $self->{"publicinbox.$listname.$key"};
76 }
77
78 sub default_file {
79         my $f = $ENV{PI_CONFIG};
80         return $f if defined $f;
81         my $pi_dir = $ENV{PI_DIR} || expand_filename('~/.public-inbox/');
82         "$pi_dir/config";
83 }
84
85 1;