Connector.pm 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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, $workdir=undef ) {
  10. my $obj = bless {
  11. 'ppid' => $$, # May not need this ultimately
  12. 'host' => $host,
  13. 'verbosity' => $verbosity,
  14. 'workdir' => $workdir,
  15. }, $class;
  16. return $obj;
  17. }
  18. # Plenty o code duplication here, can probably golf down?
  19. sub ssh ($self) {
  20. return $ssh if $ssh;
  21. print "# Connecting to $self->{'host'} via Net::OpenSSH" if $self->{'verbosity'} >= 1;
  22. $ssh = Net::OpenSSH->new($self->{'host'});
  23. die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
  24. return $ssh;
  25. }
  26. sub sftp ($self) {
  27. return $sftp if $sftp;
  28. print "# Connecting to $self->{'host'} via Net::SFTP::Foreign" if $self->{'verbosity'} >= 1;
  29. $sftp = $self->ssh->sftp();
  30. die "SFTP Connection failed: " . $sftp->error if $sftp->error;
  31. return $sftp;
  32. }
  33. 1;