Silki/Role/CLI/HasOptionalProcess.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 90 CC 6 SCAR 16.8
CC
6
Cov
0%
run
12.0
_log_coderef
6.0
_handle_error
6.0
CRAP 5.4
1 package Silki::Role::CLI::HasOptionalProcess;
2 {
3 $Silki::Role::CLI::HasOptionalProcess::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9  
10 use Silki::Schema::Process;
11 use Silki::Types qw( Str );
12  
13 use Moose::Role;
14 use Moose::Util::TypeConstraints;
15  
16 requires qw( _run _final_result_string _print_success_message );
17  
18 {
19 subtype 'Process', as 'Silki::Schema::Process';
20 coerce 'Process',
21 from Str,
22 via { Silki::Schema::Process->new( process_id => $_ ) };
23  
24 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
25 'Process' => '=s' );
26  
27 has process => (
28 is => 'ro',
29 isa => 'Process',
30 coerce => 1,
31 documentation =>
32 'A process id. If given, this process is updated instead of sending output to the console',
33 );
34 }
35  
36 sub run {
37 my $self = shift;
38  
39 $self->process()->update(
40 status => 'Starting work',
41 system_pid => $$,
42 ) if $self->process();
43  
44 my @results = eval { $self->_run() };
45  
46 if ( my $e = $@ ) {
47 $self->_handle_error($e);
48 }
49 else {
50 $self->_handle_success(@results);
51 }
52 }
53  
54 sub _log_coderef {
55 my $self = shift;
56  
57 if ( $self->process() ) {
58 my $process = $self->process();
59  
60 return sub { $process->update( status => join '', @_ ) };
61 }
62 else {
63 return sub { print q{ }, @_, "\n" };
64 }
65 }
66  
67 sub _handle_error {
68 my $self = shift;
69 my $error = shift;
70  
71 if ( $self->process() ) {
72 $self->process()->update(
73 status => "Error doing work: $error",
74 is_complete => 1,
75 );
76 }
77 else {
78 die $error;
79 }
80  
81 exit 1;
82 }
83  
84 sub _handle_success {
85 my $self = shift;
86  
87 if ( $self->process() ) {
88 $self->process()->update(
89 status => 'Completed work',
90 is_complete => 1,
91 was_successful => 1,
92 final_result => $self->_final_result_string(@_),
93 );
94 }
95 else {
96 $self->_print_success_message(@_);
97 }
98  
99 exit 0;
100 }
101  
102 if ( eval "use Getopt::Long::Descriptive; 1;"
103 && Getopt::Long::Descriptive->VERSION < 0.087 ) {
104 eval <<'EOF';
105 {
106 package
107 Getopt::Long::Descriptive::Usage;
108 no warnings 'redefine';
109  
110 sub option_text {
111 my ($self) = @_;
112  
113 my @options = @{ $self->{options} || [] };
114 my $string = q{};
115  
116 # a spec can grow up to 4 characters in usage output:
117 # '-' on short option, ' ' between short and long, '--' on long
118 my @specs = map { $_->{spec} } grep { $_->{desc} ne 'spacer' } @options;
119 my $length = (max(map { length } @specs) || 0) + 4;
120 my $spec_fmt = "\t%-${length}s";
121  
122 while (@options) {
123 my $opt = shift @options;
124 my $spec = $opt->{spec};
125 my $desc = $opt->{desc};
126 if ($desc eq 'spacer') {
127 $string .= sprintf "$spec_fmt\n", $opt->{spec};
128 next;
129 }
130  
131 $spec = Getopt::Long::Descriptive->_strip_assignment($spec);
132 $spec = join " ", reverse map { length > 1 ? "--$_" : "-$_" }
133 split /\|/, $spec;
134  
135 my @desc = $self->_split_description($length, $desc);
136  
137 $string .= sprintf "$spec_fmt %s\n", $spec, shift @desc;
138 for my $line (@desc) {
139 $string .= "\t";
140 $string .= q{ } x ( $length + 2 );
141 $string .= "$line\n";
142 }
143 }
144  
145 return $string;
146 }
147  
148 sub _split_description {
149 my ($self, $length, $desc) = @_;
150  
151 # 8 for a tab, 2 for the space between option & desc;
152 my $max_length = 78 - ( $length + 8 + 2 );
153  
154 return $desc if length $desc <= $max_length;
155  
156 my @lines;
157 while (length $desc > $max_length) {
158 my $idx = rindex( substr( $desc, 0, $max_length ), q{ }, );
159 push @lines, substr($desc, 0, $idx);
160 substr($desc, 0, $idx + 1) = q{};
161 }
162 push @lines, $desc;
163  
164 return @lines;
165 }
166 }
167 EOF
168 }
169  
170 1;