File Coverage

lib/Rex/Interface/Fs/HTTP.pm
Criterion Covered Total %
statement 24 107 22.4
branch 0 12 0.0
condition 0 3 0.0
subroutine 9 29 31.0
pod 0 20 0.0
total 33 171 19.3


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Interface::Fs::HTTP;
6              
7 1     1   14 use v5.12.5;
  1         5  
8 1     1   6 use warnings;
  1         2  
  1         42  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 1     1   6 use Rex::Commands;
  1         2  
  1         6  
13 1     1   14 use Rex::Interface::Exec;
  1         2  
  1         7  
14 1     1   26 use Rex::Interface::Fs::Base;
  1         3  
  1         10  
15 1     1   21 use Data::Dumper;
  1         2  
  1         71  
16              
17             BEGIN {
18 1     1   7 use Rex::Require;
  1         3  
  1         14  
19 1     1   56 MIME::Base64->use;
20             }
21 1     1   7 use base qw(Rex::Interface::Fs::Base);
  1         3  
  1         1375  
22              
23             sub new {
24 0     0 0   my $that = shift;
25 0   0       my $proto = ref($that) || $that;
26 0           my $self = $proto->SUPER::new(@_);
27              
28 0           bless( $self, $proto );
29              
30 0           return $self;
31             }
32              
33             sub ls {
34 0     0 0   my ( $self, $path ) = @_;
35              
36 0           my $resp = connection->post( "/fs/ls", { path => $path } );
37 0 0         if ( $resp->{ok} ) {
38 0           return @{ $resp->{ls} };
  0            
39             }
40             }
41              
42             sub is_dir {
43 0     0 0   my ( $self, $path ) = @_;
44 0           my $resp = connection->post( "/fs/is_dir", { path => $path } );
45 0           return $resp->{ok};
46             }
47              
48             sub is_file {
49 0     0 0   my ( $self, $file ) = @_;
50 0           my $resp = connection->post( "/fs/is_file", { path => $file } );
51 0           return $resp->{ok};
52             }
53              
54             sub unlink {
55 0     0 0   my ( $self, @files ) = @_;
56              
57 0           my $ok = 0;
58 0           for my $file (@files) {
59 0           my $resp = connection->post( "/fs/unlink", { path => $file } );
60 0           $ok = $resp->{ok};
61             }
62              
63 0           return $ok;
64             }
65              
66             sub mkdir {
67 0     0 0   my ( $self, $dir ) = @_;
68 0           my $resp = connection->post( "/fs/mkdir", { path => $dir } );
69 0           return $resp->{ok};
70             }
71              
72             sub stat {
73 0     0 0   my ( $self, $file ) = @_;
74 0           my $resp = connection->post( "/fs/stat", { path => $file } );
75 0 0         if ( $resp->{ok} ) {
76 0           return %{ $resp->{stat} };
  0            
77             }
78              
79 0           return undef;
80             }
81              
82             sub is_readable {
83 0     0 0   my ( $self, $file ) = @_;
84 0           my $resp = connection->post( "/fs/is_readable", { path => $file } );
85 0           return $resp->{ok};
86             }
87              
88             sub is_writable {
89 0     0 0   my ( $self, $file ) = @_;
90 0           my $resp = connection->post( "/fs/is_writable", { path => $file } );
91 0           return $resp->{ok};
92             }
93              
94             sub readlink {
95 0     0 0   my ( $self, $file ) = @_;
96 0           my $resp = connection->post( "/fs/readlink", { path => $file } );
97 0 0         if ( $resp->{ok} ) {
98 0           return $resp->{link};
99             }
100             }
101              
102             sub rename {
103 0     0 0   my ( $self, $old, $new ) = @_;
104 0           my $resp = connection->post( "/fs/rename", { old => $old, new => $new } );
105 0           return $resp->{ok};
106             }
107              
108             sub glob {
109 0     0 0   my ( $self, $glob ) = @_;
110 0           my $resp = connection->post( "/fs/glob", { glob => $glob } );
111 0 0         if ( $resp->{ok} ) {
112 0           return @{ $resp->{glob} };
  0            
113             }
114             }
115              
116             sub upload {
117 0     0 0   my ( $self, $source, $target ) = @_;
118              
119 0           my $resp = connection->upload( [ content => [$source], path => $target ] );
120 0           return $resp->{ok};
121             }
122              
123             sub download {
124 0     0 0   my ( $self, $source, $target ) = @_;
125              
126 0           my $resp = connection->post( "/fs/download", { path => $source } );
127 0 0         if ( $resp->{ok} ) {
128 0 0         open( my $fh, ">", $target ) or die($!);
129 0           print $fh decode_base64( $resp->{content} );
130 0           close($fh);
131              
132 0           return 1;
133             }
134              
135 0           return 0;
136             }
137              
138             sub ln {
139 0     0 0   my ( $self, $from, $to ) = @_;
140              
141 0           Rex::Logger::debug("Symlinking files: $to -> $from");
142 0           my $resp = connection->post( "/fs/ln", { from => $from, to => $to } );
143 0           return $resp->{ok};
144             }
145              
146             sub rmdir {
147 0     0 0   my ( $self, @dirs ) = @_;
148              
149 0           Rex::Logger::debug( "Removing directories: " . join( ", ", @dirs ) );
150 0           my $ok = 0;
151 0           for my $dir (@dirs) {
152 0           my $resp = connection->post( "/fs/rmdir", { path => $dir } );
153 0           $ok = $resp->{ok};
154             }
155              
156 0           return $ok;
157             }
158              
159             sub chown {
160 0     0 0   my ( $self, $user, $file, @opts ) = @_;
161              
162 0           my $resp = connection->post(
163             "/fs/chown",
164             {
165             user => $user,
166             path => $file,
167             options => {@opts},
168             }
169             );
170              
171 0           return $resp->{ok};
172             }
173              
174             sub chgrp {
175 0     0 0   my ( $self, $group, $file, @opts ) = @_;
176              
177 0           my $resp = connection->post(
178             "/fs/chgrp",
179             {
180             group => $group,
181             path => $file,
182             options => {@opts},
183             }
184             );
185              
186 0           return $resp->{ok};
187             }
188              
189             sub chmod {
190 0     0 0   my ( $self, $mode, $file, @opts ) = @_;
191              
192 0           my $resp = connection->post(
193             "/fs/chmod",
194             {
195             mode => $mode,
196             path => $file,
197             options => {@opts},
198             }
199             );
200              
201 0           return $resp->{ok};
202             }
203              
204             sub cp {
205 0     0 0   my ( $self, $source, $dest ) = @_;
206              
207 0           my $resp = connection->post(
208             "/fs/cp",
209             {
210             source => $source,
211             dest => $dest,
212             }
213             );
214              
215 0           return $resp->{ok};
216             }
217              
218             1;