blib/lib/Web/MREST/CLI/Parser.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 162 SCAR 69.0
CC
31
Cov
0%
parse_tokens
992.0
BEGIN
2.0
BEGIN
2.0
CRAP 992.0
1 # *************************************************************************
2 # Copyright (c) 2014-2015-2015, SUSE LLC
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 # *************************************************************************
32 #
33 # parser module
34 #
35 package Web::MREST::CLI::Parser;
36  
37 use 5.012;
38 use strict;
39 use warnings;
40  
41 use App::CELL qw( $CELL $log );
42 use Test::Deep::NoTest;
43 use Web::MREST::CLI qw( send_req );
44  
45 our $anything = qr/^.+$/i;
46  
47 =head1 NAME
48  
49 Web::MREST::CLI::Parser - Parser for demo MREST command line client
50  
51  
52  
53  
54 =head1 SYNOPSIS
55  
56 use Try::Tiny;
57
58 my $status;
59 my @tokens = split /\s+/, 'MY SAMPLE COMMAND';
60 try {
61 Web::MREST::CLI::Parse::parse_tokens( [], \@tokens );
62 } catch {
63 $status = $_;
64 };
65  
66  
67  
68  
69 =head1 CLI COMMANDS
70  
71 The parsing of CLI commands takes place in the C<parse_tokens> function,
72 which calls itself recursively until it gets to a rather macabre-sounding
73  
74 die send_req . . .
75  
76 This causes control to return to the while loop in C<bin/mrest-cli> with the
77 return value of the C<send_req>, which is a status object.
78  
79 All tokens should be chosen to be distinguishable by their first
80 three characters.
81  
82 =cut
83  
84 my $method; # store the HTTP method in a package variable so it is remembered
85  
86 sub parse_tokens {
87 my ( $pre, $tokens ) = @_;
88 return $CELL->status_err( "No more tokens" ) unless ref $tokens;
89 my @tokens = @$tokens;
90 my $token = shift @tokens;
91  
92 # the first token designates the HTTP method
93 if ( @$pre == 0 ) { # first token is supposed to be the HTTP method
94  
95 # GET ''
96 if ( $token =~ m/^GET/i ) {
97 $method = 'GET';
98 parse_tokens( [ 'GET' ], \@tokens ) if @tokens;
99 die send_req( 'GET', '' );
100 }
101  
102 # PUT ''
103 elsif ( $token =~ m/^PUT/i ) {
104 $method = 'PUT';
105 parse_tokens( [ 'PUT' ], \@tokens ) if @tokens;
106 die send_req( 'PUT', '' );
107 }
108
109 # POST ''
110 elsif ( $token =~ m/^POS/i ) {
111 $method = 'POST';
112 parse_tokens( [ 'POST' ], \@tokens ) if @tokens;
113 die send_req( 'POST', '' );
114 }
115
116 # DELETE ''
117 elsif ( $token =~ m/^DEL/i ) {
118 $method = 'DELETE';
119 parse_tokens( [ 'DELETE' ], \@tokens ) if @tokens;
120 die send_req( 'DELETE', '' );
121 }
122  
123 # EXIT, QUIT, and the like
124 elsif ( $token =~ m/^(exi)|(qu)|(\\q)/i and eq_deeply( $pre, [] ) ) {
125 die $CELL->status_ok( 'MREST_CLI_EXIT' );
126 }
127  
128 die $CELL->status_err( 'MREST_CLI_PARSE_ERROR' );
129 }
130  
131 # second token represents the resource class ( top-level, employee, priv, etc.)
132 if ( @$pre == 1 ) {
133  
134 #
135 # root resource
136 #
137 if ( $token eq '/' ) {
138 die send_req( $method, '/' );
139 }
140  
141 #
142 #
143 # top-level resource: handle it here
144 #
145 # "/bugreport"
146 if ( $token =~ m/^bug/i ) {
147 die send_req( $method, 'bugreport' );
148 }
149  
150 # "/configinfo"
151 if ( $token =~ m/^con/i ) {
152 die send_req( $method, 'configinfo' );
153 }
154  
155 # "/docu/{pod,html,text} \"$RESOURCE'""
156 if ( $token =~ m/^doc/i ) {
157 if ( @tokens ) {
158 my $pod_html = shift @tokens;
159 my $json = join( ' ', @tokens );
160 if ( $json ) {
161 $json = '"' . $json . '"' unless $json =~ m/^".*"$/;
162 if ( $pod_html =~ m/^pod/i ) {
163 die send_req( $method, 'docu/pod', $json );
164 } elsif ( $pod_html =~ m/^htm/i ) {
165 die send_req( $method, 'docu/html', $json );
166 } elsif ( $pod_html =~ m/^tex/i ) {
167 die send_req( $method, 'docu/text', $json );
168 }
169 } else {
170 print "You should specify a resource\n";
171 }
172 } else {
173 die send_req( $method, 'docu' );
174 }
175 }
176  
177 # "/echo [$JSON]"
178 if ( $token =~ m/^ech/i ) {
179 die send_req( $method, 'echo', join(' ', @tokens) );
180 }
181  
182 # "/noop"
183 if ( $token =~ m/^noo/i ) {
184 die send_req( $method, "noop" );
185 }
186  
187 # "/param $TYPE $JSON"
188 # "/param/:type/:param"
189 if ( $token =~ m/^par/i ) {
190 if ( scalar( @tokens ) > 1 ) {
191 my $type = $tokens[0];
192 my $param = $tokens[1];
193 my $json = join( ' ', @tokens[2..$#tokens] );
194 die send_req( $method, "param/$type/$param", $json );
195 }
196 }
197
198 # "/test/?:specs"
199 if ( $token =~ m/^tes/i ) {
200 my $uri_path;
201 if ( @tokens ) {
202 my $uri_ext .= join( ' ', @tokens ) if @tokens;
203 $uri_path = "test/$uri_ext";
204 } else {
205 $uri_path = "test";
206 }
207 die send_req( $method, $uri_path );
208 }
209  
210 # "/version"
211 if ( $token =~ m/^ver/i and eq_deeply( $pre, [ $method ] ) ) {
212 die send_req( $method, 'version' );
213 }
214  
215 }
216  
217 # we have gone all the way through the state machine without a match
218 die $CELL->status_err( 'MREST_CLI_PARSE_ERROR' );
219 }
220  
221 1;