| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=2 sw=2 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::Repositorio::Server::Docker::Search; | ||
| 8 | |||
| 9 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 10 | use File::Spec; | ||
| 11 | require IO::All; | ||
| 12 | use JSON::XS; | ||
| 13 | use Data::Dumper; | ||
| 14 | |||
| 15 | our $VERSION = '1.2.1'; # VERSION | ||
| 16 | |||
| 17 | sub search { | ||
| 18 | my ($self) = @_; | ||
| 19 | |||
| 20 | my $repo_dir = | ||
| 21 | File::Spec->catdir( $self->app->get_repo_dir( repo => $self->repo->{name} ), | ||
| 22 | "repository" ); | ||
| 23 | |||
| 24 | my @json_files; | ||
| 25 | my @dirs = ($repo_dir); | ||
| 26 | for my $dir (@dirs) { | ||
| 27 | opendir( my $dh, $dir ); | ||
| 28 | while ( my $entry = readdir($dh) ) { | ||
| 29 | next if ( $entry =~ m/^\./ ); | ||
| 30 | if ( -d File::Spec->catdir( $dir, $entry ) ) { | ||
| 31 | push @dirs, File::Spec->catdir( $dir, $entry ); | ||
| 32 | } | ||
| 33 | if ( -f File::Spec->catfile( $dir, $entry, "repo.json" ) ) { | ||
| 34 | push @json_files, File::Spec->catfile( $dir, $entry, "repo.json" ); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | closedir($dh); | ||
| 38 | } | ||
| 39 | |||
| 40 | my $search = $self->param("q"); | ||
| 41 | |||
| 42 | my @search_result = | ||
| 43 | map { | ||
| 44 | my @_t = split( /\//, $_ ); | ||
| 45 | { | ||
| 46 | description => '', | ||
| 47 | name => "$_t[-3]/$_t[-2]", | ||
| 48 | } | ||
| 49 | } | ||
| 50 | grep { m/\Q$search\E/ } @json_files; | ||
| 51 | |||
| 52 | my $ret = { | ||
| 53 | num_results => 1, | ||
| 54 | query => $search, | ||
| 55 | results => \@search_result, | ||
| 56 | }; | ||
| 57 | |||
| 58 | $self->render( json => $ret ); | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; |