| 1 | package CPAN::Mini::Inject::REST::Client::Command::add; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base 'CPAN::Mini::Inject::REST::Client::Command'; | ||
| 7 | use File::Basename; | ||
| 8 | |||
| 9 | |||
| 10 | #--Command usage---------------------------------------------------------------- | ||
| 11 | |||
| 12 | sub usage_desc { | ||
| 13 | return "add %o <filename>"; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub abstract { | ||
| 17 | return "Adds a new file to the repository"; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub description { | ||
| 21 | return "Adds a new file to the repository"; | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | #--Command specific options----------------------------------------------------- | ||
| 26 | |||
| 27 | sub options { | ||
| 28 | my ($class, $app) = @_; | ||
| 29 | |||
| 30 | return undef; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | #--Validate args---------------------------------------------------------------- | ||
| 35 | |||
| 36 | sub validate_args { | ||
| 37 | my ($self, $opt, $args) = @_; | ||
| 38 | |||
| 39 | $self->usage_error('Filename must be specified') unless @$args; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | #--Command execute method------------------------------------------------------- | ||
| 44 | |||
| 45 | sub execute { | ||
| 46 | my ($self, $opt, $args) = @_; | ||
| 47 | |||
| 48 | my $file = shift @$args; | ||
| 49 | my $base_file = basename($file); | ||
| 50 | |||
| 51 | my ($code, $result) = $self->api($opt)->post( | ||
| 52 | "repository/$base_file", { | ||
| 53 | file => [$file], | ||
| 54 | } | ||
| 55 | ); | ||
| 56 | |||
| 57 | given ($code) { | ||
| 58 | when (201) { | ||
| 59 | say "$base_file has been added to the repository\n"; | ||
| 60 | say "Indexed modules:"; | ||
| 61 | say ' ' . $_->{module} . ' (' . $_->{version} . ')' foreach (@{$result->{provides}}); | ||
| 62 | } | ||
| 63 | when (400) { | ||
| 64 | die $result->{error}, "\n"; | ||
| 65 | } | ||
| 66 | default { | ||
| 67 | die "Could not add file - unknown error!\n"; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | #------------------------------------------------------------------------------- | ||
| 74 | |||
| 75 | 1; |