blib/lib/SSH/RPC/Shell.pm

filter: Statementstmt 42.4% 14 / 33 Branchbran 10.0% 1 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod 100% 3 / 3 totaltotal 43.1% 22 / 51 SCAR 27.7
CC
6
Cov
35%
run
30.0
processRequest
2.1
BEGIN
1.0
CRAP 15.9
↑ Summary Build »
1 package SSH::RPC::Shell;
2  
3 1 our $VERSION = 1.201;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 SSH::RPC::Shell - The shell, or server side, of an RPC call over SSH.
11  
12 =head1 SYNOPSIS
13  
14 To make your own shell with it's own methods:
15  
16 package MyShell;
17  
18 use base 'SSH::RPC::Shell';
19  
20 sub run_time {
21 my ($class, $args) = @_;
22 return {
23 status => 200,
24 response => time(),
25 };
26 }
27  
28 1;
29  
30  
31 To create a usuable shell:
32  
33 #!/usr/bin/perl
34  
35 use strict;
36 use MyShell;
37  
38 MyShell->run();
39  
40  
41 =head1 DESCRIPTION
42  
43 SSH::RPC::Shell allows you to quickly implement your own shells that are remotely callable with L<SSH::RPC::Client>.
44  
45 =head1 METHODS
46  
47 The following methods are available from this class.
48  
49 =cut
50  
51  
52  
53 #-------------------------------------------------------------------
54  
55 =head2 processRequest ( request )
56  
57  
58 =cut
59  
60 sub processRequest {
61 1 ▶ my ($class, $request) = @_;
Subroutine processRequest: called CC=2 SCAR=7.2
Pod documented
62 1 my $command = 'run_'.$request->{command};
63 1 my $args = $request->{args};
64 1 ▶ if (my $sub = $class->can($command)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $class->can($command)) 1 0 1
65 1 return $sub->($args);
66 }
67 0 return { "error" => "Method not allowed.", "status" => "405" };
68 }
69  
70 #-------------------------------------------------------------------
71  
72 =head2 run ()
73  
74 Class method. This method is executed to invoke the shell.
75  
76 =cut
77  
78 sub run {
79 0 ▶ my $class = shift;
Subroutine run: not called CC=5 SCAR=34.0
Pod documented
80 0 my $json = JSON->new->utf8;
81 0 my $request;
82 0 ▶ while (my $line = <STDIN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STDIN)) 0 0 0
83 0 $request = eval {$json->incr_parse($line)};
84 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
85 0 warn $@;
86 0 print '{ "error" : "Malformed request.", "status" : "400" }';
87 0 return;
88 }
89 0 ▶ last if defined $request;
Branch: 1 branch
BranchTrueFalseTotal
if defined $request 0 0 0
90 }
91 0 my $result = $class->processRequest($request);
92 0 $result->{version} = $VERSION;
93 0 my $encodedResult = eval{JSON->new->pretty->utf8->encode($result)};
94 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
95 0 print { "error" => "Malformed response.", "status" => "511" };
96 }
97 else {
98 0 print $encodedResult."\n";
99 }
100 }
101  
102  
103 #-------------------------------------------------------------------
104  
105 =head2 run_noop ()
106  
107 Class method. This subroutine just returns a successful status so you know that communication is working.
108  
109 =cut
110  
111 sub run_noop {
112 3 ▶ return {status=>200};
Subroutine run_noop: called CC=1 SCAR=0.0
Pod documented
113 }
114  
115 =head1 LEGAL
116  
117 -------------------------------------------------------------------
118 SSH::RPC::Client is Copyright 2008-2009 Plain Black Corporation
119 and is licensed under the same terms as Perl itself.
120 -------------------------------------------------------------------
121 http://www.plainblack.com info@plainblack.com
122 -------------------------------------------------------------------
123  
124 =cut
125  
126 1 1;
127  
128  
↑ Summary Build »