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