]> Sergey Matveev's repositories - public-inbox.git/commitdiff
config: git_config_dump blesses
authorEric Wong <e@80x24.org>
Mon, 19 Apr 2021 08:52:13 +0000 (08:52 +0000)
committerEric Wong <e@80x24.org>
Mon, 19 Apr 2021 20:15:58 +0000 (20:15 +0000)
I don't know if it's worth it to sub (or super)class
PublicInbox::Config into something more generic for
lei, but this change simplifies a good chunk of lei
code that reuses the public-inbox config parsing.

lib/PublicInbox/Config.pm
lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiMirror.pm
lib/PublicInbox/LeiSavedSearch.pm

index 603dad983657441a68694bb7cc2bb19156794232..016f50eca3a8428237f3bb0813e214199783c43d 100644 (file)
@@ -24,11 +24,11 @@ sub new {
        if (ref($file) eq 'SCALAR') { # used by some tests
                open my $fh, '<', $file or die;  # PerlIO::scalar
                $self = config_fh_parse($fh, "\n", '=');
+               bless $self, $class;
        } else {
-               $self = git_config_dump($file);
+               $self = git_config_dump($class, $file);
                $self->{'-f'} = $file;
        }
-       bless $self, $class;
        # caches
        $self->{-by_addr} = {};
        $self->{-by_list_id} = {};
@@ -158,13 +158,13 @@ sub config_fh_parse ($$$) {
 }
 
 sub git_config_dump {
-       my ($file) = @_;
-       return {} unless -e $file;
+       my ($class, $file) = @_;
+       return bless {}, $class unless -e $file;
        my $cmd = [ qw(git config -z -l --includes), "--file=$file" ];
        my $fh = popen_rd($cmd);
        my $rv = config_fh_parse($fh, "\0", "\n");
        close $fh or die "failed to close (@$cmd) pipe: $?";
-       $rv;
+       bless $rv, $class;
 }
 
 sub valid_foo_name ($;$) {
index 56640be12358cb0a3f01f68bf6aed20876c39ec1..f641f0d9be45af143287a8f11db40322b230edd9 100644 (file)
@@ -700,8 +700,7 @@ sub _lei_cfg ($;$) {
                $cur_st = pack('dd', $st[10], $st[7]);
                qerr($self, "# $f created") if $self->{cmd} ne 'config';
        }
-       my $cfg = PublicInbox::Config::git_config_dump($f);
-       bless $cfg, 'PublicInbox::Config';
+       my $cfg = PublicInbox::Config->git_config_dump($f);
        $cfg->{-st} = $cur_st;
        $cfg->{'-f'} = $f;
        if ($sto && File::Spec->canonpath($sto_dir // store_path($self))
index 89574d28f5b3f9f12ecc859c831290917183bb57..15adb71b5d4a4dcff30efd919cb261b829d32159 100644 (file)
@@ -110,7 +110,7 @@ sub _try_config {
        }
        return $lei->err("# @$cmd failed (non-fatal)") if $cerr;
        rename($f, $ce) or return $lei->err("link($f, $ce): $! (non-fatal)");
-       my $cfg = PublicInbox::Config::git_config_dump($f);
+       my $cfg = PublicInbox::Config->git_config_dump($f);
        my $ibx = $self->{ibx} = {};
        for my $sec (grep(/\Apublicinbox\./, @{$cfg->{-section_order}})) {
                for (qw(address newsgroup nntpmirror)) {
index 19f4aa5fd4cae4b8a37cf088398f40d9f9007d95..d3a32d36b8559b3ecfb41edb18d7e085e65df39a 100644 (file)
@@ -56,9 +56,8 @@ sub list {
                print $fh "\tpath = ", cquote_val($p), "\n";
        }
        close $fh or die "close $f: $!";
-       my $cfg = PublicInbox::Config::git_config_dump($f);
+       my $cfg = PublicInbox::Config->git_config_dump($f);
        unlink($f);
-       bless $cfg, 'PublicInbox::Config';
        my $out = $cfg->get_all('lei.q.output') or return ();
        map {;
                s!\A(?:maildir|mh|mbox.+|mmdf):!!i;
@@ -76,7 +75,6 @@ sub new {
                output2lssdir($self, $lei, \$dir, \$f) or
                        return $lei->fail("--save was not used with $dst cwd=".
                                                $lei->rel2abs('.'));
-               $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
                $self->{'-f'} = $f;
        } else { # new saved search "lei q --save"
                $dst = $lei->{ovv}->{dst};
@@ -113,7 +111,6 @@ EOM
                }
                close($fh) or return $lei->fail("close $f: $!");
        }
-       bless $self->{-cfg}, 'PublicInbox::Config';
        $self->{lock_path} = "$self->{-f}.flock";
        $self->{-ovf} = "$dir/over.sqlite3";
        $self;
@@ -198,7 +195,7 @@ sub output2lssdir {
        my $dir = lss_dir_for($lei, \$dst);
        my $f = "$dir/lei.saved-search";
        if (-f $f && -r _) {
-               $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
+               $self->{-cfg} = PublicInbox::Config->git_config_dump($f);
                $$dir_ref = $dir;
                $$fn_ref = $f;
                return 1;