| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | |||||||||||
| 3 | package DarkPAN::Indexer::CLI; | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use CLI::Simple::Constants qw(:booleans); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use English qw(-no_match_vars); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use parent qw(CLI::Simple); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | caller or exit __PACKAGE__->main(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | |||||||||||
| 17 | 1 | our $VERSION = '1.0.2'; | |||||||||
| 18 | |||||||||||
| 19 | ######################################################################## | ||||||||||
| 20 | 1 | ▶ | sub cmd_index_darkpan { # 'index' / default | ||||||||
|
Subroutine
cmd_index_darkpan: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | ######################################################################## | ||||||||||
| 22 | 1 | my ($self) = @_; | |||||||||
| 23 | |||||||||||
| 24 | 1 | my $indexer = $self->get_indexer; # builds DarkPAN::Indexer from --config/--darkpan | |||||||||
| 25 | |||||||||||
| 26 | 1 | my $stats = $indexer->create_index; | |||||||||
| 27 | |||||||||||
| 28 | 1 | printf {*STDOUT} "indexed %d distributions (%d modules)\n", $stats->{distributions_indexed}, $stats->{modules_written}; # adjust to real stat keys | |||||||||
| 29 | |||||||||||
| 30 | 1 | return $SUCCESS; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | ######################################################################## | ||||||||||
| 34 | 0 | ▶ | sub cmd_update { # 'update <dist-key>' | ||||||||
|
Subroutine
cmd_update: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 35 | ######################################################################## | ||||||||||
| 36 | 0 | my ($self) = @_; | |||||||||
| 37 | |||||||||||
| 38 | 0 | my ($key) = $self->get_args; # positional | |||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | die "usage: update <distribution-key>\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | if !$key; | ||||||||||
| 42 | |||||||||||
| 43 | 0 | $self->get_indexer->update_index( distribution => $key ); | |||||||||
| 44 | |||||||||||
| 45 | 0 | print {*STDOUT} "updated $key\n"; | |||||||||
| 46 | |||||||||||
| 47 | 0 | return $SUCCESS; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | ######################################################################## | ||||||||||
| 51 | 0 | ▶ | sub cmd_delete { # 'delete <dist-key>' | ||||||||
|
Subroutine
cmd_delete: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 52 | ######################################################################## | ||||||||||
| 53 | 0 | my ($self) = @_; | |||||||||
| 54 | 0 | my ($key) = $self->get_args; | |||||||||
| 55 | 0 | ▶ | croak "usage: delete <distribution-key>\n" if !$key; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | $self->get_indexer->delete_from_index( distribution => $key ); | |||||||||
| 57 | 0 | print "deleted $key\n"; | |||||||||
| 58 | 0 | return $SUCCESS; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | ######################################################################## | ||||||||||
| 62 | 1 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 63 | ######################################################################## | ||||||||||
| 64 | 1 | my ($self) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 1 | $self->set_indexer( $self->_indexer ); | |||||||||
| 67 | |||||||||||
| 68 | 1 | return $self; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | ######################################################################## | ||||||||||
| 72 | 1 | ▶ | sub _indexer { # shared: build orchestrator from CLI options | ||||||||
|
Subroutine
_indexer: called CC=1 SCAR=0.0
| |||||||||||
| 73 | ######################################################################## | ||||||||||
| 74 | 1 | my ($self) = @_; | |||||||||
| 75 | |||||||||||
| 76 | 1 | return DarkPAN::Indexer->new( | |||||||||
| 77 | config_file => $self->get_config, # --config | ||||||||||
| 78 | ); | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | ######################################################################## | ||||||||||
| 82 | 0 | ▶ | sub main { | ||||||||
|
Subroutine
main: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 83 | ######################################################################## | ||||||||||
| 84 | 0 | my $cli = __PACKAGE__->new( | |||||||||
| 85 | option_specs => [ | ||||||||||
| 86 | qw( | ||||||||||
| 87 | help|h | ||||||||||
| 88 | config|c=s | ||||||||||
| 89 | ) | ||||||||||
| 90 | ], | ||||||||||
| 91 | |||||||||||
| 92 | default_options => {}, | ||||||||||
| 93 | |||||||||||
| 94 | commands => { | ||||||||||
| 95 | default => \&cmd_index_darkpan, | ||||||||||
| 96 | 'index-darkpan' => \&cmd_index_darkpan, | ||||||||||
| 97 | 'delete-distribution' => \&cmd_delete, | ||||||||||
| 98 | 'update-darkpan' => \&cmd_update, | ||||||||||
| 99 | }, | ||||||||||
| 100 | extra_options => [qw(indexer)], | ||||||||||
| 101 | abbreviations => $TRUE, | ||||||||||
| 102 | ); | ||||||||||
| 103 | |||||||||||
| 104 | 0 | return $cli->run(); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | 1; | |||||||||
| 108 | |||||||||||
| 109 | ## no critic | ||||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||
| 112 | |||||||||||
| 113 | =pod | ||||||||||
| 114 | |||||||||||
| 115 | =encoding utf8 | ||||||||||
| 116 | |||||||||||
| 117 | =head1 NAME | ||||||||||
| 118 | |||||||||||
| 119 | DarkPAN::Indexer::CLI - command-line driver for DarkPAN::Indexer | ||||||||||
| 120 | |||||||||||
| 121 | =head1 SYNOPSIS | ||||||||||
| 122 | |||||||||||
| 123 | # build the whole index from the repository | ||||||||||
| 124 | darkpan-indexer --config darkpan.json index-darkpan | ||||||||||
| 125 | |||||||||||
| 126 | # (re)index a single distribution | ||||||||||
| 127 | darkpan-indexer --config darkpan.json update-darkpan authors/id/A/AB/AUTHOR/Foo-1.0.tar.gz | ||||||||||
| 128 | |||||||||||
| 129 | # remove a distribution from the index-darkpan | ||||||||||
| 130 | darkpan-indexer --config darkpan.json delete-distribution authors/id/A/AB/AUTHOR/Foo-1.0.tar.gz | ||||||||||
| 131 | |||||||||||
| 132 | I<Note: all commands can be abbreviated - index, update, delete> | ||||||||||
| 133 | |||||||||||
| 134 | =head1 DESCRIPTION | ||||||||||
| 135 | |||||||||||
| 136 | C<DarkPAN::Indexer::CLI> is a thin command-line front end over | ||||||||||
| 137 | L<DarkPAN::Indexer>. It exists so a DarkPAN can be built and maintained | ||||||||||
| 138 | B<without> an external pipeline (e.g. a Lambda handler): point it at a config | ||||||||||
| 139 | file and run a command. Each command parses its arguments and delegates to the | ||||||||||
| 140 | orchestrator, which does the real work (locking, fetching, indexing, | ||||||||||
| 141 | publishing). | ||||||||||
| 142 | |||||||||||
| 143 | This is a L<CLI::Simple> modulino; it is normally invoked through the | ||||||||||
| 144 | C<darkpan-indexer> wrapper script. | ||||||||||
| 145 | |||||||||||
| 146 | =head1 OPTIONS | ||||||||||
| 147 | |||||||||||
| 148 | =over 4 | ||||||||||
| 149 | |||||||||||
| 150 | =item C<--config>, C<-c> I<FILE> | ||||||||||
| 151 | |||||||||||
| 152 | Path to the DarkPAN config file (JSON). See L<DarkPAN::Indexer/CONFIGURATION>. | ||||||||||
| 153 | |||||||||||
| 154 | =item C<--help>, C<-h> | ||||||||||
| 155 | |||||||||||
| 156 | Show usage. | ||||||||||
| 157 | |||||||||||
| 158 | =back | ||||||||||
| 159 | |||||||||||
| 160 | =head1 COMMANDS | ||||||||||
| 161 | |||||||||||
| 162 | =head2 index-darkpan | ||||||||||
| 163 | |||||||||||
| 164 | darkpan-indexer --config darkpan.json index | ||||||||||
| 165 | |||||||||||
| 166 | Full rebuild: index every distribution in the repository and publish the index. | ||||||||||
| 167 | Delegates to C<< DarkPAN::Indexer->create_index >>. This is the default command. | ||||||||||
| 168 | |||||||||||
| 169 | =head2 update-darkpan I<DISTRIBUTION-KEY> | ||||||||||
| 170 | |||||||||||
| 171 | darkpan-indexer --config darkpan.json update authors/id/A/AB/AUTHOR/Foo-1.0.tar.gz | ||||||||||
| 172 | |||||||||||
| 173 | Incrementally (re)index a single distribution, given its storage key. Delegates | ||||||||||
| 174 | to C<< DarkPAN::Indexer->update_index( distribution => $key ) >>. | ||||||||||
| 175 | |||||||||||
| 176 | =head2 delete-distribution I<DISTRIBUTION-KEY> | ||||||||||
| 177 | |||||||||||
| 178 | darkpan-indexer --config darkpan.json delete authors/id/A/AB/AUTHOR/Foo-1.0.tar.gz | ||||||||||
| 179 | |||||||||||
| 180 | Remove a single distribution's packages from the index, given its storage key. | ||||||||||
| 181 | Delegates to C<< DarkPAN::Indexer->delete_from_index( distribution => $key ) >>. | ||||||||||
| 182 | |||||||||||
| 183 | =head1 EXIT STATUS | ||||||||||
| 184 | |||||||||||
| 185 | Returns C<$SUCCESS> (0) on success. Errors from the orchestrator (missing | ||||||||||
| 186 | config, storage failures, lock contention) propagate as exceptions. | ||||||||||
| 187 | |||||||||||
| 188 | =head1 SEE ALSO | ||||||||||
| 189 | |||||||||||
| 190 | L<DarkPAN::Indexer> | ||||||||||
| 191 | |||||||||||
| 192 | =head1 AUTHOR | ||||||||||
| 193 | |||||||||||
| 194 | Rob Lauer - <rlauer@treasurersbriefcase.com> | ||||||||||
| 195 | |||||||||||
| 196 | =cut | ||||||||||