| 1 | package App::SD::CLI::Command::Ticket::Create; | ||
| 2 | use Any::Moose; | ||
| 3 | |||
| 4 | use Params::Validate qw/validate/; | ||
| 5 | extends 'Prophet::CLI::Command::Create'; | ||
| 6 | with 'App::SD::CLI::Model::Ticket'; | ||
| 7 | with 'App::SD::CLI::Command'; | ||
| 8 | with 'Prophet::CLI::TextEditorCommand'; | ||
| 9 | |||
| 10 | sub ARG_TRANSLATIONS { shift->SUPER::ARG_TRANSLATIONS(), e => 'edit' }; | ||
| 11 | |||
| 12 | # use actual valid ticket props in the help message, and make note of the | ||
| 13 | # interactive editing mode | ||
| 14 | override usage_msg => sub { | ||
| 15 | my $self = shift; | ||
| 16 | my $cmd = $self->cli->get_script_name; | ||
| 17 | |||
| 18 | my @primary_commands = @{ $self->context->primary_commands }; | ||
| 19 | |||
| 20 | # if primary commands was only length 1, the type was not specified | ||
| 21 | # and we should indicate that a type is expected | ||
| 22 | push @primary_commands, '<record-type>' if @primary_commands == 1; | ||
| 23 | |||
| 24 | my $type_and_subcmd = join( q{ }, @primary_commands ); | ||
| 25 | |||
| 26 | return <<"END_USAGE"; | ||
| 27 | usage: ${cmd}${type_and_subcmd} -- summary=foo status=open | ||
| 28 | ${cmd}${type_and_subcmd} [--edit] | ||
| 29 | END_USAGE | ||
| 30 | }; | ||
| 31 | |||
| 32 | # we want to launch an $EDITOR window to grab props and a comment if no | ||
| 33 | # props are specified on the commandline | ||
| 34 | |||
| 35 | override run => sub { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | $self->print_usage if $self->has_arg('h'); | ||
| 39 | |||
| 40 | my @prop_set = $self->prop_set; | ||
| 41 | my $record = $self->_get_record_object; | ||
| 42 | |||
| 43 | # only invoke editor if no props specified on the commandline or edit arg specified | ||
| 44 | return super() if (@{$self->prop_set} && !$self->has_arg('edit')); | ||
| 45 | |||
| 46 | my $template_to_edit = $self->create_record_template(); | ||
| 47 | |||
| 48 | my $done = 0; | ||
| 49 | |||
| 50 | while (!$done) { | ||
| 51 | $done = $self->try_to_edit( template => \$template_to_edit, record => $record); | ||
| 52 | } | ||
| 53 | |||
| 54 | }; | ||
| 55 | |||
| 56 | sub process_template { | ||
| 57 | my $self = shift; | ||
| 58 | my %args = validate( @_, { template => 1, edited => 1, record => 1 } ); | ||
| 59 | |||
| 60 | my $record = $args{record}; | ||
| 61 | my $updated = $args{edited}; | ||
| 62 | ( my $props_ref, my $comment ) = $self->parse_record_template($updated); | ||
| 63 | |||
| 64 | for my $prop ( keys %$props_ref ) { | ||
| 65 | $self->context->set_prop( $prop => $props_ref->{$prop} ); | ||
| 66 | } | ||
| 67 | |||
| 68 | my $error; | ||
| 69 | local $@; | ||
| 70 | eval { super(); } or chomp ($error = $@ || "Something went wrong!"); | ||
| 71 | |||
| 72 | return $self->handle_template_errors( | ||
| 73 | error => $error . "\n\nYou can bypass validation for a " | ||
| 74 | ."property by appending a ! to it.", | ||
| 75 | template_ref => $args{template}, | ||
| 76 | bad_template => $updated, | ||
| 77 | rtype => $record->type, | ||
| 78 | ) if ($error); | ||
| 79 | |||
| 80 | $self->add_comment( content => $comment, uuid => $self->record->uuid ) | ||
| 81 | if $comment; | ||
| 82 | |||
| 83 | return 1; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | __PACKAGE__->meta->make_immutable; | ||
| 89 | no Any::Moose; | ||
| 90 | |||
| 91 | 1; |