| 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.0'; | ||
| 13 | |||
| 14 | ######################################################################## | ||
| 15 | sub init { | ||
| 16 | ######################################################################## | ||
| 17 | my ($self) = @_; | ||
| 18 | |||
| 19 | die "ERROR: --distribution is a required argument\n" | ||
| 20 | if !$self->get_distribution; | ||
| 21 | |||
| 22 | $self->set_resource_dir( File::DistDir->new( distribution => $self->get_distribution ) ); | ||
| 23 | |||
| 24 | return $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | ######################################################################## | ||
| 28 | sub cmd_copy { | ||
| 29 | ######################################################################## | ||
| 30 | my ($self) = @_; | ||
| 31 | |||
| 32 | my ( $source, $destination ) = $self->get_args; | ||
| 33 | |||
| 34 | die "ERROR: usage: dist-dir -d DISTRIBUTION [-r] cp SOURCE [DESTINATION]\n" | ||
| 35 | if !defined $source || ( !defined $destination && $source ne q{.} ); | ||
| 36 | |||
| 37 | $self->get_resource_dir->copy( $source, $destination, recursive => $self->get_recursive ); | ||
| 38 | |||
| 39 | return $SUCCESS; | ||
| 40 | } | ||
| 41 | |||
| 42 | ######################################################################## | ||
| 43 | sub cmd_dir { | ||
| 44 | ######################################################################## | ||
| 45 | my ($self) = @_; | ||
| 46 | |||
| 47 | print {*STDOUT} $self->get_resource_dir->dir; | ||
| 48 | return $SUCCESS; | ||
| 49 | } | ||
| 50 | |||
| 51 | ######################################################################## | ||
| 52 | sub cmd_ls { | ||
| 53 | ######################################################################## | ||
| 54 | my ($self) = @_; | ||
| 55 | |||
| 56 | _print_tree( $self->get_resource_dir->dir ); | ||
| 57 | return $SUCCESS; | ||
| 58 | } | ||
| 59 | |||
| 60 | ######################################################################## | ||
| 61 | sub main { | ||
| 62 | ######################################################################## | ||
| 63 | my @option_specs = qw( | ||
| 64 | help|h | ||
| 65 | distribution|d=s | ||
| 66 | recursive|r | ||
| 67 | ); | ||
| 68 | |||
| 69 | my %commands = ( | ||
| 70 | cp => \&cmd_copy, | ||
| 71 | dir => \&cmd_dir, | ||
| 72 | ls => \&cmd_ls, | ||
| 73 | default => 'dir', | ||
| 74 | ); | ||
| 75 | |||
| 76 | my $cli = __PACKAGE__->new( | ||
| 77 | option_specs => \@option_specs, | ||
| 78 | default_options => {}, | ||
| 79 | extra_options => [qw(resource_dir)], | ||
| 80 | commands => \%commands, | ||
| 81 | ); | ||
| 82 | |||
| 83 | return $cli->run(); | ||
| 84 | } | ||
| 85 | |||
| 86 | ######################################################################## | ||
| 87 | sub _print_tree { | ||
| 88 | ######################################################################## | ||
| 89 | my ( $root, $prefix ) = @_; | ||
| 90 | |||
| 91 | $prefix //= q{}; | ||
| 92 | |||
| 93 | opendir my $dh, $root | ||
| 94 | or return; | ||
| 95 | |||
| 96 | my @entries = sort grep { $_ ne q{.} && $_ ne q{..} } readdir $dh; | ||
| 97 | closedir $dh; | ||
| 98 | |||
| 99 | for my $i ( 0 .. $#entries ) { | ||
| 100 | my $entry = $entries[$i]; | ||
| 101 | my $full = File::Spec->catfile( $root, $entry ); | ||
| 102 | my $is_last = $i == $#entries; | ||
| 103 | |||
| 104 | print {*STDOUT} $prefix; | ||
| 105 | print {*STDOUT} $is_last ? '└── ' : '├── '; | ||
| 106 | print {*STDOUT} "$entry\n"; | ||
| 107 | |||
| 108 | if ( -d $full && !-l $full ) { | ||
| 109 | _print_tree( $full, $prefix . ( $is_last ? ' ' : '│ ' ) ); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | __END__ |