File Coverage

blib/lib/App/Goto.pm
Criterion Covered Total %
statement 43 49 87.7
branch 9 14 64.2
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 58 70 82.8


line stmt bran cond sub pod time code
1             package App::Goto;
2              
3 1     1   21879 use strict;
  1         38  
  1         44  
4 1     1   14 use v5.12;
  1         4  
  1         66  
5             our $VERSION = '0.07';
6              
7 1     1   987 use Moo;
  1         88548  
  1         7  
8              
9             has args => ( is => 'ro', required => 1 );
10             has config => ( is => 'ro', required => 1 );
11             has error => ( is => 'rw', default => sub { 'Unknown error' } );
12             has is_success => ( is => 'rw', default => sub { 1 } );
13             has cmd => ( is => 'rw' );
14             has nick => ( is => 'rw', default => sub { '' } );
15             has host => ( is => 'rw', default => sub { '' } );
16              
17             sub BUILD {
18 8     8 0 40 my $self = shift;
19 8         11 my ($server, $command) = @{$self->args};
  8         26  
20              
21 8         21 my $hostname = $self->_get_host($server);
22 8 50       23 return unless $hostname;
23              
24 8         9 my $remote_command = '';
25 8 100       22 if ($command) {
26 5         12 $remote_command = $self->_get_command($command);
27             }
28              
29 8         224 $self->cmd( "ssh $hostname $remote_command" );
30             }
31              
32             sub _get_host {
33 8     8   15 my ($self, $name) = @_;
34             # Get known hosts from config
35 8         22 my $hosts = $self->config->{hosts};
36             # Get the first hostname from a sorted list of hosts
37 8         10 my ($hostname) = grep { $_ =~ m#^$name# } sort keys %{$hosts};
  24         155  
  8         33  
38 8 50       25 unless ($hostname) {
39 0         0 $self->is_success(0);
40 0 0       0 $self->error('Cannot find hostname in config') unless $hostname;
41 0         0 return;
42             }
43             # Store the retrieved nick&host names for possible later use
44 8         29 $self->nick($hostname);
45 8         21 $self->host($hosts->{$hostname});
46             # Get the right server details for the found hostname
47 8         22 return $hosts->{$hostname};
48             }
49              
50             sub _get_command {
51 5     5   7 my ($self, $cmd) = @_;
52 5         11 my $config = $self->config;
53             # If they supplied one, separate out the modifier
54 5         5 my $modifier;
55 5         26 ($cmd, undef, $modifier) = $cmd =~ m#^([^/]*)(/(.*))?#;
56              
57             # Check if the server has its own instance of this command defined.
58             # If so, use it. If not, use the generic version.
59 5         7 my $command;
60 5 100       27 if (my $custom = $config->{$self->nick.'_commands'}{$cmd}) {
61 1         3 $command = $custom;
62             }
63             else {
64 4         9 $command = $config->{commands}{$cmd};
65             }
66              
67 5 50       12 unless ($command) {
68 0         0 $self->is_success(0);
69 0         0 $self->error('Command not recognised');
70 0         0 return;
71             }
72              
73             # Replace command's modifier placeholder with supplied modifier
74             # (if supplied) or nothing
75 5 100       11 $modifier = '' unless $modifier;
76 5         9 my $nick = $self->nick;
77 5         11 my $host = $self->host;
78 5         14 $command =~ s#{{mod}}#$modifier#;
79 5         7 $command =~ s#{{nick}}#$nick#;
80 5         14 $command =~ s#{{host}}#$host#;
81              
82             # If we have a command, tell SSH to execute it remotely via '-t'
83 5         15 return "-t $command";
84             }
85              
86             1;
87             __END__