blib/lib/Catmandu/CLI.pm

filter: Statementstmt 82.1% 101 / 123 Branchbran 67.5% 27 / 40 Conditioncond 66.6% 8 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 95.0% 19 / 20 Podpod 50.0% 5 / 10 totaltotal 76.7% 162 / 211 CC 29 SCAR 23.3
CC
29
Cov
78%
__ANON__
27.1
run
8.7
default_log4perl_config
3.1
CRAP 10.3
« blib/lib/Catmandu/Cmd/copy.pm ↑ Summary blib/lib/Catmandu/TabularExporter.pm »
1 package Catmandu::CLI;
2  
3 13 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 13 our $VERSION = '1.0201_02';
6  
7 13 ▶use Catmandu::Util qw(is_instance is_able is_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Catmandu;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Log::Any::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶use parent qw(App::Cmd);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub deleted_commands {
15 [
16 480 ▶ qw(
Subroutine deleted_commands: called CC=1 SCAR=0.0
Pod undocumented
17 Catmandu::Cmd::exporter_info
18 Catmandu::Cmd::fix_info
19 Catmandu::Cmd::importer_info
20 Catmandu::Cmd::module_info
21 Catmandu::Cmd::move
22 Catmandu::Cmd::store_info
23 )
24 ];
25 }
26  
27 1 ▶sub default_command {'commands'}
Subroutine default_command: called CC=1 SCAR=0.0
Pod documented
28  
29 sub command_groups {
30 6 ▶ [qw(help config convert copy count delete drop export import info run)];
Subroutine command_groups: called CC=1 SCAR=0.0
Pod documented
31 }
32  
33 13 ▶sub plugin_search_path {'Catmandu::Cmd'}
Subroutine plugin_search_path: called CC=1 SCAR=0.0
Pod documented
34  
35 sub global_opt_spec {
36 117 ▶ (['debug|D:i', ""], ['load_path|L=s@', ""], ['lib_path|I=s@', ""]);
Subroutine global_opt_spec: called CC=1 SCAR=0.0
Pod documented
37 }
38  
39 sub default_log4perl_config {
40 2 ▶ my $level = shift // 'DEBUG';
Subroutine default_log4perl_config: called CC=3 SCAR=11.4
Pod undocumented
Condition50%1/2
shift() // "DEBUG"
l!l
20
MC/DC0%0/1
shift() // "DEBUG"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
41 2 ▶ my $appender = shift // 'STDERR';
Condition50%1/2
shift() // "STDERR"
l!l
20
MC/DC0%0/1
shift() // "STDERR"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
42  
43 2 my $config = <<EOF;
44 log4perl.category.Catmandu=$level,$appender
45 log4perl.categoty.Catmandu::Fix::log=TRACE,$appender
46  
47 log4perl.appender.STDOUT=Log::Log4perl::Appender::Screen
48 log4perl.appender.STDOUT.stderr=0
49 log4perl.appender.STDOUT.utf8=1
50  
51 log4perl.appender.STDOUT.layout=PatternLayout
52 log4perl.appender.STDOUT.layout.ConversionPattern=%d [%P] - %p %l %M time=%r : %m%n
53  
54 log4perl.appender.STDERR=Log::Log4perl::Appender::Screen
55 log4perl.appender.STDERR.stderr=1
56 log4perl.appender.STDERR.utf8=1
57  
58 log4perl.appender.STDERR.layout=PatternLayout
59 log4perl.appender.STDERR.layout.ConversionPattern=%d [%P] - %l : %m%n
60  
61 EOF
62 2 \$config;
63 }
64  
65 sub setup_debugging {
66 4 ▶ my %LEVELS = (1 => 'WARN', 2 => 'INFO', 3 => 'DEBUG');
Subroutine setup_debugging: called CC=2 SCAR=6.9
Pod undocumented
67 4 my $debug = shift;
68 4 ▶ my $level = $LEVELS{$debug} // 'WARN';
Condition100%2/2
$LEVELS{$debug} // "WARN"
l!l
13
MC/DC100%1/1
$LEVELS{$debug} // "WARN"
$LEVELS{$debug}
Truth table100%2/2
A
A= $LEVELS{$debug}
Aresult
11
00
69 4 my $load_from;
70  
71 try {
72 4 ▶ my $log4perl_pkg = Catmandu::Util::require_package('Log::Log4perl');
Subroutine __ANON__: called CC=3 SCAR=11.0
73 4 my $logany_adapter
74 = Catmandu::Util::require_package('Log::Any::Adapter::Log4perl');
75 4 my $config = Catmandu->config->{log4perl};
76  
77 4 ▶ if (defined $config) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) { } 2 2 4
78 2 ▶ if ($config =~ /^\S+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config =~ /^\S+$/) { } 1 1 2
79 1 Log::Log4perl::init($config);
80 1 $load_from = "file: $config";
81 }
82 else {
83 1 Log::Log4perl::init(\$config);
84 1 $load_from = "string: <defined in catmandu.yml>";
85 }
86 }
87 else {
88 2 Log::Log4perl::init(default_log4perl_config($level, 'STDERR'));
89 2 $load_from = "string: <defined in " . __PACKAGE__ . ">";
90 }
91  
92 4 Log::Any::Adapter->set('Log4perl');
93 }
94 catch {
95 0 ▶ print STDERR <<EOF;
Subroutine __ANON__: not called CC=1 SCAR=6.9
96  
97 Oops! Debugging tools not available on this platform
98  
99 Try to install Log::Log4perl and Log::Any::Adapter::Log4perl
100  
101 Hint: cpan Log::Log4perl Log::Any::Adapter::Log4perl
102 EOF
103 0 exit(2);
104 4 };
105  
106 4 Catmandu->log->warn(
107 "debug activated - level $level - config load from $load_from");
108 }
109  
110 # overload run to read the global options before
111 # the App::Cmd object is created
112 sub run {
113 40 ▶ my ($class) = @_;
Subroutine run: called CC=7 SCAR=21.6
Pod documented
114  
115 40 my ($global_opts, $argv)
116 = $class->_process_args([@ARGV],
117 $class->_global_option_processing_params);
118  
119 40 ▶ my $load_path = $global_opts->{load_path} || [];
Condition50%1/2
$global_opts->{'load_path'} || []
l!l
040
MC/DC0%0/1
$global_opts->{'load_path'} || []
$global_opts->{'load_path'}
Truth table50%1/2
A
A= $global_opts->{'load_path'}
Aresult
11
00
120 40 ▶ my $lib_path = $global_opts->{lib_path} || [];
Condition100%2/2
$global_opts->{'lib_path'} || []
l!l
238
MC/DC100%1/1
$global_opts->{'lib_path'} || []
$global_opts->{'lib_path'}
Truth table100%2/2
A
A= $global_opts->{'lib_path'}
Aresult
11
00
121  
122 40 ▶ if (exists $global_opts->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $global_opts->{"debug"}) 4 36 40
123 4 ▶ setup_debugging($global_opts->{debug} // 1);
Condition50%1/2
$global_opts->{"debug"} // 1
l!l
40
MC/DC0%0/1
$global_opts->{"debug"} // 1
$global_opts->{"debug"}
Truth table50%1/2
A
A= $global_opts->{"debug"}
Aresult
11
00
124 }
125  
126 40 ▶ if (@$lib_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lib_path) 2 38 40
127 2 Catmandu::Util::use_lib(@$lib_path);
128 }
129  
130 40 Catmandu->load(@$load_path);
131  
132 40 ▶ my $self = ref $class ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 40 0 40
133 40 $self->set_global_options($global_opts);
134 40 my ($cmd, $opts, @args) = $self->prepare_command(@$argv);
135  
136 try {
137 33 ▶ $self->execute_command($cmd, $opts, @args);
Subroutine __ANON__: called CC=1 SCAR=0.0
138 }
139 catch {
140 6 ▶ if (is_instance $_, 'Catmandu::NoSuchPackage') {
Subroutine __ANON__: called CC=14 SCAR=33.0
Branch: 5 branches
BranchTrueFalseTotal
if (is_instance($_, 'Catmandu::NoSuchPackage')) { } 3 3 6
elsif (is_instance($_, 'Catmandu::BadFixArg')) { } 1 2 3
elsif (is_instance($_, 'Catmandu::FixParseError')) { } 1 1 2
elsif (is_instance($_, 'Catmandu::FixError')) { } 1 0 1
elsif (is_instance($_, 'Catmandu::HTTPError')) { } 0 0 0
141 3 my $pkg_name = $_->package_name;
142  
143 3 ▶ if ($pkg_name eq 'Catmandu::Importer::help') {
Branch: 3 branches
BranchTrueFalseTotal
if ($pkg_name eq 'Catmandu::Importer::help') { } 1 2 3
elsif (() = $pkg_name =~ /^Catmandu::(Importer|Exporter|Store)::(\S+)/) { } 1 1 2
elsif ($pkg_name =~ /^Catmandu::Fix::\S+/) { } 1 0 1
144 1 say STDERR "Oops! Did you mean 'catmandu $ARGV[1] $ARGV[0]'?";
145 }
146 elsif (my ($type, $name)
147 = $pkg_name =~ /^Catmandu::(Importer|Exporter|Store)::(\S+)/)
148 {
149 1 say STDERR "Oops! Can't find the "
150 . lc($type)
151 . " '$name' in your configuration file or $pkg_name is not installed.";
152 }
153 elsif ($pkg_name =~ /^Catmandu::Fix::\S+/) {
154 1 my ($fix_name) = $pkg_name =~ /([^:]+)$/;
155 1 ▶ if ($fix_name =~ /^[a-z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix_name =~ /^[a-z]/) { } 1 0 1
156 1 say STDERR
157 "Oops! Tried to execute the fix '$fix_name' but can't find $pkg_name on your system.";
158 }
159 else { # not a fix
160 0 say STDERR "Oops! Failed to load $pkg_name";
161 }
162 }
163 else {
164 0 say STDERR "Oops! Failed to load $pkg_name";
165 }
166  
167 3 ▶ if (is_able $_, 'source') {
Branch: 1 branch
BranchTrueFalseTotal
if (is_able($_, "source")) 1 2 3
168 1 $self->print_source($_->source);
169 }
170  
171 3 goto ERROR;
172 }
173 elsif (is_instance $_, 'Catmandu::BadFixArg') {
174 1 my $fix_name = $_->fix_name;
175 1 my $source = $_->source;
176 1 say STDERR
177 "Oops! The fix '$fix_name' was called with missing or wrong arguments.";
178 1 $self->print_source($_->source);
179 }
180 elsif (is_instance $_, 'Catmandu::FixParseError') {
181 1 my $message = $_->message;
182  
183 1 say STDERR "Oops! Syntax error in your fixes...";
184 1 say STDERR "\n\t$message\n";
185 1 $self->print_source($_->source);
186  
187 1 goto ERROR;
188 }
189 elsif (is_instance $_, 'Catmandu::FixError') {
190 1 my $message = $_->message;
191 1 my $data = $_->data;
192 1 my $fix = $_->fix;
193  
194 1 say STDERR "Oops! One of your fixes threw an error...";
195 1 say STDERR "Source: " . $_->fix;
196 1 say STDERR "Error: $message";
197  
198 1 ▶ say STDERR "Input:\n" . Dumper($data) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 1 1
199  
200 1 goto ERROR;
201 }
202 elsif (is_instance $_, 'Catmandu::HTTPError') {
203 0 my $message = $_->message;
204 0 my $code = $_->code;
205 0 my $url = $_->url;
206 0 my $method = $_->method;
207 0 my $request_body = $_->request_body;
208 0 my $response_body = $_->response_body;
209  
210 0 say STDERR "Oops! Got a HTTP error...";
211 0 say STDERR "Code: $code";
212 0 say STDERR "Error: $message";
213 0 say STDERR "URL: $url";
214 0 say STDERR "Method: $method";
215 0 say STDERR "Request headers:\n" . Dumper($_->request_headers);
216 0 ▶ say STDERR "Request body:\n$request_body"
Branch: 1 branch
BranchTrueFalseTotal
if is_string($request_body) 0 0 0
217 if is_string $request_body;
218 0 say STDERR "Response headers:\n" . Dumper($_->response_headers);
219 0 ▶ say STDERR "Response body:\n$response_body"
Branch: 1 branch
BranchTrueFalseTotal
if is_string($response_body) 0 0 0
220 if is_string $response_body;
221  
222 0 goto ERROR;
223 }
224 else {
225 0 say STDERR "Oops! $_";
226  
227 0 goto ERROR;
228 }
229 33 };
230  
231 28 return 1;
232  
233 5 ERROR:
234 return undef;
235 }
236  
237 sub print_source {
238 3 ▶ my ($self, $source) = @_;
Subroutine print_source: called CC=3 SCAR=11.1
Pod undocumented
239 3 ▶ if (is_string $source) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($source)) 3 0 3
240 3 say STDERR "Source:\n";
241 3 for (split(/\n/, $source)) {
242 3 print STDERR "\t$_\n";
243 }
244 }
245 }
246  
247 sub should_ignore {
248 480 ▶ my ($self, $cmd_class) = @_;
Subroutine should_ignore: called CC=3 SCAR=11.1
Pod undocumented
249 480 for my $cmd (@{$self->deleted_commands}) {
250 2880 ▶ return 1 if $cmd_class->isa($cmd);
Branch: 1 branch
BranchTrueFalseTotal
if $cmd_class->isa($cmd) 0 2880 2880
251 }
252 480 return;
253 }
254  
255 13 1;
256  
257 __END__
258  
259 =pod
260  
261 =head1 NAME
262  
263 Catmandu::CLI - The App::Cmd application class for the catmandu command line script
264  
265 =head1 SEE ALSO
266  
267 L<catmandu>
268  
269 =cut
« blib/lib/Catmandu/Cmd/copy.pm ↑ Summary blib/lib/Catmandu/TabularExporter.pm »