| 1 | package App::lcpan::Cmd::scripts; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.010; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 3 | require App::lcpan; | |||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 3 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||
| 10 | 3 | our $DATE = '2023-09-26'; # DATE | |||||||||||||||||||||
| 11 | 3 | our $DIST = 'App-lcpan'; # DIST | |||||||||||||||||||||
| 12 | 3 | our $VERSION = '1.074'; # VERSION | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 3 | our %SPEC; | |||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | $SPEC{'handle_cmd'} = { | ||||||||||||||||||||||
| 17 | v => 1.1, | ||||||||||||||||||||||
| 18 | summary => 'List scripts', | ||||||||||||||||||||||
| 19 | description => <<'_', | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | This subcommand lists scripts. Scripts are identified heuristically from | ||||||||||||||||||||||
| 22 | contents of release archives matching this regex: | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | # container dir, script dir, script name | ||||||||||||||||||||||
| 25 | \A (\./)? ([^/]+)/? (s?bin|scripts?)/ ([^/]+) \z | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | A few exception are excluded, e.g. if script name begins with a dot (e.g. | ||||||||||||||||||||||
| 28 | `bin/.exists` which is usually a marker only). | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | Scripts are currently indexed by its release file and its name, so if a single | ||||||||||||||||||||||
| 31 | release contains both `bin/foo` and `script/foo`, only one of those will be | ||||||||||||||||||||||
| 32 | indexed. Normally a proper release shouldn't be like that though. | ||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | _ | ||||||||||||||||||||||
| 35 | args => { | ||||||||||||||||||||||
| 36 | %App::lcpan::common_args, | ||||||||||||||||||||||
| 37 | %App::lcpan::fauthor_args, | ||||||||||||||||||||||
| 38 | %App::lcpan::fdist_args, | ||||||||||||||||||||||
| 39 | %App::lcpan::query_multi_args, | ||||||||||||||||||||||
| 40 | query_type => { | ||||||||||||||||||||||
| 41 | schema => ['str*', in=>[qw/any name exact-name abstract/]], | ||||||||||||||||||||||
| 42 | default => 'any', | ||||||||||||||||||||||
| 43 | cmdline_aliases => { | ||||||||||||||||||||||
| 44 | x => { | ||||||||||||||||||||||
| 45 | summary => 'Shortcut for --query-type exact-name', | ||||||||||||||||||||||
| 46 | is_flag => 1, | ||||||||||||||||||||||
| 47 | 0 | ▶ | code => sub { $_[0]{query_type} = 'exact-name' }, | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||
| 49 | n => { | ||||||||||||||||||||||
| 50 | summary => 'Shortcut for --query-type name', | ||||||||||||||||||||||
| 51 | is_flag => 1, | ||||||||||||||||||||||
| 52 | 0 | ▶ | code => sub { $_[0]{query_type} = 'name' }, | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||
| 56 | #%App::lcpan::dist_args, | ||||||||||||||||||||||
| 57 | # all=>1 | ||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||
| 59 | 3 | }; | |||||||||||||||||||||
| 60 | 0 | ▶ | sub handle_cmd { | ||||||||||||||||||||
|
Subroutine
handle_cmd: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||||||||||
| 61 | 0 | my %args = @_; | |||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | 0 | my $state = App::lcpan::_init(\%args, 'ro'); | |||||||||||||||||||||
| 64 | 0 | my $dbh = $state->{dbh}; | |||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | 0 | my $detail = $args{detail}; | |||||||||||||||||||||
| 67 | 0 | ▶ | my $author = uc($args{author} // ''); | ||||||||||||||||||||
|
Condition0%0/2
$args{"author"} // ""
MC/DC0%0/1
$args{"author"} // ""
$args{"author"}
Truth table0%0/2
A
A= $args{"author"}
| |||||||||||||||||||||||
| 68 | 0 | my $dist = $args{dist}; | |||||||||||||||||||||
| 69 | 0 | ▶ | my $qt = $args{query_type} // 'any'; | ||||||||||||||||||||
|
Condition0%0/2
$args{"query_type"} // "any"
MC/DC0%0/1
$args{"query_type"} // "any"
$args{"query_type"}
Truth table0%0/2
A
A= $args{"query_type"}
| |||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | 0 | my @bind; | |||||||||||||||||||||
| 72 | my @where; | ||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||
| 74 | 0 | my @q_where; | |||||||||||||||||||||
| 75 | 0 | ▶ | for my $q0 (@{ $args{query} // [] }) { | ||||||||||||||||||||
|
Condition0%0/2
$args{"query"} // []
MC/DC0%0/1
$args{"query"} // []
$args{"query"}
Truth table0%0/2
A
A= $args{"query"}
| |||||||||||||||||||||||
| 76 | 0 | ▶ | if ($qt eq 'any') { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 77 | 0 | ▶ | my $q = $q0 =~ /%/ ? $q0 : '%'.$q0.'%'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 78 | 0 | push @q_where, "(script.name LIKE ? OR script.abstract LIKE ?)"; | |||||||||||||||||||||
| 79 | 0 | push @bind, $q, $q; | |||||||||||||||||||||
| 80 | } elsif ($qt eq 'abstract') { | ||||||||||||||||||||||
| 81 | 0 | ▶ | my $q = $q0 =~ /%/ ? $q0 : '%'.$q0.'%'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 82 | 0 | push @q_where, "(script.abstract LIKE ?)"; | |||||||||||||||||||||
| 83 | 0 | push @bind, $q; | |||||||||||||||||||||
| 84 | } elsif ($qt eq 'name') { | ||||||||||||||||||||||
| 85 | 0 | ▶ | my $q = $q0 =~ /%/ ? $q0 : '%'.$q0.'%'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 86 | 0 | push @q_where, "(script.name LIKE ?)"; | |||||||||||||||||||||
| 87 | 0 | push @bind, $q; | |||||||||||||||||||||
| 88 | } elsif ($qt eq 'exact-name') { | ||||||||||||||||||||||
| 89 | 0 | push @q_where, "(script.name=?)"; | |||||||||||||||||||||
| 90 | 0 | push @bind, $q0; | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | 0 | ▶ | if (@q_where > 1) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 94 | 0 | ▶ | push @where, "(".join(($args{or} ? " OR " : " AND "), @q_where).")"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 95 | } elsif (@q_where == 1) { | ||||||||||||||||||||||
| 96 | 0 | push @where, @q_where; | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||
| 99 | 0 | ▶ | if ($author) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 100 | 0 | push @where, "(script.cpanid=?)"; | |||||||||||||||||||||
| 101 | 0 | push @bind, $author; | |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||
| 103 | 0 | ▶ | if ($dist) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 104 | 0 | push @where, "(script.file_id=(SELECT id FROM file WHERE dist_name=?))"; | |||||||||||||||||||||
| 105 | 0 | push @bind, $dist; | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | 0 | ▶ | my $sql = "SELECT | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 109 | file.dist_name dist, | ||||||||||||||||||||||
| 110 | file.dist_version dist_version, | ||||||||||||||||||||||
| 111 | file.name release, | ||||||||||||||||||||||
| 112 | script.cpanid cpanid, | ||||||||||||||||||||||
| 113 | script.name name, | ||||||||||||||||||||||
| 114 | script.abstract abstract | ||||||||||||||||||||||
| 115 | FROM script | ||||||||||||||||||||||
| 116 | LEFT JOIN file ON file.id=script.file_id". | ||||||||||||||||||||||
| 117 | (@where ? " WHERE ".join(" AND ", @where) : ""); | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | 0 | my @res; | |||||||||||||||||||||
| 120 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||
| 121 | 0 | $sth->execute(@bind); | |||||||||||||||||||||
| 122 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 123 | 0 | ▶ | push @res, $detail ? $row : $row->{name}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||
| 125 | 0 | my $resmeta = {}; | |||||||||||||||||||||
| 126 | 0 | ▶ | $resmeta->{'table.fields'} = [qw/name abstract dist dist_version release cpanid/] | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 127 | if $detail; | ||||||||||||||||||||||
| 128 | 0 | [200, "OK", \@res, $resmeta]; | |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 3 | 1; | |||||||||||||||||||||
| 132 | # ABSTRACT: List scripts | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||