Catmandu/CLI.pm

filter: Statementstmt 96.3% 78 / 81 Branchbran 83.3% 15 / 18 Conditioncond 73.3% 11 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 95.0% 19 / 20 Podpod 50.0% 4 / 8 totaltotal 87.3% 131 / 150 CC 18 SCAR 12.4
CC
18
Cov
91%
run
8.1
__ANON__
4.1
default_log4perl_config
3.1
CRAP 3.4
« Catmandu/Store.pm ↑ Summary Catmandu/Fix/include.pm »
1 package Catmandu::CLI;
2  
3 14 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 our $VERSION = '1.2025';
6  
7 14 ▶use Catmandu::Util qw(is_instance);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Catmandu;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Log::Any::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 14 ▶use parent qw(App::Cmd);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 # Unknown opts are added to the args instead of generating a usage error
17 # so we can handle them dynamically (see Catmandu::Cmd _parse_options).
18 14 Getopt::Long::Configure("pass_through");
19 }
20  
21 624 ▶sub deleted_commands {
Subroutine deleted_commands: called CC=1 SCAR=0.0
Pod undocumented
22 [
23 624 qw(
24 Catmandu::Cmd::data
25 Catmandu::Cmd::exporter_info
26 Catmandu::Cmd::fix_info
27 Catmandu::Cmd::importer_info
28 Catmandu::Cmd::module_info
29 Catmandu::Cmd::move
30 Catmandu::Cmd::store_info
31 )
32 ];
33 }
34  
35 1 ▶sub default_command {'commands'}
Subroutine default_command: called CC=1 SCAR=0.0
Pod documented
36  
37 14 ▶sub plugin_search_path {'Catmandu::Cmd'}
Subroutine plugin_search_path: called CC=1 SCAR=0.0
Pod documented
38  
39 141 ▶sub global_opt_spec {
Subroutine global_opt_spec: called CC=1 SCAR=0.0
Pod documented
40 141 (['debug|D:i', ""], ['load_path|L=s@', ""], ['lib_path|I=s@', ""]);
41 }
42  
43 2 ▶sub default_log4perl_config {
Subroutine default_log4perl_config: called CC=3 SCAR=11.4
Pod undocumented
44 2 ▶ my $level = shift // 'DEBUG';
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
45 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
46  
47 2 my $config = <<EOF;
48 log4perl.category.Catmandu=$level,$appender
49 log4perl.category.Catmandu::Fix::log=TRACE,$appender
50  
51 log4perl.appender.STDOUT=Log::Log4perl::Appender::Screen
52 log4perl.appender.STDOUT.stderr=0
53 log4perl.appender.STDOUT.utf8=1
54  
55 log4perl.appender.STDOUT.layout=PatternLayout
56 log4perl.appender.STDOUT.layout.ConversionPattern=%d [%P] - %p %l %M time=%r : %m%n
57  
58 log4perl.appender.STDERR=Log::Log4perl::Appender::Screen
59 log4perl.appender.STDERR.stderr=1
60 log4perl.appender.STDERR.utf8=1
61  
62 log4perl.appender.STDERR.layout=PatternLayout
63 log4perl.appender.STDERR.layout.ConversionPattern=%d [%P] - %l : %m%n
64  
65 EOF
66 2 \$config;
67 }
68  
69 4 ▶sub setup_debugging {
Subroutine setup_debugging: called CC=2 SCAR=6.9
Pod undocumented
70 4 my %LEVELS = (1 => 'WARN', 2 => 'INFO', 3 => 'DEBUG');
71 4 my $debug = shift;
72 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
73 4 my $load_from;
74  
75 4 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
76 4 my $log4perl_pkg = Catmandu::Util::require_package('Log::Log4perl');
77 4 my $logany_adapter
78 = Catmandu::Util::require_package('Log::Any::Adapter::Log4perl');
79 4 my $config = Catmandu->config->{log4perl};
80  
81 4 ▶ if (defined $config) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) { } 2 2 4
82 2 ▶ if ($config =~ /^\S+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config =~ /^\S+$/) { } 1 1 2
83 1 Log::Log4perl::init($config);
84 1 $load_from = "file: $config";
85 }
86 else {
87 1 Log::Log4perl::init(\$config);
88 1 $load_from = "string: <defined in catmandu.yml>";
89 }
90 }
91 else {
92 2 Log::Log4perl::init(default_log4perl_config($level, 'STDERR'));
93 2 $load_from = "string: <defined in " . __PACKAGE__ . ">";
94 }
95  
96 4 Log::Any::Adapter->set('Log4perl');
97 }
98 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 print STDERR <<EOF;
100  
101 Oops! Debugging tools not available on this platform
102  
103 Try to install Log::Log4perl and Log::Any::Adapter::Log4perl
104  
105 Hint: cpan Log::Log4perl Log::Any::Adapter::Log4perl
106 EOF
107 0 exit(2);
108 4 };
109  
110 4 Catmandu->log->warn(
111 "debug activated - level $level - config load from $load_from");
112 }
113  
114 # overload run to read the global options before
115 # the App::Cmd object is created
116 48 ▶sub run {
Subroutine run: called CC=8 SCAR=20.9
Pod documented
117 48 my ($class) = @_;
118  
119 48 my ($global_opts, $argv)
120 = $class->_process_args([@ARGV],
121 $class->_global_option_processing_params);
122  
123 48 ▶ my $load_path = $global_opts->{load_path} || [];
Condition50%1/2
$global_opts->{'load_path'} || []
l!l
048
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
124 48 ▶ my $lib_path = $global_opts->{lib_path} || [];
Condition100%2/2
$global_opts->{'lib_path'} || []
l!l
246
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
125  
126 48 ▶ if (exists $global_opts->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $global_opts->{"debug"}) 4 44 48
127 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
128 }
129  
130 48 ▶ if (@$lib_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lib_path) 2 46 48
131 2 Catmandu::Util::use_lib(@$lib_path);
132 }
133  
134 48 Catmandu->load(@$load_path);
135  
136 48 ▶ my $self = ref $class ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 48 0 48
137 48 $self->set_global_options($global_opts);
138 48 my ($cmd, $opts, @args) = $self->prepare_command(@$argv);
139  
140 48 my $err;
141  
142 48 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
143 48 $self->execute_command($cmd, $opts, @args);
144 }
145 8 ▶ catch {
Subroutine __ANON__: called CC=4 SCAR=14.0
146 8 my $e = $_;
147 8 ▶ if (is_instance($e, 'Catmandu::NoSuchPackage')
Branch: 2 branches
BranchTrueFalseTotal
if (is_instance($e, 'Catmandu::NoSuchPackage') and $e->package_name eq 'Catmandu::Importer::help') { } 1 7 8
elsif (is_instance($e, 'Catmandu::Error')) { } 7 0 7
Condition100%3/3
is_instance($e, "Catmandu::NoSuchPackage") and $e->package_name eq "Catmandu::Importer::help"
!ll&&!rl&&r
431
MC/DC100%2/2
is_instance($e, "Catmandu::NoSuchPackage") and $e->package_name eq "Catmandu::Importer::help"
is_instance($e, "Catmandu::NoSuchPackage") $e->package_name eq "Catmandu::Importer::help"
Truth table100%3/3
A and B
A= is_instance($e, "Catmandu::NoSuchPackage")B= $e->package_name eq "Catmandu::Importer::help"
ABresult
0X0
100
111
148 && $e->package_name eq 'Catmandu::Importer::help')
149 {
150 1 $err = "Did you mean 'catmandu $ARGV[1] $ARGV[0]'?";
151 }
152 elsif (is_instance($e, 'Catmandu::Error')) {
153 7 $err = $e->log_message;
154 }
155 else {
156 0 $err = $e;
157 }
158 48 };
159  
160 48 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 8 40 48
161 8 say STDERR "Oops! $err";
162 8 return;
163 }
164  
165 40 1;
166 }
167  
168 624 ▶sub should_ignore {
Subroutine should_ignore: called CC=3 SCAR=11.1
Pod undocumented
169 624 my ($self, $cmd_class) = @_;
170 624 for my $cmd (@{$self->deleted_commands}) {
171 4368 ▶ return 1 if $cmd_class->isa($cmd);
Branch: 1 branch
BranchTrueFalseTotal
if $cmd_class->isa($cmd) 0 4368 4368
172 }
173 624 return;
174 }
175  
176 14 1;
177  
178 __END__
« Catmandu/Store.pm ↑ Summary Catmandu/Fix/include.pm »