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 2/3] lei_mail_sync: args2folder: common folder lookup sub
Date: Tue, 25 May 2021 11:01:35 +0000	[thread overview]
Message-ID: <20210525110136.6223-3-e@80x24.org> (raw)
In-Reply-To: <20210525110136.6223-1-e@80x24.org>

This lets us have a more consistent UX for mapping
easily-typed command-line arguments to canonical
folder locations.
---
 lib/PublicInbox/LeiExportKw.pm | 34 ++++-----------------------
 lib/PublicInbox/LeiInspect.pm  | 21 +++++++----------
 lib/PublicInbox/LeiMailSync.pm | 43 ++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 42 deletions(-)

diff --git a/lib/PublicInbox/LeiExportKw.pm b/lib/PublicInbox/LeiExportKw.pm
index fabc01f8..92c3aa43 100644
--- a/lib/PublicInbox/LeiExportKw.pm
+++ b/lib/PublicInbox/LeiExportKw.pm
@@ -95,7 +95,6 @@ lei mail_sync uninitialized, see lei-import(1)
 EOM
 	my $opt = $lei->{opt};
 	my $all = $opt->{all};
-	my @all = $lms->folders;
 	if (defined $all) { # --all=<local|remote>
 		my %x = map { $_ => $_ } split(/,/, $all);
 		my @ok = grep(defined, delete(@x{qw(local remote), ''}));
@@ -107,6 +106,7 @@ EOM
 EOM
 		}
 		my (%seen, @inc);
+		my @all = $lms->folders;
 		for my $ok (@ok) {
 			if ($ok eq 'local') {
 				@inc = grep(!m!\A[a-z0-9\+]+://!i, @all);
@@ -125,35 +125,9 @@ EOM
 no --mail-sync folders known to lei
 EOM
 	} else {
-		my %all = map { $_ => 1 } @all;
-		my @no;
-		for (@folders) {
-			next if $all{$_}; # ok
-			if (-d "$_/new" && -d "$_/cur") {
-				my $d = 'maildir:'.$lei->abs_path($_);
-				push(@no, $_) unless $all{$d};
-				$_ = $d;
-			} elsif (m!\Aimaps?://!i) {
-				my $orig = $_;
-				my $res = $lms->match_imap_url($orig, $all);
-				if (ref $res) {
-					$_ = $$res;
-					$lei->qerr(<<EOM);
-# using `$res' instead of `$orig'
-EOM
-				} else {
-					$lei->err($res) if defined $res;
-					push @no, $orig;
-				}
-			} else {
-				push @no, $_;
-			}
-		}
-		my $no = join("\n\t", @no);
-		return $lei->fail(<<EOF) if @no;
-No sync information for: $no
-Run `lei ls-mail-sync' to display valid choices
-EOF
+		my $err = $lms->arg2folder($lei, \@folders);
+		$lei->qerr(@{$err->{qerr}}) if $err->{qerr};
+		return $lei->fail($err->{fail}) if $err->{fail};
 	}
 	my $self = bless { lse => $lse }, __PACKAGE__;
 	$lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
diff --git a/lib/PublicInbox/LeiInspect.pm b/lib/PublicInbox/LeiInspect.pm
index 7fd33289..46b9197f 100644
--- a/lib/PublicInbox/LeiInspect.pm
+++ b/lib/PublicInbox/LeiInspect.pm
@@ -29,19 +29,16 @@ sub inspect_sync_folder ($$) {
 	my $ent = {};
 	my $lse = $lei->{lse} or return $ent;
 	my $lms = $lse->lms or return $ent;
-	my @folders;
-	if ($folder =~ m!\Aimaps?://!i) {
-		@folders = map { $_->as_string } $lms->match_imap_url($folder);
-	} elsif ($folder =~ m!\A(maildir|mh):(.+)!i) {
-		my $type = lc $1;
-		$folders[0] = "$type:".$lei->abs_path($2);
-	} elsif (-d $folder) {
-		$folders[0] = 'maildir:'.$lei->abs_path($folder);
-	} else {
-		$lei->fail("$folder not understood");
+	my $folders = [ $folder ];
+	my $err = $lms->arg2folder($lei, $folders);
+	if ($err) {
+		if ($err->{fail}) {
+			$lei->qerr("# no folders match $folder (non-fatal)");
+			@$folders = ();
+		}
+		$lei->qerr(@{$err->{qerr}}) if $err->{qerr};
 	}
-	$lei->qerr("# no folders match $folder (non-fatal)") if !@folders;
-	for my $f (@folders) {
+	for my $f (@$folders) {
 		$ent->{$f} = $lms->location_stats($f); # may be undef
 	}
 	$ent
diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm
index 63076fa1..094cf1fd 100644
--- a/lib/PublicInbox/LeiMailSync.pm
+++ b/lib/PublicInbox/LeiMailSync.pm
@@ -302,4 +302,47 @@ sub match_imap_url {
 			"E: `$url' is ambiguous:\n\t".join("\n\t", @match)."\n";
 }
 
+# map CLI args to folder table entries, returns undef on failure
+sub arg2folder {
+	my ($self, $lei, $folders) = @_;
+	my @all = $self->folders;
+	my %all = map { $_ => 1 } @all;
+	my ($err, @no);
+	for (@$folders) {
+		next if $all{$_}; # ok
+		if (m!\A(maildir|mh):(.+)!i) {
+			my $type = lc $1;
+			my $d = "$type:".$lei->abs_path($2);
+			push(@no, $_) unless $all{$d};
+			$_ = $d;
+		} elsif (-d "$_/new" && -d "$_/cur") {
+			my $d = 'maildir:'.$lei->abs_path($_);
+			push(@no, $_) unless $all{$d};
+			$_ = $d;
+		} elsif (m!\Aimaps?://!i) {
+			my $orig = $_;
+			my $res = match_imap_url($self, $orig, \@all);
+			if (ref $res) {
+				$_ = $$res;
+				push(@{$err->{qerr}}, <<EOM);
+# using `$res' instead of `$orig'
+EOM
+			} else {
+				$lei->err($res) if defined $res;
+				push @no, $orig;
+			}
+		} else {
+			push @no, $_;
+		}
+	}
+	if (@no) {
+		my $no = join("\n\t", @no);
+		$err->{fail} = <<EOF;
+No sync information for: $no
+Run `lei ls-mail-sync' to display valid choices
+EOF
+	}
+	$err;
+}
+
 1;

  parent reply	other threads:[~2021-05-25 11:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-25 11:01 [PATCH 0/3] lei forget-mail-sync: drop sync information Eric Wong
2021-05-25 11:01 ` [PATCH 1/3] lei export-kw: use lei->abs_path instead of rel2abs Eric Wong
2021-05-25 11:01 ` Eric Wong [this message]
2021-05-25 11:01 ` [PATCH 3/3] lei forget-mail-sync: new command to drop sync information 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=20210525110136.6223-3-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).