From f9d6b4a0f5f66a084682aba229b44ef47afdfc35 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 19 Apr 2021 08:52:14 +0000 Subject: lei_saved_search: split "lei q --save" and "lei up" init paths They're more different than alike, and having two separate methods seems less confusing to me. --- lib/PublicInbox/LeiLsSearch.pm | 2 +- lib/PublicInbox/LeiSavedSearch.pm | 79 +++++++++++++++++++++------------------ lib/PublicInbox/LeiUp.pm | 2 +- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/lib/PublicInbox/LeiLsSearch.pm b/lib/PublicInbox/LeiLsSearch.pm index 2aa457c0..9ac4870f 100644 --- a/lib/PublicInbox/LeiLsSearch.pm +++ b/lib/PublicInbox/LeiLsSearch.pm @@ -29,7 +29,7 @@ sub do_ls_search_long { my @x = sort(grep(/\A\Q$pfx/, PublicInbox::LeiSavedSearch::list($lei))); while (my $x = shift @x) { $ORS = '' if !scalar(@x); - my $lss = PublicInbox::LeiSavedSearch->new($lei, $x) or next; + my $lss = PublicInbox::LeiSavedSearch->up($lei, $x) or next; my $cfg = $lss->{-cfg}; my $ent = { q => $cfg->get_all('lei.q'), diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm index d3a32d36..948e4954 100644 --- a/lib/PublicInbox/LeiSavedSearch.pm +++ b/lib/PublicInbox/LeiSavedSearch.pm @@ -65,52 +65,57 @@ sub list { } @$out } -sub new { +sub up { # updating existing saved search via "lei up" my ($cls, $lei, $dst) = @_; + my $f; my $self = bless { ale => $lei->ale }, $cls; - my $dir; - if (defined $dst) { # updating existing saved search via "lei up" - my $f; - $dir = $dst; - output2lssdir($self, $lei, \$dir, \$f) or - return $lei->fail("--save was not used with $dst cwd=". - $lei->rel2abs('.')); - $self->{'-f'} = $f; - } else { # new saved search "lei q --save" - $dst = $lei->{ovv}->{dst}; - $dir = lss_dir_for($lei, \$dst); - require File::Path; - File::Path::make_path($dir); # raises on error - $self->{-cfg} = {}; - my $f = $self->{'-f'} = "$dir/lei.saved-search"; - open my $fh, '>', $f or return $lei->fail("open $f: $!"); - my $sq_dst = PublicInbox::Config::squote_maybe($dst); - my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing'; - if (ref $q) { - $q = join("\n", map { "\tq = ".cquote_val($_) } @$q); - } else { - $q = "\tq = ".cquote_val($q); - } - $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i; - print $fh <fail("--save was not used with $dst cwd=". + $lei->rel2abs('.')); + $self->{-cfg} = PublicInbox::Config->git_config_dump($f); + $self->{-ovf} = "$dir/over.sqlite3"; + $self->{'-f'} = $f; + $self->{lock_path} = "$self->{-f}.flock"; + $self; +} + +sub new { # new saved search "lei q --save" + my ($cls, $lei) = @_; + my $self = bless { ale => $lei->ale }, $cls; + my $dst = $lei->{ovv}->{dst}; + my $dir = lss_dir_for($lei, \$dst); + require File::Path; + File::Path::make_path($dir); # raises on error + $self->{-cfg} = {}; + my $f = $self->{'-f'} = "$dir/lei.saved-search"; + open my $fh, '>', $f or return $lei->fail("open $f: $!"); + my $sq_dst = PublicInbox::Config::squote_maybe($dst); + my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing'; + if (ref $q) { + $q = join("\n", map { "\tq = ".cquote_val($_) } @$q); + } else { + $q = "\tq = ".cquote_val($q); + } + $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i; + print $fh <{opt}->{$k} // next; - for my $x (@$ary) { - print $fh "\t$k = ".cquote_val($x)."\n"; - } - } - for my $k (BOOL_FIELDS) { - my $val = $lei->{opt}->{$k} // next; - print $fh "\t$k = ".($val ? 1 : 0)."\n"; + for my $k (ARRAY_FIELDS) { + my $ary = $lei->{opt}->{$k} // next; + for my $x (@$ary) { + print $fh "\t$k = ".cquote_val($x)."\n"; } - close($fh) or return $lei->fail("close $f: $!"); } + for my $k (BOOL_FIELDS) { + my $val = $lei->{opt}->{$k} // next; + print $fh "\t$k = ".($val ? 1 : 0)."\n"; + } + close($fh) or return $lei->fail("close $f: $!"); $self->{lock_path} = "$self->{-f}.flock"; $self->{-ovf} = "$dir/over.sqlite3"; $self; diff --git a/lib/PublicInbox/LeiUp.pm b/lib/PublicInbox/LeiUp.pm index e4cbc825..23c5c606 100644 --- a/lib/PublicInbox/LeiUp.pm +++ b/lib/PublicInbox/LeiUp.pm @@ -11,7 +11,7 @@ use PublicInbox::LeiOverview; sub lei_up { my ($lei, $out) = @_; $lei->{lse} = $lei->_lei_store(1)->search; - my $lss = PublicInbox::LeiSavedSearch->new($lei, $out) or return; + my $lss = PublicInbox::LeiSavedSearch->up($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'} // -- cgit v1.2.3-24-ge0c7