| 1 | package File::DistDir::CLI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use File::DistDir; | ||
| 8 | use File::Spec; | ||
| 9 | |||
| 10 | use parent qw(CLI::Simple); | ||
| 11 | |||
| 12 | our $VERSION = '1.0.1'; | ||
| 13 | |||
| 14 | ######################################################################## | ||
| 15 | sub init { | ||
| 16 | ######################################################################## | ||
| 17 | my ($self) = @_; | ||
| 18 | |||
| 19 | die "ERROR: specify either --distribution or --module\n" | ||
| 20 | if !!$self->get_distribution == !!$self->get_module; | ||
| 21 | |||
| 22 | my %options | ||
| 23 | = $self->get_module | ||
| 24 | ? ( module => $self->get_module ) | ||
| 25 | : ( distribution => $self->get_distribution ); | ||
| 26 | |||
| 27 | $self->set_resource_dir( File::DistDir->new(%options) ); | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | ######################################################################## | ||
| 33 | sub cmd_copy { | ||
| 34 | ######################################################################## | ||
| 35 | my ($self) = @_; | ||
| 36 | |||
| 37 | my ( $source, $destination ) = $self->get_args; | ||
| 38 | |||
| 39 | die "ERROR: usage: dist-dir -d DISTRIBUTION [-r] cp SOURCE [DESTINATION]\n" | ||
| 40 | if !defined $source || ( !defined $destination && $source ne q{.} ); | ||
| 41 | |||
| 42 | $self->get_resource_dir->copy( $source, $destination, recursive => $self->get_recursive ); | ||
| 43 | |||
| 44 | return $SUCCESS; | ||
| 45 | } | ||
| 46 | |||
| 47 | ######################################################################## | ||
| 48 | sub cmd_dir { | ||
| 49 | ######################################################################## | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | print {*STDOUT} $self->get_resource_dir->dir; | ||
| 53 | return $SUCCESS; | ||
| 54 | } | ||
| 55 | |||
| 56 | ######################################################################## | ||
| 57 | sub cmd_ls { | ||
| 58 | ######################################################################## | ||
| 59 | my ($self) = @_; | ||
| 60 | |||
| 61 | _print_tree( $self->get_resource_dir->dir ); | ||
| 62 | return $SUCCESS; | ||
| 63 | } | ||
| 64 | |||
| 65 | ######################################################################## | ||
| 66 | sub cmd_find { | ||
| 67 | ######################################################################## | ||
| 68 | my ($self) = @_; | ||
| 69 | |||
| 70 | my @args = $self->get_args; | ||
| 71 | die "ERROR: usage: dist-dir (-d DISTRIBUTION | -m MODULE) find PATTERN\n" | ||
| 72 | if @args != 1; | ||
| 73 | |||
| 74 | foreach my $path ( $self->get_resource_dir->find( $args[0] ) ) { | ||
| 75 | print {*STDOUT} "$path\n"; | ||
| 76 | } | ||
| 77 | |||
| 78 | return $SUCCESS; | ||
| 79 | } | ||
| 80 | |||
| 81 | ######################################################################## | ||
| 82 | sub main { | ||
| 83 | ######################################################################## | ||
| 84 | my @option_specs = qw( | ||
| 85 | help|h | ||
| 86 | distribution|d=s | ||
| 87 | module|m=s | ||
| 88 | recursive|r | ||
| 89 | ); | ||
| 90 | |||
| 91 | my %commands = ( | ||
| 92 | cp => \&cmd_copy, | ||
| 93 | dir => \&cmd_dir, | ||
| 94 | find => \&cmd_find, | ||
| 95 | ls => \&cmd_ls, | ||
| 96 | default => 'dir', | ||
| 97 | ); | ||
| 98 | |||
| 99 | my $cli = __PACKAGE__->new( | ||
| 100 | option_specs => \@option_specs, | ||
| 101 | default_options => {}, | ||
| 102 | extra_options => [qw(resource_dir)], | ||
| 103 | commands => \%commands, | ||
| 104 | ); | ||
| 105 | |||
| 106 | return $cli->run(); | ||
| 107 | } | ||
| 108 | |||
| 109 | ######################################################################## | ||
| 110 | sub _print_tree { | ||
| 111 | ######################################################################## | ||
| 112 | my ( $root, $prefix ) = @_; | ||
| 113 | |||
| 114 | $prefix //= q{}; | ||
| 115 | |||
| 116 | opendir my $dh, $root | ||
| 117 | or return; | ||
| 118 | |||
| 119 | my @entries = sort grep { $_ ne q{.} && $_ ne q{..} } readdir $dh; | ||
| 120 | closedir $dh; | ||
| 121 | |||
| 122 | for my $i ( 0 .. $#entries ) { | ||
| 123 | my $entry = $entries[$i]; | ||
| 124 | my $full = File::Spec->catfile( $root, $entry ); | ||
| 125 | my $is_last = $i == $#entries; | ||
| 126 | |||
| 127 | print {*STDOUT} $prefix; | ||
| 128 | print {*STDOUT} $is_last ? '└── ' : '├── '; | ||
| 129 | print {*STDOUT} "$entry\n"; | ||
| 130 | |||
| 131 | if ( -d $full && !-l $full ) { | ||
| 132 | _print_tree( $full, $prefix . ( $is_last ? ' ' : '│ ' ) ); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ |