| 1 | package Shell::Carapace::SSH; | ||||||||||
| 2 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use String::ShellQuote; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | has callback => (is => 'rw', required => 1); | |||||||||
| 8 | 1 | has host => (is => 'rw', required => 1); | |||||||||
| 9 | 0 | ▶ | has ssh_options => (is => 'rw', default => sub { {} }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 10 | 1 | has ssh => (is => 'rw', lazy => 1, builder => 1, clearer => 1); | |||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | sub _build_ssh { | ||||||||
|
Subroutine
_build_ssh: not called CC=3 SCAR=24.8
| |||||||||||
| 13 | 0 | my $self = shift; | |||||||||
| 14 | 0 | require Net::OpenSSH; | |||||||||
| 15 | 0 | ▶ | my %ssh_options = $self->ssh_options ? %{ $self->ssh_options } : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 0 | my $ssh = Net::OpenSSH->new($self->host, %ssh_options); | |||||||||
| 17 | 0 | ▶ | die $ssh->error if $ssh->error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 0 | return $ssh; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | # force ssh builder to run so the connection occurs during object instantiation | ||||||||||
| 22 | 0 | ▶ | sub BUILD { shift->ssh } | ||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 23 | |||||||||||
| 24 | 0 | ▶ | sub reconnect { | ||||||||
|
Subroutine
reconnect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 25 | 0 | my $self = shift; | |||||||||
| 26 | 0 | $self->clear_ssh; | |||||||||
| 27 | 0 | $self->ssh; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 31 | 0 | my ($self, @cmd) = @_; | |||||||||
| 32 | |||||||||||
| 33 | 0 | $self->callback->('command', $self->_stringify(@cmd), $self->host); | |||||||||
| 34 | |||||||||||
| 35 | 0 | my ($pty, $pid) = $self->ssh->open2pty(@cmd); | |||||||||
| 36 | 0 | ▶ | die $self->ssh->error if $self->ssh->error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | while (my $line = <$pty>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | $line =~ s/([\r\n])$//g; | |||||||||
| 40 | 0 | $self->callback->('remote-output', $line, $self->host); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 0 | waitpid($pid, 0); | |||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | if ($? != 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | $self->callback->("error", $self->_stringify(@cmd), $self->host); | |||||||||
| 47 | 0 | croak "cmd failed"; | |||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub _stringify { | ||||||||
|
Subroutine
_stringify: not called CC=2 SCAR=17.9
| |||||||||||
| 52 | 0 | my ($self, @cmd) = @_; | |||||||||
| 53 | 0 | ▶ | return $cmd[0] if @cmd == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | return join(" ", shell_quote @cmd); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | 1; | |||||||||