unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 7/9] lei up: support output destination as arg
Date: Fri, 16 Apr 2021 16:10:33 -0700	[thread overview]
Message-ID: <20210416231035.31807-8-e@80x24.org> (raw)
In-Reply-To: <20210416231035.31807-1-e@80x24.org>

Specifying a directory in ~/.local/share/lei/saved-searches/
is painful, so support (and start encouraging) the use of
the output.
---
 lib/PublicInbox/LeiSavedSearch.pm | 55 ++++++++++++++++++++++++-------
 lib/PublicInbox/LeiUp.pm          |  4 +--
 t/lei-q-save.t                    | 11 +++++++
 3 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm
index ebc63091..93b1b23a 100644
--- a/lib/PublicInbox/LeiSavedSearch.pm
+++ b/lib/PublicInbox/LeiSavedSearch.pm
@@ -13,24 +13,39 @@ use PublicInbox::Spawn qw(run_die);
 use PublicInbox::ContentHash qw(git_sha);
 use Digest::SHA qw(sha256_hex);
 
+sub lss_dir_for ($$) {
+	my ($lei, $dstref) = @_;
+	my @n;
+	if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
+		require PublicInbox::URIimap;
+		my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
+		$$dstref = $$uri;
+		@n = ($uri->mailbox);
+	} else { # basename
+		@n = ($$dstref =~ m{([\w\-\.]+)/*\z});
+		$$dstref = $lei->rel2abs($$dstref);
+	}
+	push @n, sha256_hex($$dstref);
+	$lei->share_path . '/saved-searches/' . join('-', @n);
+}
+
 sub new {
 	my ($cls, $lei, $dir) = @_;
-	my $self = bless { ale => $lei->ale, -cfg => {} }, $cls;
+	my $self = bless { ale => $lei->ale }, $cls;
 	if (defined $dir) { # updating existing saved search via "lei up"
-		my $f = $self->{'-f'} = "$dir/lei.saved-search";
-		-f $f && -r _ or
+		my $f = "$dir/lei.saved-search";
+		((-f $f && -r _) || output2lssdir($self, $lei, \$dir, \$f)) or
 			return $lei->fail("$f non-existent or unreadable");
-		$self->{-cfg} = PublicInbox::Config::git_config_dump($f);
+		$self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
+		$self->{'-f'} = $f;
 	} else { # new saved search "lei q --save"
-		my $saved_dir = $lei->share_path . '/saved-searches/';
-		my (@n) = ($lei->{ovv}->{dst} =~ m{([\w\-\.]+)/*\z});
-		my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
-		push @n, sha256_hex("$lei->{ovv}->{fmt}\0$lei->{ovv}->{dst}");
-
-		$dir = $saved_dir . join('-', @n);
+		my $dst = $lei->{ovv}->{dst};
+		$dir = lss_dir_for($lei, \$dst);
 		require File::Path;
 		File::Path::make_path($dir); # raises on error
+		$self->{-cfg} = {};
 		$self->{'-f'} = "$dir/lei.saved-search";
+		my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
 		if (ref $q) {
 			cfg_set($self, '--add', 'lei.q', $_) for @$q;
 		} else {
@@ -38,7 +53,8 @@ sub new {
 		}
 		my $fmt = $lei->{opt}->{'format'};
 		cfg_set($self, 'lei.q.format', $fmt) if defined $fmt;
-		cfg_set($self, 'lei.q.output', $lei->{opt}->{output});
+		$dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
+		cfg_set($self, 'lei.q.output', $dst);
 		for my $k (qw(only include exclude)) {
 			my $ary = $lei->{opt}->{$k} // next;
 			for my $x (@$ary) {
@@ -127,6 +143,23 @@ sub mm { undef }
 sub altid_map { {} }
 
 sub cloneurl { [] }
+
+# find existing directory containing a `lei.saved-search' file based on
+# $dir_ref which is an output
+sub output2lssdir {
+	my ($self, $lei, $dir_ref, $fn_ref) = @_;
+	my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
+	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);
+		$$dir_ref = $dir;
+		$$fn_ref = $f;
+		return 1;
+	}
+	undef;
+}
+
 no warnings 'once';
 *nntp_url = \&cloneurl;
 *base_url = \&PublicInbox::Inbox::base_url;
diff --git a/lib/PublicInbox/LeiUp.pm b/lib/PublicInbox/LeiUp.pm
index 386a7566..7ddb1dd0 100644
--- a/lib/PublicInbox/LeiUp.pm
+++ b/lib/PublicInbox/LeiUp.pm
@@ -9,9 +9,9 @@ use PublicInbox::LeiSavedSearch;
 use PublicInbox::LeiOverview;
 
 sub lei_up {
-	my ($lei, $dir) = @_;
+	my ($lei, $out) = @_;
 	$lei->{lse} = $lei->_lei_store(1)->search;
-	my $lss = PublicInbox::LeiSavedSearch->new($lei, $dir) or return;
+	my $lss = PublicInbox::LeiSavedSearch->new($lei, $out) or return;
 	my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
 	$mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
 	my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
diff --git a/t/lei-q-save.t b/t/lei-q-save.t
index 6cfac20b..d43f508b 100644
--- a/t/lei-q-save.t
+++ b/t/lei-q-save.t
@@ -7,6 +7,8 @@ my $doc1 = eml_load('t/plack-qp.eml');
 $doc1->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 5)}));
 my $doc2 = eml_load('t/utf8.eml');
 $doc2->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
+my $doc3 = eml_load('t/msg_iter-order.eml');
+$doc3->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
 
 test_lei(sub {
 	my $home = $ENV{HOME};
@@ -38,5 +40,14 @@ test_lei(sub {
 	$cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
 	is_deeply $cfg->{'lei.q'}, 'd:last.week..',
 		'q --stdin stores relative time';
+	my $size = -s "$home/mbcl2";
+	ok(defined($size) && $size > 0, 'results written');
+	lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
+	is(-s "$home/mbcl2", $size, 'size unchanged on noop up');
+
+	$in = $doc3->as_string;
+	lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
+	lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
+	ok(-s "$home/mbcl2" > $size, 'size increased after up');
 });
 done_testing;

  parent reply	other threads:[~2021-04-16 23:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-16 23:10 [PATCH 0/9] lei saved search usability improvements Eric Wong
2021-04-16 23:10 ` [PATCH 1/9] lei q: --save preserves relative time queries Eric Wong
2021-04-16 23:10 ` [PATCH 2/9] lei: expose share_path as a method Eric Wong
2021-04-16 23:10 ` [PATCH 3/9] lei: saved searches keyed only by path/URL and format Eric Wong
2021-04-16 23:10 ` [PATCH 4/9] lei_to_mail: cast to URIimap object early Eric Wong
2021-04-16 23:10 ` [PATCH 5/9] test_common: handle '-C' (chdir) spawn option properly Eric Wong
2021-04-17  2:24   ` [SQUASH] test_common: fix -C $DIR with TEST_RUN_MODE=0 Eric Wong
2021-04-17 10:00     ` [PATCH 10/9] lei_query: fix relative path handling on --stdin Eric Wong
2021-04-16 23:10 ` [PATCH 6/9] lei: fix rel2abs Eric Wong
2021-04-16 23:10 ` Eric Wong [this message]
2021-04-16 23:10 ` [PATCH 8/9] lei q --save: avoid lei.q.format Eric Wong
2021-04-16 23:10 ` [PATCH 9/9] lei q --save: clobber config file on repeats 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=20210416231035.31807-8-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.
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).