lib/Rex/Commands/Download.pm

filter: Statementstmt 48.3% 45 / 93 Branchbran 7.1% 2 / 28 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.0% 12 / 16 Podpod 100% 1 / 1 totaltotal 39.8% 61 / 153 CC 18 SCAR 33.8
CC
18
Cov
37%
_get_http
90.0
download
20.0
_sftp_download
12.0
CRAP 29.3
« lib/Rex/Virtualization/Docker/import.pm ↑ Summary blib/lib/Rex/Template.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::Download - Download remote files
8  
9 =head1 DESCRIPTION
10  
11 With this module you can download a remotefile via sftp, http and ftp from a host to your local computer.
12  
13 Version <= 1.0: All these functions will not be reported.
14  
15 =head1 SYNOPSIS
16  
17 # sftp
18 task "download", "remoteserver", sub {
19 download "/remote/file", "localfile";
20 };
21  
22 # http
23 task "download2", sub {
24 download "http://server/remote/file";
25 };
26  
27  
28 =head1 EXPORTED FUNCTIONS
29  
30 =cut
31  
32 package Rex::Commands::Download;
33  
34 32 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 32 ▶use Rex::Helper::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 32 our $VERSION = '1.16.1'; # VERSION
40  
41 32 ▶use vars qw($has_wget $has_curl $has_lwp);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # check which download type we should use
44 32 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=14.8
45  
46 32 ▶ if ( $^O !~ m/^MSWin/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /^MSWin/u) 32 0 32
47 32 system("which wget >/dev/null 2>&1");
48 32 $has_wget = !$?;
49  
50 32 system("which curl >/dev/null 2>&1");
51 32 $has_curl = !$?;
52 }
53  
54 32 eval {
55 32 require Rex::Helper::UserAgent;
56 32 $has_lwp = 1;
57 };
58  
59 32 ▶ if ( $^O =~ m/^MSWin/ && !$has_lwp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/u and not $Rex::Commands::Download::has_lwp) 0 32 32
Condition33%1/3
$^O =~ /^MSWin/u and not $Rex::Commands::Download::has_lwp
!ll&&!rl&&r
3200
MC/DC0%0/2
$^O =~ /^MSWin/u and not $Rex::Commands::Download::has_lwp
$^O =~ /^MSWin/u not $Rex::Commands::Download::has_lwp
Truth table33%1/3
A and not(B)
A= $^O =~ /^MSWin/uB= not $Rex::Commands::Download::has_lwp
ABresult
0X0
100
111
60 0 Rex::Logger::info("Please install LWP::UserAgent to allow file downloads.");
61 }
62  
63 }
64  
65 32 require Rex::Exporter;
66  
67 32 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 32 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 32 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 32 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 32 ▶use Rex::Interface::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 32 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 32 @EXPORT = qw(download);
76  
77 =head2 download($remote, [$local])
78  
79 Perform a download. If no local file is specified it will download the file to the current directory.
80  
81 task "download", "remoteserver", sub {
82 download "/remote/file", "localfile";
83 };
84  
85 task "download", sub {
86 download "http://www.rexify.org/index.html", "localfile.html";
87 };
88  
89 =cut
90  
91 0 ▶sub download {
Subroutine download: not called CC=4 SCAR=30.0
Pod documented
92 0 my ( $remote, $local, %option ) = @_;
93  
94 0 ▶ unless ($local) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($local) 0 0 0
95 0 $local = basename($remote);
96 }
97  
98 0 ▶ if ( -d $local ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $local) 0 0 0
99 0 $local = $local . '/' . basename($remote);
100 }
101  
102 0 Rex::Logger::debug("saving file to $local");
103 0 $remote = resolv_path($remote);
104 0 $local = resolv_path( $local, 1 );
105  
106 0 ▶ if ( $remote =~ m/^(https?|ftp):\/\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remote =~ m[^(https?|ftp)://]u) { } 0 0 0
107 0 _http_download( $remote, $local, %option );
108 }
109 else {
110 0 _sftp_download( $remote, $local, %option );
111 }
112 }
113  
114 0 ▶sub _sftp_download {
Subroutine _sftp_download: not called CC=3 SCAR=24.8
115 0 my $remote = shift;
116 0 my $local = shift;
117  
118 0 my $fs = Rex::Interface::Fs->create;
119  
120 0 Rex::Logger::debug("Downloading via SFTP");
121  
122 0 ▶ unless ( is_file($remote) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_file($remote)) 0 0 0
123 0 Rex::Logger::info("File $remote not found");
124 0 die("$remote not found.");
125 }
126  
127 0 ▶ unless ( is_readable($remote) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_readable($remote)) 0 0 0
128 0 Rex::Logger::info("File $remote is not readable.");
129 0 die("$remote is not readable.");
130 }
131  
132 0 $fs->download( $remote, $local );
133  
134 }
135  
136 0 ▶sub _http_download {
Subroutine _http_download: not called CC=2 SCAR=17.9
137 0 my ( $remote, $local, %option ) = @_;
138  
139 0 my $content = _get_http( $remote, %option );
140  
141 0 ▶ open( my $fh, ">", $local ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $local or die $! 0 0 0
142 0 binmode $fh;
143 0 print $fh $content;
144 0 close($fh);
145 }
146  
147 0 ▶sub _get_http {
Subroutine _get_http: not called CC=9 SCAR=45.0
148 0 my ( $url, %option ) = @_;
149  
150 0 my $html;
151 0 ▶ if ($has_curl) {
Branch: 3 branches
BranchTrueFalseTotal
if ($Rex::Commands::Download::has_curl) { } 0 0 0
elsif ($Rex::Commands::Download::has_wget) { } 0 0 0
elsif ($Rex::Commands::Download::has_lwp) { } 0 0 0
152 0 Rex::Logger::debug("Downloading via curl");
153 0 ▶ if ( exists $option{user} && exists $option{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{'user'} and exists $option{'password'}) { } 0 0 0
Condition0%0/3
exists $option{"user"} and exists $option{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option{"user"} and exists $option{"password"}
exists $option{"user"} exists $option{"password"}
Truth table0%0/3
A and B
A= exists $option{"user"}B= exists $option{"password"}
ABresult
0X0
100
111
154 0 $html =
155 qx{curl -u '$option{user}:$option{password}' -# -L -k '$url' 2>/dev/null};
156 }
157 else {
158 0 $html = qx{curl -# -L -k '$url' 2>/dev/null};
159 }
160 }
161 elsif ($has_wget) {
162 0 Rex::Logger::debug("Downloading via wget");
163 0 ▶ if ( exists $option{user} && exists $option{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{'user'} and exists $option{'password'}) { } 0 0 0
Condition0%0/3
exists $option{"user"} and exists $option{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option{"user"} and exists $option{"password"}
exists $option{"user"} exists $option{"password"}
Truth table0%0/3
A and B
A= exists $option{"user"}B= exists $option{"password"}
ABresult
0X0
100
111
164 0 $html =
165 qx{wget --http-user=$option{user} '--http-password=$option{password}' --no-check-certificate -O - '$url' 2>/dev/null};
166 }
167 else {
168 0 $html = qx{wget --no-check-certificate -O - '$url' 2>/dev/null};
169 }
170 }
171 elsif ($has_lwp) {
172 0 Rex::Logger::debug("Downloading via LWP::UserAgent");
173 0 my $ua = Rex::Helper::UserAgent->new;
174 0 my $resp = $ua->get( $url, %option );
175 0 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
176 0 $html = $resp->content;
177 }
178 else {
179 0 confess "Error downloading $url.";
180 }
181 }
182 else {
183 0 die("No tool found to download something. (curl, wget, LWP::Simple)");
184 }
185  
186 0 return $html;
187 }
188  
189 32 1;
« lib/Rex/Virtualization/Docker/import.pm ↑ Summary blib/lib/Rex/Template.pm »