Connector.pm 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package App::Prove::Remote::Connector;
  2. use strict;
  3. use warnings;
  4. use Net::OpenSSH ();
  5. use Net::SFTP::Foreign ();
  6. use experimental 'signatures';
  7. # Cache the connections/objects internally
  8. my ( $ssh, $sftp );
  9. sub new ( $class, $host='127.0.0.1', $verbosity=0 ) {
  10. my $obj = bless {
  11. 'ppid' => $$, # May not need this ultimately
  12. 'host' => $host,
  13. 'verbosity' => $verbosity
  14. }, $class;
  15. return $obj;
  16. }
  17. # Plenty o code duplication here, can probably golf down?
  18. sub ssh ($self) {
  19. return $ssh if $ssh;
  20. print "# Connecting to $self->{'host'} via Net::OpenSSH" if $self->{'verbosity'} >= 1;
  21. $ssh = Net::OpenSSH->new($self->{'host'});
  22. die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
  23. return $ssh;
  24. }
  25. sub sftp ($self) {
  26. return $sftp if $sftp;
  27. print "# Connecting to $self->{'host'} via Net::SFTP::Foreign" if $self->{'verbosity'} >= 1;
  28. $sftp = $self->ssh->sftp();
  29. die "SFTP Connection failed: " . $sftp->error if $sftp->error;
  30. return $sftp;
  31. }
  32. 1;