]> Sergey Matveev's repositories - public-inbox.git/commitdiff
trivial config module for dumping config
authorEric Wong <normalperson@yhbt.net>
Thu, 6 Feb 2014 22:19:20 +0000 (22:19 +0000)
committerEric Wong <normalperson@yhbt.net>
Fri, 7 Feb 2014 03:05:06 +0000 (03:05 +0000)
We'll be using git config files after all...

lib/PublicInbox/Config.pm [new file with mode: 0644]
t/config.t [new file with mode: 0644]

diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
new file mode 100644 (file)
index 0000000..4078585
--- /dev/null
@@ -0,0 +1,18 @@
+# Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
+# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+package PublicInbox::Config;
+
+# returns key-value pairs of config directives in a hash
+sub dump {
+       my ($class, $file) = @_;
+
+       local $ENV{GIT_CONFIG} = $file;
+
+       my @cfg = `git config -l`;
+       $? == 0 or die "git config -l failed: $?\n";
+       chomp @cfg;
+       my %rv = map { split(/=/, $_, 2) } @cfg;
+       \%rv;
+}
+
+1;
diff --git a/t/config.t b/t/config.t
new file mode 100644 (file)
index 0000000..7dddd2e
--- /dev/null
@@ -0,0 +1,23 @@
+# Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
+# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+use strict;
+use warnings;
+use Test::More;
+use PublicInbox::Config;
+use File::Temp qw/tempdir/;
+my $tmpdir = tempdir(CLEANUP => 1);
+
+{
+       is(system(qw(git init --bare), $tmpdir), 0, "git init successful");
+       {
+               local $ENV{GIT_DIR} = $tmpdir;
+               is(system(qw(git config foo.bar hihi)), 0, "set config");
+       }
+
+       my $tmp = PublicInbox::Config->dump("$tmpdir/config");
+
+       is("hihi", $tmp->{"foo.bar"}, "config read correctly");
+       is("true", $tmp->{"core.bare"}, "used --bare repo");
+}
+
+done_testing();