Neaf/CLI.pm

filter: Statementstmt 85.4% 82 / 96 Branchbran 50.0% 20 / 40 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 74.8% 125 / 167 CC 28 SCAR 34.2
CC
28
Cov
73%
run_test
67.2
__ANON__
5.3
run
5.0
CRAP 30.5
« Neaf/X/Session/SQL.pm ↑ Summary Neaf/X/Session/Base.pm »
1 package MVC::Neaf::CLI;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.2901';
6  
7 =head1 NAME
8  
9 MVC::Neaf::CLI - Command line debugger and runner for Not Even A Framework
10  
11 =head1 DESCRIPTION
12  
13 Run your applications from command line, with various overrides.
14  
15 May be useful for command-line mode debugging (think CGI.pm)
16 as well as starting the app from command line.
17  
18 =head1 SYNOPSIS
19  
20 perl application.pl --list
21  
22 Print routes defined in the application.
23  
24 perl application.pl --post /foo/bar arg=42
25  
26 Simulate a request without running a server.
27  
28 perl application.pl --listen :5000
29  
30 Run a psgi server.
31  
32 =head1 OPTIONS
33  
34 =over
35  
36 =item * --help - display a brief usage message.
37  
38 =item * --list - print routes configured in the application.
39  
40 =item * --listen <port-or-socket> - start application as a standalone
41 plack servers. Any subsequent options compatible with plackup(1)
42 are allowed in this mode.
43  
44 =item * --post - set method to POST.
45  
46 =item * --method METHOD - set method to anything else.
47  
48 =item * --upload id=/path/to/file - add upload. Requires --post.
49  
50 =item * --cookie name="value" - add cookie.
51  
52 =item * --header name="value" - set http header.
53  
54 =item * --view - force (JS,TT,Dumper) view.
55  
56  
57 =back
58  
59 =head2 METHODS
60  
61 The usage doesn't expect these are called directly.
62  
63 But just for the sake of completeness...
64  
65 =cut
66  
67 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use HTTP::Headers::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 2 ▶use MVC::Neaf;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶use MVC::Neaf::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 =head2 run( $app )
76  
77 Run the application.
78 This reads command line options, as shown in the summary above.
79  
80 $app is an MVC::Neaf object.
81  
82 B<NOTE> Spoils @AGRV.
83  
84 =cut
85  
86 5 ▶sub run {
Subroutine run: called CC=5 SCAR=16.1
Pod documented
87 5 my ($self, $app) = @_;
88  
89 5 my %test;
90  
91 5 ▶ if (grep { $_ eq '--list' } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "--list";} @ARGV) 1 4 5
92 1 return $self->list($app);
93 };
94 4 ▶ if (grep { $_ eq '--help' } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "--help";} @ARGV) 1 3 4
95 1 return usage();
96 };
97  
98 # TODO 0.30 --view here so that view is forced in both modes
99 3 ▶ if (grep { $_ =~ /^--listen/ } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ =~ /^--listen/;} @ARGV) 1 2 3
100 1 return $self->serve( $app );
101 };
102  
103 GetOptions(
104 1 ▶ "post" => sub { $test{method} = 'POST' },
Subroutine __ANON__: called CC=1 SCAR=0.0
105 "method=s" => \$test{method},
106 "upload=s@" => \$test{upload},
107 "cookie=s@" => \$test{cookie},
108 "header=s@" => \$test{head},
109 "view=s" => \$test{view},
110 # TODO 0.30 --session to reduce hassle
111 2 ▶ ) or croak "Bad command line options in MVC::Neaf::CLI, see $0 --help";
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("post", sub { $test{"method"} = "POST"; } , "method=s", \$test{"method"}, "upload=s\@", \$test{"upload"}, "cookie=s\@", \$test{"cookie"}, "header=s\@", \$test{"head"}, "view=s", \$test{"view"}) or croak("Bad command line options in MVC::Neaf::CLI, see $0 --help") 2 0 2
112  
113 2 return $self->run_test($app, %test);
114 };
115  
116 =head2 serve( $app, @arg )
117  
118 Use L<Plack::Runner> to start server.
119  
120 =cut
121  
122 1 ▶sub serve {
Subroutine serve: called CC=1 SCAR=0.0
Pod documented
123 1 my ($self, $app) = @_;
124  
125 1 require Plack::Runner;
126 1 my $runner = Plack::Runner->new;
127 1 $runner->parse_options( @ARGV );
128 1 $runner->run( $app->run );
129 };
130  
131 =head2 run_test( $app, %override )
132  
133 Call L<MVC::Neaf>'s C<run_test>.
134  
135 =cut
136  
137 2 ▶sub run_test {
Subroutine run_test: called CC=18 SCAR=42.1
Pod documented
138 2 my ($self, $app, %test) = @_;
139  
140 2 ▶ $test{method} = uc $test{method} if $test{method};
Branch: 1 branch
BranchTrueFalseTotal
if $test{"method"} 1 1 2
141  
142 croak "--upload requires --post"
143 2 ▶ if $test{upload} and $test{method} ne 'POST';
Branch: 1 branch
BranchTrueFalseTotal
if $test{"upload"} and $test{"method"} ne "POST" 0 2 2
Condition33%1/3
$test{"upload"} and $test{"method"} ne "POST"
!ll&&!rl&&r
200
MC/DC0%0/2
$test{"upload"} and $test{"method"} ne "POST"
$test{"upload"} $test{"method"} ne "POST"
Truth table33%1/3
A and B
A= $test{"upload"}B= $test{"method"} ne "POST"
ABresult
0X0
100
111
144  
145 2 ▶ if (my $up = delete $test{upload}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $up = delete $test{"upload"}) 0 2 2
146 0 foreach (@$up) {
147 0 ▶ /^(\S+?)=(.+)$/ or croak "Usage: --upload key=/path/to/file";
Branch: 1 branch
BranchTrueFalseTotal
/^(\S+?)=(.+)$/ or croak("Usage: --upload key=/path/to/file") 0 0 0
148 0 my ($key, $file) = ($1, $2);
149  
150 0 ▶ open my $fd, "<", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "<", $file or die "Failed to open upload $key file ${file}: $!" 0 0 0
151 or die "Failed to open upload $key file $file: $!";
152  
153 # TODO 0.30 create temp file
154 0 $test{uploads}{$key} = MVC::Neaf::Upload->new(
155 id => $key, handle => $fd, filename => $file );
156 };
157 };
158  
159 2 ▶ if (my $cook = delete $test{cookie}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cook = delete $test{"cookie"}) 0 2 2
160 0 foreach (@$cook) {
161 0 ▶ /^(\S+?)=(.*)$/
Branch: 1 branch
BranchTrueFalseTotal
/^(\S+?)=(.*)$/ or croak("Usage: --cookie name=value") 0 0 0
162 or croak "Usage: --cookie name=value";
163 0 $test{cookie}{$1} = $2;
164 };
165 };
166  
167 2 ▶ if (my @head = @{ delete $test{head} || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@head) = @{delete $test{"head"} or [];}) 0 2 2
Condition50%1/2
delete $test{'head'} || []
l!l
02
MC/DC0%0/1
delete $test{'head'} || []
delete $test{'head'}
Truth table50%1/2
A
A= delete $test{'head'}
Aresult
11
00
168 $test{header_in} = HTTP::Headers::Fast->new (
169 0 ▶ map { /^([^=]+)=(.*)$/ or croak "Bad header format"; $1=>$2 } @head
Branch: 1 branch
BranchTrueFalseTotal
/^([^=]+)=(.*)$/ or croak("Bad header format") 0 0 0
170 );
171 };
172  
173 2 my ($path, @rest) = @ARGV;
174 2 ▶ $path ||= '/';
Condition50%1/2
$path ||= "/"
l!l
20
MC/DC0%0/1
$path ||= "/"
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
175 2 ▶ if (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 0 2 2
176 0 ▶ my $sep = $path =~ /\?/ ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /\?/ ? : 0 0 0
177 0 $path .= $sep . join '&', @rest;
178 };
179  
180 2 ▶ if (my $view = delete $test{view}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $view = delete $test{"view"}) 1 1 2
181 1 $app->set_forced_view( $view );
182 };
183  
184 2 my ($status, $head, $content) = $app->run_test( $path, %test );
185  
186 2 print STDOUT "Status $status\n";
187 2 print STDOUT $head->as_string, "\n";
188 2 print STDOUT $content;
189 };
190  
191 =head2 usage()
192  
193 Display help message.
194  
195 =cut
196  
197 1 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
198 1 my $script = basename($0);
199  
200 1 print <<"USAGE";
201 $script
202 is a web-application powered by Perl and MVC::Neaf (Not Even A Framework).
203 It will behave according to the CGI spec if run without parameters.
204 It will return a PSGI-compliant subroutine if require'd from other Perl code.
205 To run it as a standalone server, use --listen switch along with any
206 other switches recognized by plackup(1)
207 perl $script --listen :31415 <...>
208 To peek at the application, run
209 perl $script --list
210 To get this summary, run
211 perl $script --help
212 To invoke debugging mode, run:
213 perl $script [options] [/path] <param=value> ...
214 Options may include:
215 --post - force request method to POST
216 --method METHOD - force method to anything else
217 --upload id=/path/to/file - add upload. Requires --post.
218 --cookie name="value" - add cookie.
219 --header name="value" - set http header.
220 --view - force (JS,TT,Dumper) view.
221 See `perldoc MVC::Neaf::CLI` for more.
222 USAGE
223  
224 };
225  
226 =head2 list()
227  
228 List registered Neaf routes.
229  
230 =cut
231  
232 1 ▶sub list {
Subroutine list: called CC=3 SCAR=11.2
Pod documented
233 1 my ($self, $app) = @_;
234  
235 1 my %inverse_descr; # {path+printable descr} = [method, method]
236  
237 6 ▶ my $routes = $app->get_routes( sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
238 6 my ($route, $path, $method) = @_;
239  
240 6 my @features;
241 # TODO 0.30 call methods instead of ->{}
242 6 ▶ if ( my $rex = $route->{path_info_regex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rex = $route->{"path_info_regex"}) 6 0 6
243 6 $rex = "$rex";
244 6 ▶ $rex =~ m#^\(.*?\((.*)\).*?\)$# and $rex = $1;
Branch: 1 branch
BranchTrueFalseTotal
$rex =~ /^\(.*?\((.*)\).*?\)$/ and $rex = $1 0 6 6
245 6 push @features, "/$rex"
246 };
247 0 my $param = join "&", map { "$_=$route->{param_regex}{$_}" }
248 6 sort keys %{ $route->{param_regex} };
249 6 ▶ push @features, "?$param" if $param;
Branch: 1 branch
BranchTrueFalseTotal
if $param 0 6 6
250  
251 push @features, " # $route->{description}"
252 6 ▶ if $route->{description};
Branch: 1 branch
BranchTrueFalseTotal
if $route->{"description"} 0 6 6
253  
254 6 my $descr = join "", $path, @features;
255  
256 6 push @{ $inverse_descr{$descr} }, $method;
257 1 } );
258  
259 # Convert available methods to printable format
260 1 $_ = join ",", sort @$_ for values %inverse_descr;
261  
262 1 foreach (sort keys %inverse_descr) {
263 2 printf "[%s] %s\n", $inverse_descr{$_}, $_;
264 };
265 };
266  
267 =head1 LICENSE AND COPYRIGHT
268  
269 This module is part of L<MVC::Neaf> suite.
270  
271 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
272  
273 This program is free software; you can redistribute it and/or modify it
274 under the terms of either: the GNU General Public License as published
275 by the Free Software Foundation; or the Artistic License.
276  
277 See L<http://dev.perl.org/licenses/> for more information.
278  
279 =cut
280  
281 2 1;
« Neaf/X/Session/SQL.pm ↑ Summary Neaf/X/Session/Base.pm »