363e214d7156575d42c78ee22a3f0d89f625fda1
[scpubgit/Object-Remote.git] / lib / Object / Remote / Connector / SSH.pm
1 package Object::Remote::Connector::SSH;
2
3 use Object::Remote::ModuleSender;
4 use Object::Remote::Handle;
5 use Moo;
6
7 with 'Object::Remote::Role::Connector::PerlInterpreter';
8
9 has ssh_to => (is => 'ro', required => 1);
10
11 has ssh_perl_command => (is => 'lazy');
12
13 has ssh_options => (is => 'ro', default => sub { [ '-A' ] });
14
15 has ssh_command => (is => 'ro', default => sub { 'ssh' });
16
17 sub _escape_shell_arg { 
18     my ($self, $str) = (@_);
19     $str =~ s/((?:^|[^\\])(?:\\\\)*)'/$1\\'/g;
20     return "$str";
21 }
22
23
24 sub _build_ssh_perl_command {
25   my ($self) = @_;
26   my $perl_command = join('', @{$self->perl_command});
27   
28   #TODO non-trivial to escape properly for ssh and shell
29   #this "works" but is not right, needs to be replaced
30   #after testing
31   return [
32     do { my $c = $self->ssh_command; ref($c) ? @$c : $c },
33     @{$self->ssh_options}, $self->ssh_to,
34     $self->_escape_shell_arg($perl_command),
35   ];
36 }
37
38 sub final_perl_command { shift->ssh_perl_command }
39
40 no warnings 'once';
41
42 push @Object::Remote::Connection::Guess, sub { 
43   for ($_[0]) {
44     # 0-9 a-z _ - first char, those or . subsequent - hostnamish
45     if (defined and !ref and /^(?:.*?\@)?[\w\-][\w\-\.]/) {
46       my $host = shift(@_);
47       return __PACKAGE__->new(@_, ssh_to => $host);
48     }
49   }
50   return;
51 };
52
53 1;