Skip to content

Commit

Permalink
Extending command info
Browse files Browse the repository at this point in the history
Simplified the code and added field 'about' with module description.
  • Loading branch information
nichtich committed Jun 18, 2015
1 parent 9c5b391 commit e70a5a3
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions lib/Catmandu/Cmd/info.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package Catmandu::Cmd::info;

use Catmandu::Sane;
use parent 'Catmandu::Cmd';
use Catmandu::Importer::Modules;
use Catmandu::Store::Hash;
use Catmandu::Util 'pod_section';

sub command_opt_spec {
(
["all" , "show all module on this server"],
["exporters" , "show all catmandu exporters"],
["importers" , "show all catmandu importers"],
["fixes" , "show all catmandu fixes"],
["stores" , "show all catmandu stores"],
["namespace=s", "search by namespace"],
["max_depth=i", "maximum depth to search for modules"],
["inc=s@", 'override included directories (defaults to @INC)', {default => [@INC]}],
["verbose|v", ""]
);
}

sub description { "list installed Catmandu modules\n\nOptions:" }

sub add_about {
my $item = shift;
my $name = pod_section($item->{file}, 'NAME');
$name =~ s/[^-]+(\s*-?\s*)?//;
$name =~ s/\n/ /mg;
chomp $name;
$item->{about} = $name;
$item;
}

sub command {
my ($self, $opts, $args) = @_;
my $verbose = $opts->verbose;

if (defined $opts->{namespace}) {
}
elsif ($opts->{all}) {
delete $opts->{all};
}
elsif ($opts->{exporters}) {
delete $opts->{exporters};
$opts->{namespace} = 'Catmandu::Exporter';
}
elsif ($opts->{importers}) {
delete $opts->{importers};
$opts->{namespace} = 'Catmandu::Importer';
}
elsif ($opts->{fixes}) {
delete $opts->{fixes};
$opts->{namespace} = 'Catmandu::Fix';
}
elsif ($opts->{stores}) {
delete $opts->{stores};
$opts->{namespace} = 'Catmandu::Store';
}
else {
$opts->{namespace} = [qw(Catmandu::Exporter Catmandu::Fix Catmandu::Importer Catmandu::Store)];
}

my $from_opts = { fix => [sub{add_about(@_)}] };

for my $key (qw(inc namespace max_depth)) {
$from_opts->{$key} = $opts->$key if defined $opts->$key;
}

my $from = Catmandu::Importer::Modules->new($from_opts);

my $into_args = [];
my $into_opts = {};
my $into;

if (@$args && $args->[0] eq 'to') {
# TODO: don't duplicate argument parsing
for (my $i = 1; $i < @$args; $i++) {
my $arg = $args->[$i];
if ($arg =~ s/^-+//) {
$arg =~ s/-/_/g;
if ($arg eq 'fix') {
push @{$into_opts->{$arg} ||= []}, $args->[++$i];
} else {
$into_opts->{$arg} = $args->[++$i];
}
} else {
push @$into_args, $arg;
}
}
}

if (@$into_args || %$into_opts) {
$into = Catmandu->exporter($into_args->[0], $into_opts);
$into->add_many($from);
$into->commit;
} else {
my $cols = [qw(name version about)];
push @$cols, 'file' if $opts->verbose;
$from->format(cols => $cols);
}
}

1;
__END__
=head1 NAME
Catmandu::Cmd::info - list installed Catmandu modules
=head1 DESCRIPTION
This L<Catmandu::Cmd> uses L<Catmandu::Importer::Modules> to list all modules.
=cut

0 comments on commit e70a5a3

Please sign in to comment.