]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei_saved_search: split "lei q --save" and "lei up" init paths
authorEric Wong <e@80x24.org>
Mon, 19 Apr 2021 08:52:14 +0000 (08:52 +0000)
committerEric Wong <e@80x24.org>
Mon, 19 Apr 2021 20:15:59 +0000 (20:15 +0000)
They're more different than alike, and having two separate
methods seems less confusing to me.

lib/PublicInbox/LeiLsSearch.pm
lib/PublicInbox/LeiSavedSearch.pm
lib/PublicInbox/LeiUp.pm

index 2aa457c05707c60f1b503c8abb581023c3e5457a..9ac4870f8399f705cf3323abfcc5f4aac8c5f0ac 100644 (file)
@@ -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'),
index d3a32d36b8559b3ecfb41edb18d7e085e65df39a..948e4954343a59690fd2e28a8a00220869bf0bb2 100644 (file)
@@ -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 <<EOM;
+       my $dir = $dst;
+       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->{-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 <<EOM;
 ; to refresh with new results, run: lei up $sq_dst
 [lei]
-$q
+       $q
 [lei "q"]
        output = $dst
 EOM
-               for my $k (ARRAY_FIELDS) {
-                       my $ary = $lei->{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;
index e4cbc825cdb13a7d2dcc590dd5105f79cdb5a50f..23c5c606502d4a5c53499296ce4722cea65c553c 100644 (file)
@@ -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'} //