File Coverage

blib/lib/Ixchel/Actions/base.pm
Criterion Covered Total %
statement 8 68 11.7
branch 0 34 0.0
condition 0 9 0.0
subroutine 3 8 37.5
pod 5 5 100.0
total 16 124 12.9


line stmt bran cond sub pod time code
1             package Ixchel::Actions::base;
2              
3 37     37   10534 use 5.006;
  37         172  
4 37     37   279 use strict;
  37         67  
  37         1103  
5 37     37   332 use warnings;
  37         78  
  37         35998  
6              
7             =head1 NAME
8              
9             Ixchel::Actions::base - Base module for actions.
10              
11             =head1 VERSION
12              
13             Version 0.0.1
14              
15             =cut
16              
17             our $VERSION = '0.0.1';
18              
19             =head1 SYNOPSIS
20              
21             package Ixchel::Actions::install_cpanm;
22              
23             use strict;
24             use warnings;
25             use Ixchel::functions::install_cpanm;
26             use base 'Ixchel::Actions::base';
27              
28             sub new_extra {
29             }
30              
31             sub action {
32             my $self = $_[0];
33              
34             $self->status_add(status=>'Installing cpanm via packges');
35              
36             eval{
37             install_cpanm;
38             };
39             if ($@) {
40             $self->status_add(status=>'Failed to install cpanm via packages ... '.$@, error=>1);
41             }else {
42             $self->status_add(status=>'cpanm installed');
43             }
44              
45             if (!defined($self->{results}{errors}[0])) {
46             $self->{results}{ok}=1;
47             }else {
48             $self->{results}{ok}=0;
49             }
50              
51             return $self->{results};
52             }
53              
54             sub short {
55             return 'Install cpanm via packages.';
56             }
57              
58             =head2 DESCRIPTION
59              
60             =head1 RESULT HASH REF
61              
62             .errors :: A array of errors encountered.
63             .status_text :: A string description of what was done and the results.
64             .ok :: Set to zero if any of the above errored.
65              
66             =cut
67              
68             =head1 METHODS
69              
70             =head2 new
71              
72             This initiates the action. At the ned of the new method, $self->new_extra; is called.
73             This method should exist in the action package. It is for anything else that needs done
74             for new. If nothing it should just be empty.
75              
76             The returned object has the following keys.
77              
78             - $self->{config} :: The config hash for Ixchel.
79              
80             - $self->{vars} :: The value of vars passed to the new method.
81              
82             - $self->{opts} :: Decoded opts as specifed via opts data.
83              
84             - $self->{argv} :: Left over arguments post decoding opts.
85              
86             - $self->{ixchel} :: The calling Ixchel object.
87              
88             - $self->{share_dir} :: Location of the share dir.
89              
90             - $self->{type} :: The type that will be used with status_add.
91              
92             - $self->{t} :: The Template object Initiated via Ixchel.
93              
94             =cut
95              
96             sub new {
97 0     0 1   my ( $empty, %opts ) = @_;
98              
99 0           my $class = shift;
100              
101 0           my $self = {
102             config => {},
103             vars => {},
104             arggv => [],
105             opts => {},
106             no_print => 0,
107             };
108 0           bless $self, $class;
109              
110 0           $self->{type} = ref($self);
111 0           $self->{type} =~ s/^Ixchel\:\:Actions\:\://;
112              
113 0 0         if ( defined( $opts{config} ) ) {
114 0           $self->{config} = $opts{config};
115             }
116              
117 0 0         if ( defined( $opts{t} ) ) {
118 0           $self->{t} = $opts{t};
119             } else {
120 0           die('$opts{t} is undef');
121             }
122              
123 0 0         if ( defined( $opts{share_dir} ) ) {
124 0           $self->{share_dir} = $opts{share_dir};
125             }
126              
127 0 0         if ( defined( $opts{opts} ) ) {
128 0           $self->{opts} = \%{ $opts{opts} };
  0            
129             }
130              
131 0 0         if ( defined( $opts{argv} ) ) {
132 0           $self->{argv} = $opts{argv};
133             }
134              
135 0 0         if ( defined( $opts{vars} ) ) {
136 0           $self->{vars} = $opts{vars};
137             }
138              
139 0 0         if ( defined( $opts{ixchel} ) ) {
140 0           $self->{ixchel} = $opts{ixchel};
141             }
142              
143             $self->{results} = {
144 0           errors => [],
145             status_text => '',
146             ok => 0,
147             };
148              
149 0           $self->new_extra;
150              
151 0           return $self;
152             } ## end sub new
153              
154             =head2 action
155              
156             Will call $self->action_extra.
157              
158             Upon undef it will check if $self->{results}{errors}[0]
159             is defined and if it is not $self->{results}{ok} is set
160             to 1.
161              
162             Upon $self->{results} being returned, it makes it checks if
163             $results->{errors} is defined, is a array and then if
164             it is a array it will check $results->{errors}[0] is defined
165             for setting $results->{ok} before returning $results.
166              
167             Any other results return will be returned as is.
168              
169             Or as code it does...
170              
171             if ( defined($results)
172             && ref($results) eq 'HASH'
173             && defined( $self->{results}{errors} )
174             && ref( $results->{results}{errors} ) eq 'ARRAY' )
175             {
176             if ( !defined( $self->{results}{errors}[0] ) ) {
177             $self->{results}{ok} = 1;
178             }
179             return $self->{results};
180             }elsif (!defined($results)) {
181             if ( !defined( $self->{results}{errors}[0] ) ) {
182             $self->{results}{ok} = 1;
183             }
184             return $self->{results};
185             }
186              
187             return $results;
188              
189             =cut
190              
191             sub action {
192 0     0 1   my ($self) = @_;
193              
194 0           my $results;
195 0           eval { $results = $self->action_extra; };
  0            
196 0 0         if ($@) {
197 0           $self->status_add( error => 1, '$self->action_extra died... ' . $@ );
198 0           return $self->{results};
199             }
200              
201 0 0 0       if ( defined($results)
    0 0        
      0        
202             && ref($results) eq 'HASH'
203             && defined( $self->{results}{errors} )
204             && ref( $results->{results}{errors} ) eq 'ARRAY' )
205             {
206 0 0         if ( !defined( $self->{results}{errors}[0] ) ) {
207 0           $self->{results}{ok} = 1;
208             }
209 0           return $self->{results};
210             } elsif ( !defined($results) ) {
211 0 0         if ( !defined( $self->{results}{errors}[0] ) ) {
212 0           $self->{results}{ok} = 1;
213             }
214              
215 0           return $self->{results};
216             }
217              
218 0           return $results;
219             } ## end sub action
220              
221             =head2 status_add
222              
223             Adds a item to $self->{results}{status_text}.
224              
225             The following are required.
226              
227             - status :: Status to add.
228             Default :: undef
229              
230             The following are optional.
231              
232             - error :: A Perl boolean for if it is a error or not. If true
233             it will be pushed to the array $self->{results}{errors}.
234             Default :: undef
235              
236             - type :: What to display the current type as in the status line.
237             Default :: $action
238              
239             $self->status_add(status=>'Some status');
240              
241             $self->status_add(error=>1, status=>'Some error');
242              
243             =cut
244              
245             sub status_add {
246 0     0 1   my ( $self, %opts ) = @_;
247              
248 0 0         if ( !defined( $opts{status} ) ) {
249 0           return;
250             }
251              
252 0 0         if ( !defined( $opts{error} ) ) {
253 0           $opts{error} = 0;
254             }
255              
256 0 0         if ( !defined( $opts{type} ) ) {
257 0           $opts{type} = $self->{type};
258             }
259              
260 0           my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
261 0           my $timestamp = sprintf( "%04d-%02d-%02dT%02d:%02d:%02d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec );
262              
263 0           my $status = '[' . $timestamp . '] [' . $opts{type} . ', ' . $opts{error} . '] ' . $opts{status};
264              
265 0 0         if ( !$self->{no_print} ) {
266 0           print $status. "\n";
267             }
268              
269 0           $self->{results}{status_text} = $self->{results}{status_text} . $status;
270              
271 0 0         if ( $opts{error} ) {
272 0           push( @{ $self->{results}{errors} }, $opts{status} );
  0            
273              
274 0           eval { $self->status_add_error_extra; };
  0            
275             }
276             } ## end sub status_add
277              
278             =head2 short
279              
280             The default short just returns ''.
281              
282             =cut
283              
284             sub short {
285 0     0 1   return '';
286             }
287              
288             =head2 opts_data
289              
290             The default opts_data returns "\n".
291              
292             =cut
293              
294             sub opts_data {
295 0     0 1   return '
296             ';
297             }
298              
299             1;