File: FTP.pm

package info (click to toggle)
libalien-build-perl 2.84-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,116 kB
  • sloc: perl: 10,350; ansic: 134; sh: 66; makefile: 2
file content (65 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package MyTest::FTP;

use strict;
use warnings;
use Path::Tiny qw( path );
use JSON::PP qw( decode_json );
use Exporter qw( import );

our @EXPORT = qw( ftp_url ftp_error );

my $ftp_error;

sub ftp_error
{
  my($new) = @_;
  if($new)
  {
    $ftp_error = $new;
    return undef;
  }
  else
  {
    return $ftp_error;
  }
}

sub ftp_url
{
  my $file = path('t/bin/ftpd.json');
  return ftp_error('no ftpd.json') unless -r $file;

  my $config = eval { decode_json($file->slurp) };
  return ftp_error("error loading ftpd.json $@") if $@;

  my $url = $config->{url};
  return ftp_error("no url in ftpd.json") unless $url;

  require Net::FTP;
  require URI;

  $url = URI->new($url);

  my $ftp = Net::FTP->new($url->host, Port =>  $url->port) or do {
    return ftp_error("Connot connect to @{[ $url->host ]}");
  };

  eval {
    $ftp->login($url->user, $url->password) or die;
    $ftp->binary;
    $ftp->cwd($url->path) or die;
    my $path = Path::Tiny->tempfile;
    $ftp->get('foo-1.00.tar.xz', $path->stringify) or die;
    -e $path || die;
    $ftp->quit;
  };

  return ftp_error($ftp->message) if $@;

  $url->path($url->path . '/')
    unless $url->path =~ m!/$!;

  return $url;
}

1;