]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: allow taking an existing reference
[public-inbox.git] / lib / PublicInbox / Config.pm
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # Used throughout the project for reading configuration
5 package PublicInbox::Config;
6 use strict;
7 use warnings;
8 use base qw/Exporter/;
9 our @EXPORT_OK = qw/try_cat/;
10 require PublicInbox::Inbox;
11 use File::Path::Expand qw/expand_filename/;
12
13 # returns key-value pairs of config directives in a hash
14 # if keys may be multi-value, the value is an array ref containing all values
15 sub new {
16         my ($class, $file) = @_;
17         $file = default_file() unless defined($file);
18         $file = ref $file ? $file : git_config_dump($file);
19         my $self = bless $file, $class;
20
21         # caches
22         $self->{-by_addr} ||= {};
23         $self->{-by_name} ||= {};
24         $self;
25 }
26
27 sub lookup {
28         my ($self, $recipient) = @_;
29         my $addr = lc($recipient);
30         my $inbox = $self->{-by_addr}->{$addr};
31         return $inbox if $inbox;
32
33         my $pfx;
34
35         foreach my $k (keys %$self) {
36                 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
37                 my $v = $self->{$k};
38                 if (ref($v) eq "ARRAY") {
39                         foreach my $alias (@$v) {
40                                 (lc($alias) eq $addr) or next;
41                                 $pfx = $1;
42                                 last;
43                         }
44                 } else {
45                         (lc($v) eq $addr) or next;
46                         $pfx = $1;
47                         last;
48                 }
49         }
50         defined $pfx or return;
51         _fill($self, $pfx);
52 }
53
54 sub lookup_name {
55         my ($self, $name) = @_;
56         my $rv = $self->{-by_name}->{$name};
57         return $rv if $rv;
58         $self->{-by_name}->{$name} = _fill($self, "publicinbox.$name");
59 }
60
61 sub get {
62         my ($self, $inbox, $key) = @_;
63
64         $self->{"publicinbox.$inbox.$key"};
65 }
66
67 sub config_dir { $ENV{PI_DIR} || expand_filename('~/.public-inbox') }
68
69 sub default_file {
70         my $f = $ENV{PI_CONFIG};
71         return $f if defined $f;
72         config_dir() . '/config';
73 }
74
75 sub git_config_dump {
76         my ($file) = @_;
77         my ($in, $out);
78         my @cmd = (qw/git config/, "--file=$file", '-l');
79         my $cmd = join(' ', @cmd);
80         my $pid = open(my $fh, '-|', @cmd);
81         defined $pid or die "$cmd failed: $!";
82         my %rv;
83         foreach my $line (<$fh>) {
84                 chomp $line;
85                 my ($k, $v) = split(/=/, $line, 2);
86                 my $cur = $rv{$k};
87
88                 if (defined $cur) {
89                         if (ref($cur) eq "ARRAY") {
90                                 push @$cur, $v;
91                         } else {
92                                 $rv{$k} = [ $cur, $v ];
93                         }
94                 } else {
95                         $rv{$k} = $v;
96                 }
97         }
98         close $fh or die "failed to close ($cmd) pipe: $!";
99         $? and warn "$$ $cmd exited with: ($pid) $?";
100         \%rv;
101 }
102
103 sub try_cat {
104         my ($path) = @_;
105         my $rv;
106         if (open(my $fh, '<', $path)) {
107                 local $/;
108                 $rv = <$fh>;
109         }
110         $rv;
111 }
112
113 sub _fill {
114         my ($self, $pfx) = @_;
115         my $rv = {};
116
117         foreach my $k (qw(mainrepo address filter url)) {
118                 my $v = $self->{"$pfx.$k"};
119                 $rv->{$k} = $v if defined $v;
120         }
121         my $inbox = $pfx;
122         $inbox =~ s/\Apublicinbox\.//;
123         $rv->{name} = $inbox;
124         my $v = $rv->{address} ||= 'public-inbox@example.com';
125         $rv->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
126         $rv = PublicInbox::Inbox->new($rv);
127         if (ref($v) eq 'ARRAY') {
128                 $self->{-by_addr}->{lc($_)} = $rv foreach @$v;
129         } else {
130                 $self->{-by_addr}->{lc($v)} = $rv;
131         }
132         $rv;
133 }
134
135
136 1;