| 1 | #!/usr/bin/env perl | ||
| 2 | package App::SD::CLI::Dispatcher; | ||
| 3 | use Prophet::CLI::Dispatcher -base; | ||
| 4 | use Any::Moose; | ||
| 5 | require Prophet::CLIContext; | ||
| 6 | use File::Basename; | ||
| 7 | |||
| 8 | Prophet::CLI::Dispatcher->add_command_prefix('App::SD::CLI::Command'); | ||
| 9 | |||
| 10 | # "sd ?about" => "sd help about" | ||
| 11 | rewrite qr/^\?(.*)/ => sub { "help ".($1||'') }; | ||
| 12 | |||
| 13 | # 'sd about' -> 'sd help about', 'sd copying' -> 'sd help copying' | ||
| 14 | rewrite [ ['about', 'copying'] ] => sub { "help $1" }; | ||
| 15 | |||
| 16 | on qr'^(?!help)' => sub { | ||
| 17 | my $self = shift; | ||
| 18 | my $cmd = $_; | ||
| 19 | |||
| 20 | if ($self->context->has_arg('help')) { | ||
| 21 | run("help $cmd", $self, @_); | ||
| 22 | } | ||
| 23 | elsif ($self->context->has_arg('version') | ||
| 24 | || $self->context->has_arg('V') ) { | ||
| 25 | $self->context->delete_arg('version'); | ||
| 26 | $self->context->delete_arg('V'); | ||
| 27 | run("version", $self); | ||
| 28 | } | ||
| 29 | else { | ||
| 30 | next_rule; | ||
| 31 | } | ||
| 32 | }; | ||
| 33 | |||
| 34 | under help => sub { | ||
| 35 | on [ [ 'intro', 'init' ] ] => run_command('Help::Intro'); | ||
| 36 | on about => run_command('Help::About'); | ||
| 37 | on config => run_command('Help::Config'); | ||
| 38 | on copying => run_command('Help::Copying'); | ||
| 39 | on commands => run_command('Help::Commands'); | ||
| 40 | on [ ['summary-format', 'ticket.summary-format', 'ticket_summary_format'] ] | ||
| 41 | => run_command('Help::ticket_summary_format'); | ||
| 42 | |||
| 43 | on [ ['author', 'authors'] ] => run_command('Help::Authors'); | ||
| 44 | on [ ['environment', 'env'] ] => run_command('Help::Environment'); | ||
| 45 | on [ ['ticket', 'tickets'] ] => run_command('Help::Tickets'); | ||
| 46 | on [ ['attach', 'attachment', 'attachments'] ] => run_command('Help::Attachments'); | ||
| 47 | on [ ['comment', 'comments'] ] => run_command('Help::Comments'); | ||
| 48 | on [ ['setting', 'settings'] ] => run_command('Help::Settings'); | ||
| 49 | on [ ['history', 'log'] ] => run_command('Help::History'); | ||
| 50 | on [ ['alias', 'aliases'] ] => run_command('Help::Aliases'); | ||
| 51 | |||
| 52 | on [ | ||
| 53 | ['ticket', 'attachment', 'comment'], | ||
| 54 | ['list', 'search', 'find'], | ||
| 55 | ] => run_command('Help::Search'); | ||
| 56 | |||
| 57 | # anything else under ticket, e.g. 'ticket close' etc. should be covered | ||
| 58 | # in the tickets help | ||
| 59 | on qr/^ticket/ => run_command('Help::Tickets'); | ||
| 60 | |||
| 61 | on [ ['search', 'list', 'find'] ] => run_command('Help::Search'); | ||
| 62 | |||
| 63 | on [ ['sync', 'push', 'pull', 'publish', 'server', 'browser', 'clone'] ] | ||
| 64 | => run_command('Help::Sync'); | ||
| 65 | |||
| 66 | on qr/^(\S+)$/ => sub { | ||
| 67 | my $self = shift; | ||
| 68 | my $topic = $1; | ||
| 69 | die "Cannot find help on topic '$topic'. Try '".$self->cli->get_script_name()."help'?\n"; | ||
| 70 | }; | ||
| 71 | }; | ||
| 72 | |||
| 73 | on help => run_command('Help'); | ||
| 74 | |||
| 75 | on qr'.*' => sub { | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | unless ( $self->cli->app_handle->local_replica_url || | ||
| 79 | $self->cli->context->has_arg('h') ) { | ||
| 80 | |||
| 81 | print join "\n", | ||
| 82 | "", | ||
| 83 | "It appears that you haven't specified a local replica path.", | ||
| 84 | "You can do so by setting the SD_REPO environment variable.", | ||
| 85 | "", | ||
| 86 | " 'sd help intro' will tell you a bit about how to get started with sd.", | ||
| 87 | " 'sd help' will show show you a list of help topics.", | ||
| 88 | "", ""; | ||
| 89 | |||
| 90 | exit 1; | ||
| 91 | } | ||
| 92 | next_rule; | ||
| 93 | }; | ||
| 94 | |||
| 95 | on qr'.*' => sub { | ||
| 96 | my $self = shift; | ||
| 97 | my $command = $_; | ||
| 98 | next_rule if $command =~ /^(?:shell|clone|init)$|(config|alias(?:es)?)/; | ||
| 99 | next_rule if $self->cli->app_handle->handle->replica_exists; | ||
| 100 | |||
| 101 | print join("\n","No SD database was found at " . $self->cli->app_handle->handle->url(), | ||
| 102 | qq{Type "} . $self->cli->get_script_name(). qq{help init" or "}. | ||
| 103 | $self->cli->get_script_name().qq{help environment" for tips on how to sort that out.}); | ||
| 104 | exit 1; | ||
| 105 | }; | ||
| 106 | |||
| 107 | on browser => run_command('Browser'); | ||
| 108 | |||
| 109 | # allow doing some things backwards -- e.g. 'list tickets' etc. | ||
| 110 | on qr/^(\w+)\s+tickets?(.*)$/ => sub { | ||
| 111 | my $self = shift; | ||
| 112 | my $primary = $1; | ||
| 113 | my $secondary = $2; | ||
| 114 | next_rule if $primary eq 'help'; | ||
| 115 | my $cmd = join( ' ', grep { $_ ne '' } 'ticket',$primary, $secondary); | ||
| 116 | my @orig_argv = @{$self->cli->context->raw_args}; | ||
| 117 | my ($subcommand, undef) = (shift @orig_argv, shift @orig_argv); | ||
| 118 | $self->cli->run_one_command( 'ticket', $subcommand, @orig_argv); | ||
| 119 | }; | ||
| 120 | |||
| 121 | under ticket => sub { | ||
| 122 | # all these might possibly have IDs tacked onto the end | ||
| 123 | on | ||
| 124 | qr/^((?:comment\s+)?(?:comments?|update|edit|show|details|display|delete|del|rm|history|claim|take|resolve|basics|close)) $Prophet::CLIContext::ID_REGEX$/i => sub { | ||
| 125 | my $self = shift; | ||
| 126 | $self->context->set_id_from_primary_commands; | ||
| 127 | run("ticket $1", $self, @_); | ||
| 128 | }; | ||
| 129 | |||
| 130 | on [ [ 'new' , 'create' ] ] => run_command('Ticket::Create'); | ||
| 131 | on [ [ 'show' , 'display' ] ] => run_command('Ticket::Show'); | ||
| 132 | on [ [ 'update' , 'edit' ] ] => run_command('Ticket::Update'); | ||
| 133 | on [ [ 'search', 'list', 'ls' ] ] => run_command('Ticket::Search'); | ||
| 134 | on review => run_command('Ticket::Review'); | ||
| 135 | on details => run_command('Ticket::Details'); | ||
| 136 | on basics => run_command('Ticket::Basics'); | ||
| 137 | on comment => run_command('Ticket::Comment'); | ||
| 138 | on comments => run_command('Ticket::Comments'); | ||
| 139 | |||
| 140 | under [ [ 'give', 'assign' ] ] => sub { | ||
| 141 | on [qr/^$Prophet::CLIContext::ID_REGEX$/, qr/^\S+$/] => sub { | ||
| 142 | my $self = shift; | ||
| 143 | my ($id, $owner) = ($1, $2); | ||
| 144 | |||
| 145 | $self->context->set_arg(id => $id); | ||
| 146 | $self->context->set_arg(type => 'ticket'); | ||
| 147 | $self->context->set_prop(owner => $owner); | ||
| 148 | $self->context->set_type_and_uuid; | ||
| 149 | run('ticket update', $self, @_); | ||
| 150 | }; | ||
| 151 | on qr/^(.*)$/ => sub { | ||
| 152 | die "Usage: give <id> <email>\n"; | ||
| 153 | }; | ||
| 154 | }; | ||
| 155 | |||
| 156 | on [ ['resolve', 'close'] ] => sub { | ||
| 157 | my $self = shift; | ||
| 158 | $self->context->set_prop(status => 'closed'); | ||
| 159 | run('ticket update', $self, @_); | ||
| 160 | }; | ||
| 161 | |||
| 162 | # simulate a 'claim' command by setting the owner prop and passing | ||
| 163 | # off to update | ||
| 164 | on [ [ 'claim', 'take' ] ] => sub { | ||
| 165 | my $self = shift; | ||
| 166 | my $email = $self->context->app_handle->current_user_email; | ||
| 167 | if ($email) { | ||
| 168 | $self->context->set_prop(owner => $email); | ||
| 169 | run('ticket update', $self, @_); | ||
| 170 | } else { | ||
| 171 | die "Could not determine email address to assign ticket to!\n". | ||
| 172 | "Set the 'user.email-address' config variable.\n"; | ||
| 173 | } | ||
| 174 | }; | ||
| 175 | |||
| 176 | under comment => sub { | ||
| 177 | on create => run_command('Ticket::Comment::Create'); | ||
| 178 | on update => run_command('Ticket::Comment::Update'); | ||
| 179 | }; | ||
| 180 | |||
| 181 | under attachment => sub { | ||
| 182 | on create => run_command('Ticket::Attachment::Create'); | ||
| 183 | on [ [ 'create', 'new' ], qr/^$Prophet::CLIContext::ID_REGEX$/ ] => sub { | ||
| 184 | my $self = shift; | ||
| 185 | $self->context->set_id_from_primary_commands; | ||
| 186 | run('ticket attachment create', $self, @_); | ||
| 187 | }; | ||
| 188 | on search => run_command('Ticket::Attachment::Search'); | ||
| 189 | }; | ||
| 190 | |||
| 191 | |||
| 192 | }; | ||
| 193 | |||
| 194 | under attachment => sub { | ||
| 195 | on qr/^(.*)\s+($Prophet::CLIContext::ID_REGEX)$/i => sub { | ||
| 196 | my $self = shift; | ||
| 197 | my $next = $1; | ||
| 198 | my $id = $2; | ||
| 199 | $self->context->set_id($id); | ||
| 200 | run("attachment $next", $self, @_); | ||
| 201 | }; | ||
| 202 | |||
| 203 | on content => run_command('Attachment::Content'); | ||
| 204 | on create => run_command('Attachment::Create'); | ||
| 205 | }; | ||
| 206 | |||
| 207 | # allow type to be specified via primary commands, e.g. | ||
| 208 | # 'sd ticket display --id 14' -> 'sd display --type ticket --id 14' | ||
| 209 | on qr{^(ticket|comment|attachment) \s+ (.*)}xi => sub { | ||
| 210 | my $self = shift; | ||
| 211 | my $type = $1; | ||
| 212 | my $redispatch_to = $2; | ||
| 213 | $self->context->set_arg(type => $type); | ||
| 214 | run($redispatch_to, $self, @_); | ||
| 215 | }; | ||
| 216 | |||
| 217 | redispatch_to('Prophet::CLI::Dispatcher'); | ||
| 218 | |||
| 219 | on '' => run_command('Shell'); | ||
| 220 | |||
| 221 | on qr/^(.*)$/ => sub { | ||
| 222 | my $self = shift; | ||
| 223 | my $command = $1; | ||
| 224 | die "The command you ran, '$command', could not be found. Perhaps running '" | ||
| 225 | .$self->cli->get_script_name."help' would help?\n"; | ||
| 226 | |||
| 227 | }; | ||
| 228 | |||
| 229 | sub run_command { Prophet::CLI::Dispatcher::run_command(@_) } | ||
| 230 | |||
| 231 | __PACKAGE__->meta->make_immutable; | ||
| 232 | no Any::Moose; | ||
| 233 | |||
| 234 | 1; | ||
| 235 |