]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Config.pm
config: use popen_rd when spawning `git config'
[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 PublicInbox::Spawn qw(popen_rd);
12 use File::Path::Expand qw/expand_filename/;
13
14 # returns key-value pairs of config directives in a hash
15 # if keys may be multi-value, the value is an array ref containing all values
16 sub new {
17         my ($class, $file) = @_;
18         $file = default_file() unless defined($file);
19         $file = ref $file ? $file : git_config_dump($file);
20         my $self = bless $file, $class;
21
22         # caches
23         $self->{-by_addr} ||= {};
24         $self->{-by_name} ||= {};
25         $self;
26 }
27
28 sub lookup {
29         my ($self, $recipient) = @_;
30         my $addr = lc($recipient);
31         my $inbox = $self->{-by_addr}->{$addr};
32         return $inbox if $inbox;
33
34         my $pfx;
35
36         foreach my $k (keys %$self) {
37                 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
38                 my $v = $self->{$k};
39                 if (ref($v) eq "ARRAY") {
40                         foreach my $alias (@$v) {
41                                 (lc($alias) eq $addr) or next;
42                                 $pfx = $1;
43                                 last;
44                         }
45                 } else {
46                         (lc($v) eq $addr) or next;
47                         $pfx = $1;
48                         last;
49                 }
50         }
51         defined $pfx or return;
52         _fill($self, $pfx);
53 }
54
55 sub lookup_name {
56         my ($self, $name) = @_;
57         my $rv = $self->{-by_name}->{$name};
58         return $rv if $rv;
59         $self->{-by_name}->{$name} = _fill($self, "publicinbox.$name");
60 }
61
62 sub get {
63         my ($self, $inbox, $key) = @_;
64
65         $self->{"publicinbox.$inbox.$key"};
66 }
67
68 sub config_dir { $ENV{PI_DIR} || expand_filename('~/.public-inbox') }
69
70 sub default_file {
71         my $f = $ENV{PI_CONFIG};
72         return $f if defined $f;
73         config_dir() . '/config';
74 }
75
76 sub git_config_dump {
77         my ($file) = @_;
78         my ($in, $out);
79         my @cmd = (qw/git config/, "--file=$file", '-l');
80         my $cmd = join(' ', @cmd);
81         my $fh = popen_rd(\@cmd);
82         my %rv;
83         local $/ = "\n";
84         foreach my $line (<$fh>) {
85                 chomp $line;
86                 my ($k, $v) = split(/=/, $line, 2);
87                 my $cur = $rv{$k};
88
89                 if (defined $cur) {
90                         if (ref($cur) eq "ARRAY") {
91                                 push @$cur, $v;
92                         } else {
93                                 $rv{$k} = [ $cur, $v ];
94                         }
95                 } else {
96                         $rv{$k} = $v;
97                 }
98         }
99         close $fh or die "failed to close ($cmd) pipe: $?";
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;