Skip to content

Commit 7fbe8c2

Browse files
committed
Add some hint when using invalid search
1 parent f58c78d commit 7fbe8c2

File tree

4 files changed

+96
-31
lines changed

4 files changed

+96
-31
lines changed

src/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ grepcpan:
7575
git: '/home/atoomic/bin/git'
7676
cache:
7777
directory: '~APPDIR~/var/tmp'
78-
version: '2.10'
78+
version: '2.11'
7979

src/lib/GrepCpan/Grep.pm

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use File::Path ();
2929
use File::Slurp ();
3030
use IO::Handle ();
3131
use Fcntl qw(:flock SEEK_END);
32-
use Test::More;
3332

3433
use FindBin;
3534
use utf8;
@@ -264,6 +263,7 @@ sub do_search ( $self, %opts ) {
264263
is_incomplete => $cache->{is_incomplete} || 0,
265264
search_in_progress => $cache->{search_in_progress} || 0,
266265
match => $cache->{match},
266+
adjusted_request => $cache->{adjusted_request} // {},
267267
results => $output,
268268
time_elapsed => $elapsed,
269269
is_a_known_distro => $is_a_known_distro,
@@ -585,6 +585,23 @@ sub _load_cache ( $self, $cache_file ) {
585585
return Sereal::read_sereal($cache_file);
586586
}
587587

588+
sub _parse_and_check_query_filetype ( $self, $query_filetype, $adjusted_request={} ) {
589+
590+
my $rules = $self->_parse_query_filetype($query_filetype);
591+
592+
my $r = $rules // [];
593+
my $value = join( ',', @$r );
594+
$query_filetype =~ s{\s+}{}g;
595+
if ( $query_filetype ne $value ) {
596+
$adjusted_request->{'qft'} = {
597+
error => "Incorrect search filter: invalid characters - $query_filetype",
598+
value => $value,
599+
}
600+
}
601+
602+
return $rules;
603+
}
604+
588605
sub _parse_query_filetype ( $self, $query_filetype ) {
589606

590607
return unless defined $query_filetype;
@@ -600,6 +617,23 @@ sub _parse_query_filetype ( $self, $query_filetype ) {
600617
return \@filetypes;
601618
}
602619

620+
sub _parse_and_check_ignore_files ( $self, $ignore_files, $adjusted_request={} ) {
621+
622+
return unless length $ignore_files;
623+
624+
my $rules = $self->_parse_ignore_files($ignore_files);
625+
626+
if ( ! $rules ) {
627+
$adjusted_request->{'qifl'} = {
628+
error => "Incorrect ignore files: invalid characters.",
629+
value => $ignore_files, # not updated
630+
}
631+
}
632+
633+
return $rules;
634+
}
635+
636+
603637
# convert a string of patterns (file to exclude) to a list of git rules to ignore the path
604638
# t/*, *.md, *.json, *.yaml, *.yml, *.conf, cpanfile, LICENSE, MANIFEST, INSTALL, Changes, Makefile.PL, Build.PL, Copying, *.SKIP, *.ini, README
605639
sub _parse_ignore_files ( $self, $ignore_files ) {
@@ -652,6 +686,8 @@ sub _get_match_cache(
652686
return $load if $load;
653687
}
654688

689+
my $adjusted_request = {};
690+
655691
$search_distro =~ s{::+}{-}g if defined $search_distro;
656692

657693
# the distro can either come from url or the query with some glob
@@ -668,7 +704,7 @@ sub _get_match_cache(
668704
}
669705

670706
# filter on some type files distro + query filetype
671-
if ( my $rules = $self->_parse_query_filetype($query_filetype) ) {
707+
if ( my $rules = $self->_parse_and_check_query_filetype($query_filetype, $adjusted_request) ) {
672708
my $base_search = $git_cmd[-1];
673709
my $is_first_rule = 1;
674710
foreach my $rule (@$rules) {
@@ -685,7 +721,7 @@ sub _get_match_cache(
685721
}
686722
}
687723

688-
if ( my $rules = $self->_parse_ignore_files($ignore_files) ) {
724+
if ( my $rules = $self->_parse_and_check_ignore_files($ignore_files, $adjusted_request) ) {
689725
push @git_cmd, $rules->@*;
690726
}
691727

@@ -710,8 +746,8 @@ sub _get_match_cache(
710746
# remove the final marker if there
711747
my $search_in_progress = 1;
712748

713-
#note "LAST LINE .... " . $list_files->[-1];
714-
#note " check ? ", $list_files->[-1] eq END_OF_FILE_MARKER() ? 1 : 0;
749+
#say "LAST LINE .... " . $list_files->[-1];
750+
#say " check ? ", $list_files->[-1] eq END_OF_FILE_MARKER() ? 1 : 0;
715751
if ( scalar @$list_files && $list_files->[-1] eq END_OF_FILE_MARKER() ) {
716752
pop @$list_files;
717753
$search_in_progress = 0;
@@ -744,10 +780,11 @@ sub _get_match_cache(
744780
files => $match_files,
745781
distros => scalar keys $cache->{distros}->%*,
746782
};
783+
$cache->{adjusted_request} = $adjusted_request;
747784

748785
if ( !$search_in_progress ) {
749786

750-
#note "Search in progress..... done caching yaml file";
787+
#say "Search in progress..... done caching yaml file";
751788
$self->_save_cache( $request_cache_file, $cache );
752789
$self->_save_cache( $cache_file, $cache );
753790
unlink $raw_cache_file if -e $raw_cache_file;
@@ -794,7 +831,7 @@ sub run_git_cmd_limit ( $self, %opts ) {
794831
}
795832
else {
796833
# return the content of our current cache from previous run
797-
#note "use our cache from previous run";
834+
#say "use our cache from previous run";
798835
my @from_cache = File::Slurp::read_file($cache_file);
799836
chomp @from_cache;
800837
return \@from_cache;
@@ -829,7 +866,7 @@ sub run_git_cmd_limit ( $self, %opts ) {
829866
push @lines, $line;
830867
last if ++$c > $limit;
831868

832-
#note "GOT: $line ", $line eq END_OF_FILE_MARKER() ? 1 : 0;
869+
#say "GOT: $line ", $line eq END_OF_FILE_MARKER() ? 1 : 0;
833870
last if $line eq END_OF_FILE_MARKER();
834871
}
835872
alarm(0);
@@ -892,8 +929,8 @@ sub run_git_cmd_limit ( $self, %opts ) {
892929
exit 42;
893930
}
894931

895-
note "Running in kid command: " . join( ' ', 'git', @$cmd );
896-
note "KID is caching to file ", $cache_file;
932+
say "Running in kid command: " . join( ' ', 'git', @$cmd );
933+
say "KID is caching to file ", $cache_file;
897934

898935
my $to_cache;
899936

@@ -921,7 +958,7 @@ sub run_git_cmd_limit ( $self, %opts ) {
921958
qq{\n}; # in case of the last line did not had a newline
922959
print {$to_cache} END_OF_FILE_MARKER() . qq{\n} if $cache_file;
923960
print {$CW} END_OF_FILE_MARKER() . qq{\n} if $can_write_to_pipe;
924-
note "-- Request finished by kid: $counter lines - "
961+
say "-- Request finished by kid: $counter lines - "
925962
. join( ' ', 'git', @$cmd );
926963
exit $?;
927964
}

src/lib/grepcpan.pm

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,44 +58,63 @@ get '/source-code' => sub {
5858
};
5959

6060
get '/search' => sub {
61-
my $q = param('q'); # FIXME clean query
62-
my $filetype = param('qft');
63-
my $qdistro = param('qd');
61+
my %i = ( # input
62+
q => param('q'), # search query
63+
qft => param('qft'), # filetype
64+
qd => param('qd'), # distro
65+
qls => param('qls'), # only list files
66+
qifl => param('qifl'), # ignore files
67+
);
68+
6469
my $qci = param('qci'); # case insensitive
65-
my $qls = param('qls'); # only list files
6670
my $page = param('p') || 1;
6771
my $file = param('f');
68-
my $ignore_files = param('qifl');
72+
6973
my $query = $grep->do_search(
70-
search => $q,
74+
search => $i{'q'},
7175
page => $page - 1,
72-
search_distro => $qdistro, # filter on a distribution
76+
search_distro => $i{'qd'}, # filter on a distribution
7377
search_file => $file,
74-
filetype => $filetype,
78+
filetype => $i{'qft'},
7579
caseinsensitive => $qci,
76-
list_files => $qls, # not used for now, only impact the view
77-
ignore_files => $ignore_files
80+
list_files => $i{'qls'}, # not used for now, only impact the view
81+
ignore_files => $i{'qifl'},
7882
);
7983

8084
my $nopagination = defined $file && length $file ? 1 : 0;
8185
my $show_sumup = !$query->{is_a_known_distro}
8286
; #defined $distro && length $distro ? 0 : 1;
8387

84-
my $template = $qls ? 'list-files' : 'search';
88+
my $template = $i{'qls'} ? 'list-files' : 'search';
89+
90+
my $alerts = {};
91+
92+
# check if some of the input parameters are invalid and updated
93+
if ( my $adjustments = $query->{adjusted_request} ) {
94+
foreach my $key ( keys $adjustments->%* ) {
95+
my $adjustment = $adjustments->{$key} // {};
96+
if ( $adjustment->{error} ) {
97+
$alerts->{danger} //= '';
98+
$alerts->{danger} .= $adjustment->{error};
99+
}
100+
$i{$key} = $adjustment->{value} if defined $adjustment->{value};
101+
}
102+
}
85103

86104
return template $template => {
87-
search => $q,
88-
search_distro => $qdistro,
105+
search => $i{'q'},
106+
search_distro => $i{'qd'},
89107
query => $query,
90108
page => $page,
91-
last_searches => _update_history_cookie($q),
109+
last_searches => _update_history_cookie($i{'q'}),
92110
nopagination => $nopagination,
93111
show_sumup => $show_sumup,
94-
qft => $filetype // q{},
95-
qd => $qdistro, #$qdistro // q{},
96-
qls => $qls,
112+
qft => $i{'qft'} // '',
113+
qd => $i{'qd'} // '',
114+
qls => $i{'qls'},
97115
qci => $qci,
98-
qifl => $ignore_files,
116+
qifl => $i{'qifl'},
117+
alert => $alerts // {},
99118
};
100119
};
101120

src/views/_display.tt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<% IF query.time_elapsed %>
1+
<% IF query.time_elapsed %>
22
<%
33
SET gram_sec = 'second';# : 'second';
44
SET gram_sec = 'seconds' IF query.time_elapsed >= 2;
@@ -55,6 +55,15 @@
5555
</div>
5656
</form>
5757

58+
<% IF alert.danger %>
59+
<div align="center" class="container">
60+
<div class="row text-center justify-content-md-center">
61+
<div class="alert alert-danger">
62+
<% alert.danger %>
63+
</div>
64+
</div>
65+
</div>
66+
<% END %>
5867

5968
</div>
6069
</div>

0 commit comments

Comments
 (0)