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;
6 use File::Path::Expand qw/expand_filename/;
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
11 my ($class, $file) = @_;
14 $file = default_file() unless defined($file);
15 my @cmd = (qw/git config/, "--file=$file", '-l');
16 my $cmd = join(' ', @cmd);
17 my $pid = open(my $fh, '-|', @cmd);
18 defined $pid or die "$cmd failed: $!\n";
20 foreach my $line (<$fh>) {
22 my ($k, $v) = split(/=/, $line, 2);
26 if (ref($cur) eq "ARRAY") {
29 $rv{$k} = [ $cur, $v ];
35 close $fh or die "failed to close ($cmd) pipe: $!\n";
36 $? and warn "$$ $cmd exited with: ($pid) $?\n";
41 my ($self, $recipient) = @_;
42 my $addr = lc($recipient);
45 foreach my $k (keys %$self) {
46 $k =~ /\A(publicinbox\.[A-Z0-9a-z-]+)\.address\z/ or next;
48 if (ref($v) eq "ARRAY") {
49 foreach my $alias (@$v) {
50 (lc($alias) eq $addr) or next;
55 (lc($v) eq $addr) or next;
61 defined $pfx or return;
64 foreach my $k (qw(mainrepo address)) {
65 my $v = $self->{"$pfx.$k"};
66 $rv{$k} = $v if defined $v;
69 $listname =~ s/\Apublicinbox\.//;
70 $rv{listname} = $listname;
72 $rv{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
77 my ($self, $listname, $key) = @_;
79 $self->{"publicinbox.$listname.$key"};
83 my $f = $ENV{PI_CONFIG};
84 return $f if defined $f;
85 my $pi_dir = $ENV{PI_DIR} || expand_filename('~/.public-inbox');