]> Sergey Matveev's repositories - public-inbox.git/commitdiff
config: support multiple addresses for a inbox
authorEric Wong <e@80x24.org>
Fri, 11 Apr 2014 21:08:50 +0000 (21:08 +0000)
committerEric Wong <e@80x24.org>
Fri, 11 Apr 2014 22:24:30 +0000 (22:24 +0000)
This makes it possible to gradually migrate to new address in case
of list name changes, and is one step closer to operating in
"stealth hijack mode" :)

examples/public-inbox-config
lib/PublicInbox/Config.pm
t/config.t

index fd97600f7d0fbceb823611dd1bdb1a2dd0c51827..000cf2c35970a9743e75654f70a74ae697164bd7 100644 (file)
@@ -1,9 +1,11 @@
 # this usually in ~/.public-inbox/config and parseable with git-config(1)
 # update t/config.t if changing this, that test relies on this
 [publicinbox "test"]
+       address = try@public-inbox.org
+       address = sandbox@public-inbox.org
        address = test@public-inbox.org
        mainrepo = /home/pi/test-main.git
-       description = test repo, occasionally reset
+       description = test/sandbox area, occasionally reset
        url = http://example.com/test
 [publicinbox "bugs"]
        address = bugs@public-inbox.org
index 300dd88bf2bd5bd402339379ac47552ada3ec219..c8f2a8d82721330fae86f36ab86980061c3050fa 100644 (file)
@@ -6,6 +6,7 @@ use warnings;
 use File::Path::Expand qw/expand_filename/;
 
 # returns key-value pairs of config directives in a hash
+# if keys may be multi-value, the value is an array ref containing all values
 sub new {
        my ($class, $file) = @_;
 
@@ -14,7 +15,21 @@ sub new {
        my @cfg = `git config -l`;
        $? == 0 or die "git config -l failed: $?\n";
        chomp @cfg;
-       my %rv = map { split(/=/, $_, 2) } @cfg;
+       my %rv;
+       foreach my $line (@cfg) {
+               my ($k, $v) = split(/=/, $line, 2);
+               my $cur = $rv{$k};
+
+               if (defined $cur) {
+                       if (ref($cur) eq "ARRAY") {
+                               push @$cur, $v;
+                       } else {
+                               $rv{$k} = [ $cur, $v ];
+                       }
+               } else {
+                       $rv{$k} = $v;
+               }
+       }
        bless \%rv, $class;
 }
 
@@ -25,16 +40,27 @@ sub lookup {
 
        foreach my $k (keys %$self) {
                $k =~ /\A(publicinbox\.[A-Z0-9a-z-]+)\.address\z/ or next;
-               (lc($self->{$k}) eq $addr) or next;
-               $pfx = $1;
-               last;
+               my $v = $self->{$k};
+               if (ref($v) eq "ARRAY") {
+                       foreach my $alias (@$v) {
+                               (lc($alias) eq $addr) or next;
+                               $pfx = $1;
+                               last;
+                       }
+               } else {
+                       (lc($v) eq $addr) or next;
+                       $pfx = $1;
+                       last;
+               }
        }
 
        defined $pfx or return;
 
-       my %rv = map {
-               $_ => $self->{"$pfx.$_"}
-       } (qw(mainrepo description address));
+       my %rv;
+       foreach my $k (qw(mainrepo description address)) {
+               my $v = $self->{"$pfx.$k"};
+               $rv{$k} = $v if defined $v;
+       }
        my $listname = $pfx;
        $listname =~ s/\Apublicinbox\.//;
        $rv{listname} = $listname;
index 44e051f2f939caab3f3b040f369ffec70055e1a5..cd61fe091d2d607dfde12f2c286e4ef0b4becf75 100644 (file)
@@ -34,6 +34,16 @@ my $tmpdir = tempdir(CLEANUP => 1);
 
        is($cfg->lookup('blah@example.com'), undef,
                "non-existent lookup returns undef");
+
+       my $test = $cfg->lookup('test@public-inbox.org');
+       is_deeply($test, {
+               'address' => ['try@public-inbox.org',
+                             'sandbox@public-inbox.org',
+                             'test@public-inbox.org'],
+               'mainrepo' => '/home/pi/test-main.git',
+               'description' => 'test/sandbox area, occasionally reset',
+               'listname' => 'test',
+       }, "lookup matches expected output for test");
 }
 
 done_testing();