Debian Codegraph @

bin/mrest-cli

raw
1 #!/usr/bin/perl
2 # *************************************************************************
3 # Copyright (c) 2014-2015-2015, SUSE LLC
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright notice,
11 # this list of conditions and the following disclaimer.
12 #
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 #
17 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without
19 # specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 # *************************************************************************
33 #
34 # Dochazka CLI script
35 #
36 use 5.012;
37 use strict;
38 use warnings;
39
40 use App::CELL qw( $CELL $log $site $meta );
41 use Data::Dumper;
42 use File::ShareDir;
43 use File::Spec;
44 use Getopt::Long 2.32;
45 use Log::Any::Adapter;
46 use Pod::Usage;
47 use Term::ReadLine;
48 use Try::Tiny;
49 use Web::MREST::CLI qw( init_cli_client normalize_filespec );
50 use Web::MREST::CLI::Parser;
51
52 my $Parser = 'Web::MREST::CLI::Parser';
53
54 local $Data::Dumper::Terse = 1;
55
56
57
58
59 #
60 # logger initialization routine
61 #
62 sub init_logger {
63
64 my $logfile = normalize_filespec( $site->MREST_CLI_LOG_FILE );
65 print( "Log file is $logfile\n" );
66 unlink $logfile if $site->MREST_CLI_LOG_FILE_RESET;
67 # -----------------------------------------------------------
68 # -- to debug configuration file loading issues, uncomment --
69 # -- this and call init_logger() before $CELL->load(...) --
70 # -----------------------------------------------------------
71 #my $logfile = normalize_filespec( "mrest-cli.log" );
72 #unlink $logfile;
73
74 Log::Any::Adapter->set('File', $logfile);
75 $log->init( ident => 'mrest-cli', debug_mode => 1 );
76 $log->debug( 'Logger initialized' );
77 }
78
79 #
80 # CLI client initialization routine: might die
81 #
82 sub init {
83 my ( $early_debug ) = @_;
84
85 die if defined( $site->MREST_CLI_LOG_FILE );
86
87 my $status = init_cli_client( distro => 'Web-MREST-CLI', early_debug => $early_debug );
88 return $status unless $status->ok;
89
90 die unless defined( $site->MREST_CLI_LOG_FILE );
91
92 if ( ! ( $meta->MREST_CLI_URI_BASE ) ) {
93 $meta->set( 'MREST_CLI_URI_BASE', 'http://localhost:5000' );
94 }
95 print "Using URI base " . $meta->MREST_CLI_URI_BASE . "\n";
96 print "Hopefully, a server is listening there. . .\n";
97
98 # initialize CLI-specific logger
99 init_logger();
100
101 return $CELL->status_ok;
102 }
103
104 #
105 # get prompt
106 #
107 sub get_prompt { "$Parser> " }
108
109
110 # -------------------------------------------------------------------------
111 # main
112 # -------------------------------------------------------------------------
113
114 # process command-line options
115 my $help = 0;
116 my $sitedir;
117 my $early_debug;
118 GetOptions(
119 'help|?' => \$help,
120 'early-debug|e=s' => \$early_debug,
121 );
122 pod2usage(1) if $help;
123
124 if ( $early_debug ) {
125 print "Early debugging activated; will log early messages to $early_debug\n";
126 }
127
128 # initialize CLI client
129 my $status;
130 if ( ( my $status = init( $early_debug ) )->not_ok ) {
131 print '(' . $status->level . ') ' . $status->text . "\n";
132 exit;
133 }
134
135 my $term = new Term::ReadLine 'mrest-cli';
136
137 binmode STDOUT, ":utf8";
138
139 my $cmd;
140 while ( defined ( $cmd = $term->readline( get_prompt() ) ) ) {
141 my @tokens = split /\s+/, $cmd;
142 next unless @tokens;
143 #print join( ' ', @tokens ), "\n";
144 # parse_tokens will die with the status
145 try {
146 Web::MREST::CLI::Parser::parse_tokens( [], \@tokens );
147 } catch {
148 $status = $_;
149 };
150 if ( ref( $status ) ne 'App::CELL::Status' ) {
151 $status = $CELL->status_crit( 'MREST_CLI_PARSER_DECEASED', payload => $status );
152 }
153 last if $status->code eq 'MREST_CLI_EXIT';
154 if ( $status->code ne 'MREST_CLI_PARSE_ERROR' ) {
155 print "HTTP status: " . ( delete $status->{'http_status'} || '<NONE>' ) . "\n";
156 print "Non-suppressed headers: " . Dumper( $status->{'headers'} ) if $status->{'headers'};
157 delete $status->{'headers'};
158 my $expurgated_status = $status->expurgate;
159 #print Dumper( $expurgated_status );
160 print "Response: " . Dumper( $expurgated_status ) . "\n";
161 } else {
162 print( ( $status->code eq 'MREST_CLI_PARSE_ERROR' )
163 ? $status->text . "\n"
164 : $status->code . ' (' . $status->level . ') ' . $status->text . "\n" );
165 }
166 }
167
168 __END__
169
170 =head1 NAME
171
172 mrest-cli - Web::MREST demo/testing command-line client
173
174
175 =head1 SYNOPSIS
176
177 $ mrest-cli -h
178 --help -h Get help
179 --sitedir -s Specify sitedir (defaults to none)
180
181 $ mrest-cli
182 Web::MREST> get bugreport
183 DISPATCH_BUGREPORT (OK) See payload for bug reporting instructions
184 HTTP status: 200 OK
185 Non-suppressed headers: {
186 'X-Web-Machine-Trace' => 'b13,b12,b11,b10,b9,b8,b7,b6,b5,b4,b3,c3,c4,d4,e5,f6,g7,g8,h10,i12,l13,m16,n16,o16,o18,o18b'
187 }
188 Response: {
189 'report_bugs_to' => '[email protected]'
190 }
191
192
193 =head1 DESCRIPTION
194
195 This is the L<Web::MREST> demo/testing command line interface (CLI). It enables
196 the user to generate HTTP requests (GET, PUT, POST, DELETE) to the REST server
197 and view the server's responses. Each REST resource has a documented CLI syntax
198 that can be viewed by querying the REST server -- e.g., via a web browser.
199
200 For more information, see L<http://metacpan.org/pod/Web::MREST>.
201