blib/lib/Clustericious/Client/Command.pm

filter: Statementstmt 46.6% 70 / 150 Branchbran 26.7% 23 / 86 Conditioncond 22.2% 16 / 72 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 81.2% 13 / 16 Podpod 100% 1 / 1 totaltotal 33.9% 123 / 362 CC 70 SCAR 64.9
CC
70
Cov
35%
run
971.4
_usage
42.0
_load_yaml
30.0
CRAP 657.3
↑ Summary blib/lib/Clustericious/RouteBuilder/Search.pm »
1 package Clustericious::Client::Command;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Command line type processing for clients.
7 22 our $VERSION = '1.29'; # VERSION
8  
9  
10 22 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use YAML::XS qw(Load Dump LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use Log::Log4perl qw/:easy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Data::Rmap qw/rmap_ref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 ▶use Clustericious::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 22 ▶use Clustericious::Client::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 0 ▶sub _usage {
Subroutine _usage: not called CC=6 SCAR=37.4
21 0 my $class = shift;
22 0 my $client = shift;
23 0 my $msg = shift;
24 0 my $routes = Clustericious::Client::Meta->routes(ref $client);
25 0 my $objects = Clustericious::Client::Meta->objects(ref $client);
26 0 ▶ print STDERR $msg,"\n" if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
27 0 print STDERR "Usage:\n";
28 0 my $name = basename($0);
29 0 ▶ print STDERR <<EOPRINT if $routes && @$routes;
Branch: 1 branch
BranchTrueFalseTotal
if $routes and @$routes 0 0 0
Condition0%0/3
$routes and @$routes
!ll&&!rl&&r
000
MC/DC0%0/2
$routes and @$routes
$routes @$routes
Truth table0%0/3
A and B
A= $routesB= @$routes
ABresult
0X0
100
111
30 0 @{[ join "\n", map " $name [opts] $_->[0] $_->[1]", @$routes ]}
31 EOPRINT
32 0 ▶ print STDERR <<EOPRINT if $objects && @$objects;
Branch: 1 branch
BranchTrueFalseTotal
if $objects and @$objects 0 0 0
Condition0%0/3
$objects and @$objects
!ll&&!rl&&r
000
MC/DC0%0/2
$objects and @$objects
$objects @$objects
Truth table0%0/3
A and B
A= $objectsB= @$objects
ABresult
0X0
100
111
33 $name [opts] <object>
34 $name [opts] <object> <keys>
35 $name [opts] search <object> [--key value]
36 $name [opts] create <object> [<filename list>]
37 $name [opts] update <object> <keys> [<filename>]
38 $name [opts] delete <object> <keys>
39  
40 where "opts" are as described in Clustericious::Log::CommandLine, or
41 may be "--remote <remote>" to specify a remote to use from the
42 config file (see Clustericious::Client).
43  
44 and "<object>" may be one of the following :
45 0 @{[ join "\n", map " $_->[0] $_->[1]", @$objects ]}
46  
47 EOPRINT
48  
49 0 exit 0;
50 }
51  
52  
53 22 our $Ssh = "ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o PasswordAuthentication=no";
54 0 ▶sub _expand_remote_glob {
Subroutine _expand_remote_glob: not called CC=4 SCAR=30.0
55 # Given a glob, e.g. omidev.gsfc.nasa.gov:/devsips/app/*/doc/Description.txt
56 # Return a list of filenames with the host prepended to each one, e.g.
57 # omidev.gsfc.nasa.gov:/devsips/app/foo-1/doc/Description.txt
58 # omidev.gsfc.nasa.gov:/devsips/app/bar-2/doc/Description.txt
59 0 my $pattern = shift;
60 0 ▶ return ( $pattern ) unless $pattern =~ /^(\S+):(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern =~ /^(\S+):(.*)$/ 0 0 0
61 0 my ($host,$file) = ( $1, $2 );
62 0 ▶ return ( $pattern ) unless $file =~ /[*?]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /[*?]/ 0 0 0
63 0 INFO "Remote glob : $host:$file";
64 0 my $errs = File::Temp->new();
65 0 my @filenames = `$Ssh $host ls $file 2>$errs`;
66 0 ▶ LOGDIE "Error ssh $host ls $file returned (code $?)".`tail -2 $errs` if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
67 0 return map "$host:$_", @filenames;
68 }
69  
70 0 ▶sub _load_yaml {
Subroutine _load_yaml: not called CC=5 SCAR=34.0
71 # _load_yaml can take a local filename or a remote ssh host + filename and
72 # returns parsed yaml content.
73 0 my $filename = shift;
74  
75 0 ▶ unless ($filename =~ /^(\S+):(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename =~ /^(\S+):(.*)$/) 0 0 0
76 0 INFO "Loading $filename";
77 0 ▶ my $parsed = LoadFile($filename) or LOGDIE "Invalid YAML : $filename\n";
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = LoadFile($filename) or LOGDIE("Invalid YAML : $filename\n") 0 0 0
78 0 return $parsed;
79 }
80  
81 0 my ($host,$file) = ($1,$2);
82 0 INFO "Loading remote file $file from $host";
83 0 my $errs = File::Temp->new();
84 0 my $content = `$Ssh $host cat $file 2>$errs`;
85 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
86 0 LOGDIE "Error (code $?) running ssh $host cat $file : ".`tail -2 $errs`;
87 }
88 0 ▶ my $parsed = Load($content) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = Load($content) or do { ERROR("Invalid YAML: $filename"); return } 0 0 0
89 0 ERROR "Invalid YAML: $filename";
90 0 return;
91 };
92 0 return $parsed;
93 }
94  
95 6 ▶sub run {
Subroutine run: called CC=57 SCAR=68.8
Pod documented
96 6 my $class = shift;
97 6 my $client = shift;
98 6 ▶ my @args = @_ ? @_ : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 0 6
99  
100 6 ▶ return $class->_usage($client) if !$args[0] || $args[0] =~ /help/;
Branch: 1 branch
BranchTrueFalseTotal
if not $args[0] or $args[0] =~ /help/ 0 6 6
Condition33%1/3
not $args[0] or $args[0] =~ /help/
l!l&&r!l&&!r
006
MC/DC0%0/2
not $args[0] or $args[0] =~ /help/
not $args[0] $args[0] =~ /help/
Truth table33%1/3
not(A) or B
A= not $args[0]B= $args[0] =~ /help/
ABresult
1X1
011
000
101  
102 # Preprocessing for any common args, e.g. --remote
103 6 my $arg;
104 ARG :
105 6 ▶ while ($arg = shift @args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg = shift @args) 6 0 6
106 6 for ($arg) {
107 6 ▶ /--remote/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/--remote/ and do { my $remote = shift @args; TRACE("Using remote $remote"); $client->remote($remote); next ARG } 0 6 6
108 0 my $remote = shift @args;
109 0 TRACE "Using remote $remote";
110 0 $client->remote($remote);
111 0 next ARG;
112 };
113 6 last ARG;
114 }
115 }
116  
117 6 ▶ my $method = $arg or $class->_usage($client);
Branch: 1 branch
BranchTrueFalseTotal
my $method = $arg or $class->_usage($client) 6 0 6
118  
119 # Map some alternative command line forms.
120 6 my $try_stdin;
121 6 ▶ if ( $method eq 'create' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "create") 0 6 6
122 0 ▶ $method = shift @args or $class->_usage( $client, "Missing <object>" );
Branch: 1 branch
BranchTrueFalseTotal
$method = shift @args or $class->_usage($client, "Missing <object>") 0 0 0
123 0 $try_stdin = 1;
124 }
125  
126 6 ▶ if ( $method =~ /^(delete|search)$/ ) { # e.g. search -> app_search
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(delete|search)$/) 0 6 6
127 0 $method = ( shift @args ) . '_' . $method;
128 }
129  
130 6 ▶ unless ($client->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client->can($method)) 0 6 6
131 0 $class->_usage($client, "Unrecognized argument : $method");
132 0 return;
133 }
134  
135 6 my $meta = Clustericious::Client::Meta::Route->new(
136 route_name => $method,
137 client_class => ref $client
138 );
139  
140 6 ▶ if ($meta->get('args')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->get("args")) 5 1 6
141 # No heuristics for args.
142  
143 5 my $obj = $client->$method({ command_line => 1 }, @args);
144  
145 3 ▶ ERROR $client->errorstring if $client->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $client->has_error 0 3 3
146  
147 # Copied from below, until that code is deprecated.
148 3 ▶ if ( blessed($obj) && $obj->isa("Mojo::Transaction") ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($obj) and $obj->isa('Mojo::Transaction')) { } 0 3 3
elsif (ref $obj eq 'HASH' and keys %$obj == 1 and $obj->{'text'}) { } 0 3 3
elsif ($client->tx and $client->tx->req->method eq 'POST' and $meta->get('quiet_post')) { } 0 3 3
Condition33%1/3
builtin::blessed($obj) and $obj->isa("Mojo::Transaction")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Mojo::Transaction")
builtin::blessed($obj) $obj->isa("Mojo::Transaction")
Truth table33%1/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Mojo::Transaction")
ABresult
0X0
100
111
149 0 ▶ if ( my $res = $obj->success ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $obj->success) { } 0 0 0
150 0 print $res->code," ",$res->default_message,"\n";
151 } else {
152 0 my ( $message, $code ) = $obj->error;
153 0 ▶ ERROR $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
154 0 ERROR $message;
155 }
156 ▶ } elsif (ref $obj eq 'HASH' && keys %$obj == 1 && $obj->{text}) {
Condition33%2/6
ref $obj eq "HASH" and keys %$obj == 1 and $obj->{"text"}
!ll&&!rl&&r
300
ref $obj eq "HASH" and keys %$obj == 1
!ll&&!rl&&r
300
MC/DC0%0/3
ref $obj eq "HASH" and keys %$obj == 1 and $obj->{"text"}
ref $obj eq "HASH" keys %$obj == 1 $obj->{"text"}
Truth table25%1/4
A and B and C
A= ref $obj eq "HASH"B= keys %$obj == 1C= $obj->{"text"}
ABCresult
0XX0
10X0
1100
1111
157 0 print $obj->{text};
158 ▶ } elsif ($client->tx && $client->tx->req->method eq 'POST' && $meta->get("quiet_post")) {
Condition33%2/6
$client->tx and $client->tx->req->method eq "POST" and $meta->get("quiet_post")
!ll&&!rl&&r
300
$client->tx and $client->tx->req->method eq "POST"
!ll&&!rl&&r
300
MC/DC0%0/3
$client->tx and $client->tx->req->method eq "POST" and $meta->get("quiet_post")
$client->tx $client->tx->req->method eq "POST" $meta->get("quiet_post")
Truth table25%1/4
A and B and C
A= $client->txB= $client->tx->req->method eq "POST"C= $meta->get("quiet_post")
ABCresult
0XX0
10X0
1100
1111
159 0 my $msg = $client->res->code." ".$client->res->default_message;
160 0 my $got = $client->res->json;
161 0 ▶ if ($got && ref $got eq 'HASH' and keys %$got==1 && $got->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got and ref $got eq "HASH" and (keys %$got == 1 and $got->{"text"})) 0 0 0
Condition0%0/9
$got and ref $got eq "HASH" and (keys %$got == 1 and $got->{"text"})
!ll&&!rl&&r
000
$got and ref $got eq "HASH"
!ll&&!rl&&r
000
keys %$got == 1 and $got->{"text"}
!ll&&!rl&&r
000
MC/DC0%0/4
$got and ref $got eq "HASH" and (keys %$got == 1 and $got->{"text"})
$got ref $got eq "HASH" keys %$got == 1 $got->{"text"}
Truth table0%0/5
A and B and C and D
A= $gotB= ref $got eq "HASH"C= keys %$got == 1D= $got->{"text"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
162 0 $msg .= " ($got->{text})";
163 }
164 0 INFO $msg;
165 } else {
166 3 print _prettyDump($obj);
167 }
168 3 return;
169 }
170  
171 # Code below here should be deprecated, these are various heuristics for argument processing.
172  
173 1 my @extra_args = ( '/dev/null' );
174 1 my $have_filenames;
175  
176 # Assume we have files and/or remote globs
177 1 ▶ if ( !$meta->get('dont_read_files') && @args > 0 && ( -r $_[-1] || $_[-1] =~ /^\S+:/ ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $meta->get('dont_read_files') and @args > 0 and -r $_[-1] || $_[-1] =~ /^\S+:/) { } 0 1 1
elsif ($try_stdin and -r STDIN and @args == 0) { } 0 2 2
Condition33%3/9
not $meta->get("dont_read_files") and @args > 0 and -r $_[-1] || $_[-1] =~ /^\S+:/
!ll&&!rl&&r
010
not $meta->get("dont_read_files") and @args > 0
!ll&&!rl&&r
001
-r $_[-1] or $_[-1] =~ /^\S+:/
l!l&&r!l&&!r
001
MC/DC0%0/4
not $meta->get("dont_read_files") and @args > 0 and -r $_[-1] || $_[-1] =~ /^\S+:/
not $meta->get("dont_read_files") @args > 0 -r $_[-1] $_[-1] =~ /^\S+:/
Truth table20%1/5
not(A) and B and C or D
A= not $meta->get("dont_read_files")B= @args > 0C= -r $_[-1]D= $_[-1] =~ /^\S+:/
ABCDresult
0XXX0
10XX0
11000
111X1
11011
178 0 $have_filenames = 1;
179 0 @extra_args = ();
180 0 ▶ while (my $arg = pop @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = pop @args) 0 0 0
181 0 ▶ if ($arg =~ /^\S+:/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ /^\S+:/) { } 0 0 0
elsif (-e $arg) { } 0 0 0
182 0 push @extra_args, _expand_remote_glob($arg);
183 } elsif (-e $arg) {
184 0 push @extra_args, $arg;
185 } else {
186 0 LOGDIE "Do not know how to interpret argument : $arg";
187 }
188 }
189 ▶ } elsif ( $try_stdin && (-r STDIN) && @args==0) {
Condition33%2/6
$try_stdin and -r STDIN and @args == 0
!ll&&!rl&&r
100
$try_stdin and -r STDIN
!ll&&!rl&&r
100
MC/DC0%0/3
$try_stdin and -r STDIN and @args == 0
$try_stdin -r STDIN @args == 0
Truth table25%1/4
A and B and C
A= $try_stdinB= -r STDINC= @args == 0
ABCresult
0XX0
10X0
1100
1111
190 0 my $content = join '', <STDIN>;
191 0 $content = Load($content);
192 0 ▶ LOGDIE "Invalid yaml content in $method" unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
193 0 push @args, $content;
194 }
195  
196 # Finally, run :
197 1 for my $arg (@extra_args) {
198 1 my $obj;
199 1 ▶ if ($have_filenames) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_filenames) { } 0 1 1
200 0 $obj = $client->$method(@args, _load_yaml($arg));
201 } else {
202 1 $obj = $client->$method(@args);
203 }
204 1 ▶ ERROR $client->errorstring if $client->errorstring;
Branch: 1 branch
BranchTrueFalseTotal
if $client->errorstring 0 1 1
205 1 ▶ next unless $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 0 1 1
206  
207 1 ▶ if ( blessed($obj) && $obj->isa("Mojo::Transaction") ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($obj) and $obj->isa('Mojo::Transaction')) { } 0 1 1
elsif (ref $obj eq 'HASH' and keys %$obj == 1 and $obj->{'text'}) { } 0 1 1
elsif ($client->tx and $client->tx->req->method eq 'POST' and $meta->get('quiet_post')) { } 0 1 1
Condition33%1/3
builtin::blessed($obj) and $obj->isa("Mojo::Transaction")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Mojo::Transaction")
builtin::blessed($obj) $obj->isa("Mojo::Transaction")
Truth table33%1/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Mojo::Transaction")
ABresult
0X0
100
111
208 0 ▶ if ( my $res = $obj->success ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $obj->success) { } 0 0 0
209 0 print $res->code," ",$res->default_message,"\n";
210 } else {
211 0 my ( $message, $code ) = $obj->error;
212 0 ▶ ERROR $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
213 0 ERROR $message;
214 }
215 ▶ } elsif (ref $obj eq 'HASH' && keys %$obj == 1 && $obj->{text}) {
Condition33%2/6
ref $obj eq "HASH" and keys %$obj == 1 and $obj->{"text"}
!ll&&!rl&&r
100
ref $obj eq "HASH" and keys %$obj == 1
!ll&&!rl&&r
100
MC/DC0%0/3
ref $obj eq "HASH" and keys %$obj == 1 and $obj->{"text"}
ref $obj eq "HASH" keys %$obj == 1 $obj->{"text"}
Truth table25%1/4
A and B and C
A= ref $obj eq "HASH"B= keys %$obj == 1C= $obj->{"text"}
ABCresult
0XX0
10X0
1100
1111
216 0 print $obj->{text};
217 ▶ } elsif ($client->tx && $client->tx->req->method eq 'POST' && $meta->get("quiet_post")) {
Condition33%2/6
$client->tx and $client->tx->req->method eq "POST" and $meta->get("quiet_post")
!ll&&!rl&&r
100
$client->tx and $client->tx->req->method eq "POST"
!ll&&!rl&&r
100
MC/DC0%0/3
$client->tx and $client->tx->req->method eq "POST" and $meta->get("quiet_post")
$client->tx $client->tx->req->method eq "POST" $meta->get("quiet_post")
Truth table25%1/4
A and B and C
A= $client->txB= $client->tx->req->method eq "POST"C= $meta->get("quiet_post")
ABCresult
0XX0
10X0
1100
1111
218 0 my $msg = $client->res->code." ".$client->res->default_message;
219 0 my $got = $client->res->json;
220 0 ▶ if ($got && ref $got eq 'HASH' and keys %$got==1 && $got->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got and ref $got eq "HASH" and (keys %$got == 1 and $got->{"text"})) 0 0 0
Condition0%0/9
$got and ref $got eq "HASH" and (keys %$got == 1 and $got->{"text"})
!ll&&!rl&&r
000
$got and ref $got eq "HASH"
!ll&&!rl&&r
000
keys %$got == 1 and $got->{"text"}
!ll&&!rl&&r
000
MC/DC0%0/4
$got and ref $got eq "HASH" and (keys %$got == 1 and $got->{"text"})
$got ref $got eq "HASH" keys %$got == 1 $got->{"text"}
Truth table0%0/5
A and B and C and D
A= $gotB= ref $got eq "HASH"C= keys %$got == 1D= $got->{"text"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
221 0 $msg .= " ($got->{text})";
222 }
223 0 INFO $msg;
224 } else {
225 1 print _prettyDump($obj);
226 }
227 }
228 1 return;
229 }
230  
231 4 ▶sub _prettyDump {
Subroutine _prettyDump: called CC=1 SCAR=0.0
232 4 my $what = shift;
233 4 ▶ rmap_ref { $_ = $_->iso8601() if ref($_) eq 'DateTime' } $what;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "DateTime" 0 12 12
234 4 return Dump($what);
235 }
236  
237  
238 22 1;
239  
240 __END__
↑ Summary blib/lib/Clustericious/RouteBuilder/Search.pm »