user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 1/4] config: hoist out common functions
  2015-12-22  1:02  7% [PATCH 0/4] a bunch of cleanups for future expansion Eric Wong
@ 2015-12-22  1:02  7% ` Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2015-12-22  1:02 UTC (permalink / raw)
  To: meta

These will be reused elsewhere.
---
 lib/PublicInbox/Config.pm | 72 +++++++++++++++++++++++++++++------------------
 lib/PublicInbox/WWW.pm    | 13 +--------
 2 files changed, 46 insertions(+), 39 deletions(-)

diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index 353a1fb..844f666 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -5,38 +5,16 @@
 package PublicInbox::Config;
 use strict;
 use warnings;
+use base qw/Exporter/;
+our @EXPORT_OK = qw/try_cat/;
 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) = @_;
-	my ($in, $out);
-
 	$file = default_file() unless defined($file);
-	my @cmd = (qw/git config/, "--file=$file", '-l');
-	my $cmd = join(' ', @cmd);
-	my $pid = open(my $fh, '-|', @cmd);
-	defined $pid or die "$cmd failed: $!\n";
-	my %rv;
-	foreach my $line (<$fh>) {
-		chomp $line;
-		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;
-		}
-	}
-	close $fh or die "failed to close ($cmd) pipe: $!\n";
-	$? and warn "$$ $cmd exited with: ($pid) $?\n";
-	bless \%rv, $class;
+	bless git_config_dump($file), $class;
 }
 
 sub lookup {
@@ -81,11 +59,51 @@ sub get {
 	$self->{"publicinbox.$listname.$key"};
 }
 
+sub config_dir { $ENV{PI_DIR} || expand_filename('~/.public-inbox') }
+
 sub default_file {
 	my $f = $ENV{PI_CONFIG};
 	return $f if defined $f;
-	my $pi_dir = $ENV{PI_DIR} || expand_filename('~/.public-inbox');
-	"$pi_dir/config";
+	config_dir() . '/config';
+}
+
+sub git_config_dump {
+	my ($file) = @_;
+	my ($in, $out);
+	my @cmd = (qw/git config/, "--file=$file", '-l');
+	my $cmd = join(' ', @cmd);
+	my $pid = open(my $fh, '-|', @cmd);
+	defined $pid or die "$cmd failed: $!\n";
+	my %rv;
+	foreach my $line (<$fh>) {
+		chomp $line;
+		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;
+		}
+	}
+	close $fh or die "failed to close ($cmd) pipe: $!\n";
+	$? and warn "$$ $cmd exited with: ($pid) $?\n";
+	\%rv;
+}
+
+sub try_cat {
+	my ($path) = @_;
+	my $rv;
+	if (open(my $fh, '<', $path)) {
+		local $/;
+		$rv = <$fh>;
+		close $fh;
+	}
+	$rv;
 }
 
 1;
diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index d00dfe7..5cd3bc6 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -13,7 +13,7 @@ package PublicInbox::WWW;
 use 5.008;
 use strict;
 use warnings;
-use PublicInbox::Config;
+use PublicInbox::Config qw(try_cat);
 use URI::Escape qw(uri_escape_utf8 uri_unescape);
 use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
 use constant PI_URL => 'http://public-inbox.org/';
@@ -218,17 +218,6 @@ sub ctx_get {
 	$val;
 }
 
-sub try_cat {
-	my ($path) = @_;
-	my $rv;
-	if (open(my $fh, '<', $path)) {
-		local $/;
-		$rv = <$fh>;
-		close $fh;
-	}
-	$rv;
-}
-
 sub footer {
 	my ($ctx) = @_;
 	return '' unless $ctx;
-- 
EW


^ permalink raw reply related	[relevance 7%]

* [PATCH 0/4] a bunch of cleanups for future expansion
@ 2015-12-22  1:02  7% Eric Wong
  2015-12-22  1:02  7% ` [PATCH 1/4] config: hoist out common functions Eric Wong
  0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2015-12-22  1:02 UTC (permalink / raw)
  To: meta

Pushing 4 changes out which are generic enough for master.
We'll be expanding the scope of the project slightly :>

      config: hoist out common functions
      hval: move PRE constant for wrapping UGC here
      git: cat-file wrapper enhancements
      rename 'GitCatFile' package to 'Git'

 MANIFEST                      |   2 +-
 lib/PublicInbox/Config.pm     |  72 +++++++++++++--------
 lib/PublicInbox/ExtMsg.pm     |  19 ++----
 lib/PublicInbox/Feed.pm       |  27 ++++----
 lib/PublicInbox/Git.pm        | 142 ++++++++++++++++++++++++++++++++++++++++++
 lib/PublicInbox/GitCatFile.pm |  91 ---------------------------
 lib/PublicInbox/Hval.pm       |   5 ++
 lib/PublicInbox/Mbox.pm       |   4 +-
 lib/PublicInbox/NNTP.pm       |   2 +-
 lib/PublicInbox/NewsGroup.pm  |   4 +-
 lib/PublicInbox/SearchIdx.pm  |  18 ++----
 lib/PublicInbox/SearchView.pm |  11 ++--
 lib/PublicInbox/View.pm       |  20 +++---
 lib/PublicInbox/WWW.pm        |  29 ++-------
 t/git.fast-import-data        | 101 ++++++++++++++++++++++++++++++
 t/git.t                       | 134 +++++++++++++++++++++++++++++++++++++++
 16 files changed, 472 insertions(+), 209 deletions(-)


^ permalink raw reply	[relevance 7%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2015-12-22  1:02  7% [PATCH 0/4] a bunch of cleanups for future expansion Eric Wong
2015-12-22  1:02  7% ` [PATCH 1/4] config: hoist out common functions Eric Wong

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).