| 1 | package App::BambooCli; | ||
| 2 | |||
| 3 | # Created on: 2019-05-15 09:09:16 | ||
| 4 | # Create by: Ivan Wills | ||
| 5 | # $Id$ | ||
| 6 | # $Revision$, $HeadURL$, $Date$ | ||
| 7 | # $Revision$, $Source$, $Date$ | ||
| 8 | |||
| 9 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use version; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Scalar::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use List::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | #use List::MoreUtils; | ||
| 16 | 1 | ▶ | use Getopt::Alt; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 1 | ▶ | use Data::Dumper qw/Dumper/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use English qw/ -no_match_vars /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use Path::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use YAML::Syck qw/ LoadFile DumpFile /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 0 | ▶ | use App::BambooCli::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 22 | |||
| 23 | our $VERSION = version->new('0.0.1'); | ||
| 24 | |||
| 25 | has config => ( | ||
| 26 | is => 'rw', | ||
| 27 | ); | ||
| 28 | |||
| 29 | has sub_commands => ( | ||
| 30 | is => 'rw', | ||
| 31 | lazy => 1, | ||
| 32 | builder => '_sub_commands', | ||
| 33 | ); | ||
| 34 | |||
| 35 | sub start { | ||
| 36 | my ($self) = @_; | ||
| 37 | my @sub_commands = keys %{ $self->sub_commands }; | ||
| 38 | |||
| 39 | my ($options, $cmd, $opt) = get_options( | ||
| 40 | { | ||
| 41 | name => 'bamboo', | ||
| 42 | conf_prefix => '.', | ||
| 43 | helper => 1, | ||
| 44 | default => { | ||
| 45 | config => {}, | ||
| 46 | test => 0, | ||
| 47 | }, | ||
| 48 | auto_complete => sub { | ||
| 49 | my ($option, $auto, $errors) = @_; | ||
| 50 | my $sub_command = $option->files->[0] || ''; | ||
| 51 | if ( $sub_command eq '--' ) { | ||
| 52 | print join ' ', sort @sub_commands; | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | elsif ( grep {/^$sub_command./} @sub_commands ) { | ||
| 56 | print join ' ', sort grep {/^$sub_command/} @sub_commands; | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | elsif ( ! $self->sub_commands->{$sub_command} ) { | ||
| 60 | unshift @{$option->files}, $sub_command; | ||
| 61 | $sub_command = $ENV{VTIDE_DIR} ? 'edit' : 'start'; | ||
| 62 | } | ||
| 63 | eval { | ||
| 64 | $self->load_subcommand( $sub_command, $option )->auto_complete($auto); | ||
| 65 | 1; | ||
| 66 | } or do { | ||
| 67 | print join ' ', grep {/$sub_command/xms} sort @sub_commands; | ||
| 68 | } | ||
| 69 | }, | ||
| 70 | auto_complete_shortener => sub { | ||
| 71 | my ($getopt, @args) = @_; | ||
| 72 | my $sub_command = shift @args || ''; | ||
| 73 | |||
| 74 | if ( grep {/^$sub_command./} @sub_commands ) { | ||
| 75 | $getopt->cmd($sub_command); | ||
| 76 | } | ||
| 77 | elsif ( ! $self->sub_commands->{$sub_command} ) { | ||
| 78 | $getopt->cmd( $ENV{VTIDE_DIR} ? 'edit' : 'start' ); | ||
| 79 | unshift @args, $sub_command; | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | $getopt->cmd($sub_command) if ! $getopt->cmd; | ||
| 83 | } | ||
| 84 | |||
| 85 | return @args; | ||
| 86 | }, | ||
| 87 | sub_command => $self->sub_commands, | ||
| 88 | help_package => __PACKAGE__, | ||
| 89 | help_packages => { | ||
| 90 | map {$_ => __PACKAGE__ . '::Command::' . ucfirst $_} | ||
| 91 | @sub_commands, | ||
| 92 | }, | ||
| 93 | }, | ||
| 94 | [ | ||
| 95 | 'config|c=s%', | ||
| 96 | 'test|T!', | ||
| 97 | 'verbose|v+', | ||
| 98 | ], | ||
| 99 | ); | ||
| 100 | |||
| 101 | $self->config( App::BambooCli::Config->new(%{ $options->config }) ); | ||
| 102 | |||
| 103 | #if ( ! $self->sub_commands->{ $opt->cmd } ) { | ||
| 104 | # unshift @ARGV, $opt->cmd; | ||
| 105 | # $opt->cmd( $ENV{VTIDE_DIR} ? 'edit' : 'start' ); | ||
| 106 | # $opt->files(\@ARGV); | ||
| 107 | #} | ||
| 108 | |||
| 109 | my $subcommand = $self->load_subcommand( $opt->cmd, $opt ); | ||
| 110 | #if ( ! $subcommand ) { | ||
| 111 | # $subcommand = $self->load_subcommand( $ENV{VTIDE_DIR} ? 'edit' : 'start', $opt ); | ||
| 112 | # my (undef, $dir) = $subcommand->session_dir($opt->cmd); | ||
| 113 | # if ( !$dir ) { | ||
| 114 | # my $error = $@; | ||
| 115 | # warn $@ if $opt->opt->verbose; | ||
| 116 | # warn "Unknown command '$cmd'!\n", | ||
| 117 | # "Valid commands - ", ( join ', ', sort @sub_commands ), | ||
| 118 | # "\n"; | ||
| 119 | # require Pod::Usage; | ||
| 120 | # Pod::Usage::pod2usage( | ||
| 121 | # -verbose => 1, | ||
| 122 | # -input => __FILE__, | ||
| 123 | # ); | ||
| 124 | # } | ||
| 125 | # unshift @{ $opt->files }, $opt->cmd; | ||
| 126 | #} | ||
| 127 | |||
| 128 | return $subcommand->run; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub load_subcommand { | ||
| 132 | my ( $self, $cmd, $opt ) = @_; | ||
| 133 | |||
| 134 | my $file = 'App/BambooCli/Command/' . ucfirst $cmd . '.pm'; | ||
| 135 | my $module = 'App::BambooCli::Command::' . ucfirst $cmd; | ||
| 136 | |||
| 137 | require $file; | ||
| 138 | |||
| 139 | return $module->new( | ||
| 140 | defaults => $opt->opt, | ||
| 141 | options => $opt, | ||
| 142 | bamboo => $self, | ||
| 143 | )->get_sub_options; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _sub_commands { | ||
| 147 | my ($self) = @_; | ||
| 148 | my $sub_file = path $ENV{HOME}, '.bamboo', 'sub-commands.yml'; | ||
| 149 | |||
| 150 | mkdir $sub_file->parent if ! -d $sub_file->parent; | ||
| 151 | |||
| 152 | return LoadFile("$sub_file") if -f $sub_file; | ||
| 153 | |||
| 154 | return $self->_generate_sub_command(); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _generate_sub_command { | ||
| 158 | my ($self) = @_; | ||
| 159 | my $sub_file = path $ENV{HOME}, '.bamboo', 'sub-commands.yml'; | ||
| 160 | |||
| 161 | require Module::Pluggable; | ||
| 162 | Module::Pluggable->import( require => 1, search_path => ['App::BambooCli::Command'] ); | ||
| 163 | my @commands = __PACKAGE__->plugins; | ||
| 164 | |||
| 165 | my $sub_commands = {}; | ||
| 166 | for my $command (reverse sort @commands) { | ||
| 167 | my ($name, $conf) = $command->details_sub; | ||
| 168 | $sub_commands->{$name} = $conf; | ||
| 169 | } | ||
| 170 | |||
| 171 | DumpFile($sub_file, $sub_commands); | ||
| 172 | |||
| 173 | return $sub_commands; | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; | ||
| 177 | |||
| 178 | __END__ | ||