| 1 | package CPAN::Mirror::Tiny::CLI; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use CPAN::Mirror::Tiny; | ||
| 6 | use File::Find (); | ||
| 7 | use File::Spec; | ||
| 8 | use File::stat (); | ||
| 9 | use Getopt::Long (); | ||
| 10 | use IPC::Run3 (); | ||
| 11 | use POSIX (); | ||
| 12 | use Pod::Usage 1.33 (); | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | bless { | ||
| 17 | base => $ENV{PERL_CPAN_MIRROR_TINY_BASE} || "darkpan", | ||
| 18 | }, $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub run { | ||
| 22 | shift->_run(@_) ? 0 : 1; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub _run { | ||
| 26 | my $self = shift->new; | ||
| 27 | $self->parse_options(@_) or return; | ||
| 28 | |||
| 29 | my $cmd = shift @{$self->{argv}}; | ||
| 30 | if (!$cmd) { | ||
| 31 | warn "Missing subcommand, try `$0 --help`\n"; | ||
| 32 | return; | ||
| 33 | } | ||
| 34 | |||
| 35 | ( my $_cmd = $cmd ) =~ s/-/_/g; | ||
| 36 | if (my $sub = $self->can("cmd_$_cmd")) { | ||
| 37 | return $self->$sub(@{$self->{argv}}); | ||
| 38 | } else { | ||
| 39 | warn "Unknown subcommand '$cmd', try `$0 --help`\n"; | ||
| 40 | return; | ||
| 41 | } | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | sub parse_options { | ||
| 46 | my $self = shift; | ||
| 47 | local @ARGV = @_; | ||
| 48 | my $parser = Getopt::Long::Parser->new( | ||
| 49 | config => [qw(no_auto_abbrev no_ignore_case pass_through)], | ||
| 50 | ); | ||
| 51 | $parser->getoptions( | ||
| 52 | "h|help" => sub { $self->cmd_help; exit }, | ||
| 53 | "v|version" => sub { $self->cmd_version; exit }, | ||
| 54 | "q|quiet" => \$self->{quiet}, | ||
| 55 | "b|base=s" => \$self->{base}, | ||
| 56 | "a|author=s" => \$self->{author}, | ||
| 57 | ) or return 0; | ||
| 58 | $self->{argv} = \@ARGV; | ||
| 59 | return 1; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub cmd_help { | ||
| 63 | Pod::Usage::pod2usage(verbose => 99, sections => 'SYNOPSIS|OPTIONS|EXAMPLES'); | ||
| 64 | return 1; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub cmd_version { | ||
| 68 | my $klass = "CPAN::Mirror::Tiny"; | ||
| 69 | printf "%s %s\n", $klass, $klass->VERSION; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub cmd_inject { | ||
| 73 | my ($self, @argv) = @_; | ||
| 74 | die "Missing urls, try `$0 --help`\n" unless @argv; | ||
| 75 | my $cpan = CPAN::Mirror::Tiny->new(base => $self->{base}); | ||
| 76 | my $option = $self->{author} ? { author => $self->{author} } : +{}; | ||
| 77 | for my $argv (@argv) { | ||
| 78 | print STDERR "Injecting $argv" unless $self->{quiet}; | ||
| 79 | if (eval { $cpan->inject($argv, $option); 1 }) { | ||
| 80 | print STDERR " DONE\n" unless $self->{quiet}; | ||
| 81 | } else { | ||
| 82 | print STDERR " FAIL\n" unless $self->{quiet}; | ||
| 83 | die $@; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | return 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub cmd_gen_index { | ||
| 90 | my ($self, @argv) = @_; | ||
| 91 | my $cpan = CPAN::Mirror::Tiny->new(base => $self->{base}); | ||
| 92 | $cpan->write_index(compress => 1, $self->{quiet} ? () : (show_progress => 1)); | ||
| 93 | warn sprintf "Generated %s\n", $cpan->index_path(compress => 1) unless $self->{quiet}; | ||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub cmd_cat_index { | ||
| 98 | my ($self, @argv) = @_; | ||
| 99 | my $cpan = CPAN::Mirror::Tiny->new(base => $self->{base}); | ||
| 100 | my $index = $cpan->index_path(compress => 1); | ||
| 101 | return unless -f $index; | ||
| 102 | my @cmd = ($cpan->{gzip}, "--decompress", "--stdout", $index); | ||
| 103 | IPC::Run3::run3 \@cmd, \undef, undef, undef; | ||
| 104 | return $? == 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub cmd_list { | ||
| 108 | my $self = shift; | ||
| 109 | return unless -d $self->{base}; | ||
| 110 | my ($index, @dist); | ||
| 111 | my $wanted = sub { | ||
| 112 | my $name = $_; | ||
| 113 | return if !-f $name or $name =~ /\.json$/; | ||
| 114 | my $stat = File::stat::stat($name); | ||
| 115 | if ($name =~ /02packages.details.txt.gz$/) { | ||
| 116 | $index = {name => $name, mtime => $stat->mtime, size => $stat->size}; | ||
| 117 | } else { | ||
| 118 | push @dist, {name => $name, mtime => $stat->mtime, size => $stat->size}; | ||
| 119 | } | ||
| 120 | }; | ||
| 121 | File::Find::find({wanted => $wanted, no_chdir => 1}, $self->{base}); | ||
| 122 | |||
| 123 | my $print = sub { | ||
| 124 | printf "%s %8d %s\n", | ||
| 125 | POSIX::strftime("%FT%T", localtime($_[0]->{mtime})), | ||
| 126 | $_[0]->{size}, | ||
| 127 | $_[0]->{name}; | ||
| 128 | }; | ||
| 129 | $print->($index) if $index; | ||
| 130 | for my $dist (sort { $a->{name} cmp $b->{name} } @dist) { | ||
| 131 | $print->($dist); | ||
| 132 | } | ||
| 133 | return 1; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub cmd_server { | ||
| 137 | my $self = shift; | ||
| 138 | if (!eval { require CPAN::Mirror::Tiny::Server }) { | ||
| 139 | if ($@ =~ m{Can't locate Plack}) { | ||
| 140 | die "To run server, you should install Plack first.\n"; | ||
| 141 | } else { | ||
| 142 | die $@; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | CPAN::Mirror::Tiny::Server->start(@{$self->{argv}}, $self->{base}); | ||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; |