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)
4 # Used throughout the project for reading configuration
5 package PublicInbox::Config;
9 our @EXPORT_OK = qw/try_cat/;
10 use File::Path::Expand qw/expand_filename/;
12 # returns key-value pairs of config directives in a hash
13 # if keys may be multi-value, the value is an array ref containing all values
15 my ($class, $file) = @_;
16 $file = default_file() unless defined($file);
17 bless git_config_dump($file), $class;
21 my ($self, $recipient) = @_;
22 my $addr = lc($recipient);
25 foreach my $k (keys %$self) {
26 $k =~ /\A(publicinbox\.[\w-]+)\.address\z/ or next;
28 if (ref($v) eq "ARRAY") {
29 foreach my $alias (@$v) {
30 (lc($alias) eq $addr) or next;
35 (lc($v) eq $addr) or next;
41 defined $pfx or return;
44 foreach my $k (qw(mainrepo address filter)) {
45 my $v = $self->{"$pfx.$k"};
46 $rv{$k} = $v if defined $v;
49 $listname =~ s/\Apublicinbox\.//;
50 $rv{listname} = $listname;
52 $rv{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
57 my ($self, $listname, $key) = @_;
59 $self->{"publicinbox.$listname.$key"};
62 sub config_dir { $ENV{PI_DIR} || expand_filename('~/.public-inbox') }
65 my $f = $ENV{PI_CONFIG};
66 return $f if defined $f;
67 config_dir() . '/config';
73 my @cmd = (qw/git config/, "--file=$file", '-l');
74 my $cmd = join(' ', @cmd);
75 my $pid = open(my $fh, '-|', @cmd);
76 defined $pid or die "$cmd failed: $!";
78 foreach my $line (<$fh>) {
80 my ($k, $v) = split(/=/, $line, 2);
84 if (ref($cur) eq "ARRAY") {
87 $rv{$k} = [ $cur, $v ];
93 close $fh or die "failed to close ($cmd) pipe: $!";
94 $? and warn "$$ $cmd exited with: ($pid) $?";
101 if (open(my $fh, '<', $path)) {