user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 4/6] config: git_config_dump blesses
Date: Mon, 19 Apr 2021 08:52:13 +0000	[thread overview]
Message-ID: <20210419085215.14742-5-e@80x24.org> (raw)
In-Reply-To: <20210419085215.14742-1-e@80x24.org>

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         | 10 +++++-----
 lib/PublicInbox/LEI.pm            |  3 +--
 lib/PublicInbox/LeiMirror.pm      |  2 +-
 lib/PublicInbox/LeiSavedSearch.pm |  7 ++-----
 4 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index 603dad98..016f50ec 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -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 ($;$) {
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 56640be1..f641f0d9 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -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))
diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 89574d28..15adb71b 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -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)) {
diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm
index 19f4aa5f..d3a32d36 100644
--- a/lib/PublicInbox/LeiSavedSearch.pm
+++ b/lib/PublicInbox/LeiSavedSearch.pm
@@ -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;

  parent reply	other threads:[~2021-04-19  8:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-19  8:52 [PATCH 0/6] lei saved search improvements Eric Wong
2021-04-19  8:52 ` [PATCH 1/6] lei: support unlinked/missing saved searches Eric Wong
2021-04-19  8:52 ` [PATCH 2/6] lei q: implement import-before default for --save Eric Wong
2021-04-19  8:52 ` [PATCH 3/6] lei_saved_search: avoid needless var shadowing Eric Wong
2021-04-19  8:52 ` Eric Wong [this message]
2021-04-19  8:52 ` [PATCH 5/6] lei_saved_search: split "lei q --save" and "lei up" init paths Eric Wong
2021-04-19  8:52 ` [PATCH 6/6] lei q: --save and --augment may be combined Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210419085215.14742-5-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).