Client/Command/download.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 60 CC 3 SCAR 11.7
CC
3
Cov
0%
validate_args
6.0
execute
6.0
usage_desc
2.0
CRAP 3.2
1 package CPAN::Mini::Inject::REST::Client::Command::download;
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 "download %o <filename>";
14 }
15  
16 sub abstract {
17 return "Downloads a file from the repository";
18 }
19  
20 sub description {
21 return "Downloads a file from the repository";
22 }
23  
24  
25 #--Command specific options-----------------------------------------------------
26  
27 sub options {
28 my ($class, $app) = @_;
29
30 return (
31 [ "stdout" => "Output file to STDOUT" ]
32 );
33 }
34  
35  
36 #--Validate args----------------------------------------------------------------
37  
38 sub validate_args {
39 my ($self, $opt, $args) = @_;
40
41 $self->usage_error('Filename must be specified') unless @$args;
42 }
43  
44  
45 #--Command execute method-------------------------------------------------------
46  
47 sub execute {
48 my ($self, $opt, $args) = @_;
49
50 my $file = shift @$args;
51 my $base_file = basename($file);
52
53 my ($code, $result) = $self->api($opt)->get("repository/$base_file");
54
55 given ($code) {
56 when (200) {
57 if ($opt->stdout) {
58 print $result;
59 } else {
60 open my $fh, '>', $file;
61 print $fh $result;
62 close $fh;
63 }
64 }
65 when (404) {
66 die "File $base_file does not exist\n";
67 }
68 default {
69 die "Could not download file - unknown error!\n";
70 }
71 }
72 }
73  
74  
75 #-------------------------------------------------------------------------------
76  
77 1;