File Coverage

blib/lib/Toader/Config.pm
Criterion Covered Total %
statement 15 192 7.8
branch 0 78 0.0
condition n/a
subroutine 5 14 35.7
pod 9 9 100.0
total 29 293 9.9


line stmt bran cond sub pod time code
1             package Toader::Config;
2              
3 2     2   21385 use warnings;
  2         5  
  2         78  
4 2     2   13 use strict;
  2         5  
  2         53  
5 2     2   13 use base 'Error::Helper';
  2         24  
  2         1093  
6 2     2   1570 use Sys::Hostname;
  2         1207  
  2         92  
7 2     2   1561 use Config::Tiny;
  2         2076  
  2         3547  
8              
9             =head1 NAME
10              
11             Toader::Config - Represents the Toader config.
12              
13             =head1 VERSION
14              
15             Version 1.0.0
16              
17             =cut
18              
19             our $VERSION = '1.0.0';
20              
21             =head1 METHODS
22              
23             =head2 new
24              
25             This initiates the object.
26              
27             One argument is required and it is a Toader object.
28              
29             my $foo = Toader::Config->new( $toader );
30             if ( $foo->error ){
31             warn('error:'.$foo->error.': '.$foo->errorString);
32             }
33              
34             =cut
35              
36             sub new{
37 0     0 1   my $toader=$_[1];
38              
39 0           my $self={
40             error=>undef,
41             errorString=>'',
42             perror=>undef,
43             errorExtra=>{
44             flags=>{
45             1=>'noToaderObj',
46             2=>'notAtoaderObj',
47             3=>'noDirSet',
48             4=>'configReadFailed',
49             5=>'sectionDoesNotExist',
50             6=>'noVariableSpecified',
51             7=>'invalidVariableOrSection',
52             8=>'configWriteFailed',
53             9=>'getVCSerrored',
54             10=>'VCSusableErrored',
55             11=>'underVCSerrored',
56             12=>'VCSaddErrored',
57             },
58             },
59             VCSusable=>0,
60             };
61 0           bless $self;
62              
63             #make sure a Toader object is specified
64 0 0         if ( ! defined( $toader ) ){
65 0           $self->{perror}=1;
66 0           $self->{error}=1;
67 0           $self->{errorString}='No toader object specified';
68 0           $self->warn;
69 0           return $self;
70             }
71              
72             #make sure it is a Toader object
73 0 0         if ( ref( $toader ) ne 'Toader' ){
74 0           $self->{perror}=1;
75 0           $self->{error}=2;
76 0           $self->{errorString}='The passed object is "'.ref( $toader ).'" and not a Toader object';
77 0           $self->warn;
78 0           return $self;
79             }
80              
81             #saves the Toader object
82 0           $self->{toader}=$toader;
83              
84             #gets the directory
85 0           $self->{dir}=$self->{toader}->getRootDir;
86 0 0         if ( ! defined( $self->{dir} ) ){
87 0           $self->{perror}=1;
88 0           $self->{error}=3;
89 0           $self->{errorString}='The Toader object did not return a directory';
90 0           $self->warn;
91 0           return $self;
92             }
93              
94             #this handles the toader config file
95 0           $self->{configFile}=$self->{dir}.'/.toader/config.ini';
96 0 0         if ( -f $self->{configFile} ){
97 0           $self->{config}=Config::Tiny->read( $self->{configFile} );
98 0 0         if ( ! defined( $self->{config} ) ){
99 0           $self->{perror}='1';
100 0           $self->{error}=4;
101             $self->{errorString}='Unable to read the config file, "'.
102 0           $self->{configFile}.'",';
103 0           $self->warn;
104 0           return $self;
105             }
106             }else{
107 0           $self->{config}=Config::Tiny->new;
108             }
109              
110             #sets some defaults for the config if they are not set
111             #default to the site name being the hostname
112 0 0         if ( ! defined( $self->{config}->{_}->{site} ) ){
113 0           $self->{config}->{_}->{site}=hostname;
114             }
115             #sets the owner if one is not specified
116 0 0         if ( ! defined( $self->{config}->{_}->{'owner'} ) ){
117 0           $self->{config}->{_}->{'owner'}=getlogin.'@'.hostname;
118             }
119             #sets the default last
120 0 0         if ( ! defined( $self->{config}->{_}->{'last'} ) ){
121 0           $self->{config}->{_}->{'last'}=25;
122             }
123             #disable vcs by default
124 0 0         if ( ! defined( $self->{config}->{_}->{'vcs'} ) ){
125 0           $self->{config}->{_}->{'vcs'}=0;
126             }
127              
128             #gets the Toader::VCS object
129 0           $self->{vcs}=$self->{toader}->getVCS;
130 0 0         if ( $toader->error ){
131 0           $self->{perror}=1;
132 0           $self->{error}=9;
133             $self->{errorString}='Toader->getVCS errored. error="'.
134 0           $self->{toader}->error.'" errorString="'.$self->{toader}->errorString.'"';
135 0           $self->warn;
136 0           return $self;
137             }
138              
139             #checks if VCS is usable
140 0           $self->{VCSusable}=$self->{vcs}->usable;
141 0 0         if ( $self->{vcs}->error ){
142 0           $self->{perror}=1;
143 0           $self->{error}=10;
144             $self->{errorString}='Toader::VCS->usable errored. error="'.
145 0           $self->{toader}->error.'" errorString="'.$self->{toader}->errorString.'"';
146 0           $self->warn;
147 0           return $self;
148             }
149              
150 0           return $self;
151             }
152              
153             =head2 getConfig
154              
155             This returns the L object storing the Toader
156             config.
157              
158             There is no need to do any error checking as long as
159             Toader new suceeded with out issue.
160              
161             my $config=$foo->getConfig;
162              
163             =cut
164              
165             sub getConfig{
166 0     0 1   my $self=$_[0];
167            
168             #blank any previous errors
169 0 0         if(!$self->errorblank){
170 0           return undef;
171             }
172              
173 0           return $self->{config};
174             }
175              
176             =head2 getConfigFile
177              
178             This returns the config file for Toader.
179              
180             my $configFile=$foo->getConfigFile;
181              
182             =cut
183              
184             sub getConfigFile{
185 0     0 1   my $self=$_[0];
186            
187             #blank any previous errors
188 0 0         if(!$self->errorblank){
189 0           return undef;
190             }
191              
192 0           return $self->{configFile};
193             }
194              
195             =head2 listSections
196              
197             This returns a list of sections.
198              
199             my @sections=$foo->listSections;
200              
201             =cut
202              
203             sub listSections{
204 0     0 1   my $self=$_[0];
205            
206             #blank any previous errors
207 0 0         if(!$self->errorblank){
208 0           return undef;
209             }
210              
211 0           return keys( %{ $self->{config} } );
  0            
212             }
213              
214             =head2 listVariables
215              
216             This returns a list of variables for a section.
217              
218             my @variables=$foo->listVariables( $section );
219             if ( $foo->error ){
220             warn( 'error:'.$foo->error.': '.$foo->errorString );
221             }
222              
223             =cut
224              
225             sub listVariables{
226 0     0 1   my $self=$_[0];
227 0           my $section=$_[1];
228              
229             #blank any previous errors
230 0 0         if(!$self->errorblank){
231 0           return undef;
232             }
233              
234             # make sure a variable is specifed
235 0 0         if ( ! defined( $section ) ){
236 0           $self->{error}='6';
237 0           $self->{erroRstring}='No variable name specified';
238 0           $self->warn;
239 0           return undef;
240             }
241              
242             #default to _
243 0 0         if ( ! defined( $section ) ){
244 0           $section='_';
245             }
246              
247             # make sure the section exists
248 0 0         if ( ! defined( $self->{config}->{$section} ) ){
249 0           $self->{error}='5';
250 0           $self->{erroRstring}='The section "'.$section.'" does not exist';
251 0           $self->warn;
252 0           return undef;
253             }
254              
255 0           return keys( %{ $self->{config}->{$section} } );
  0            
256             }
257              
258             =head2 valueDel
259              
260             This deletes a specified value.
261              
262             Two arguments are taken. The first is the section. If not
263             specified, "_" is used. The second and required one is the
264             variable name.
265              
266             As long as the section exists, which it always will for '_',
267             and a variable name is specified, this won't error.
268              
269             $foo->valueDel( $section, $variable );
270              
271             =cut
272              
273             sub valueDel{
274 0     0 1   my $self=$_[0];
275 0           my $section=$_[1];
276 0           my $variable=$_[2];
277              
278             #blank any previous errors
279 0 0         if(!$self->errorblank){
280 0           return undef;
281             }
282              
283             # make sure a variable is specifed
284 0 0         if ( ! defined( $variable ) ){
285 0           $self->{error}='6';
286 0           $self->{erroRstring}='No variable name specified';
287 0           $self->warn;
288 0           return undef;
289             }
290              
291             #default to _
292 0 0         if ( ! defined( $section ) ){
293 0           $section='_';
294             }
295              
296             # make sure the section exists
297 0 0         if ( ! defined( $self->{config}->{$section} ) ){
298 0           $self->{error}='5';
299 0           $self->{erroRstring}='The section "'.$section.'" does not exist';
300 0           $self->warn;
301 0           return undef;
302             }
303              
304 0 0         if ( ! defined( $self->{config}->{$section}->{$variable} ) ){
305 0           return 1;
306             }
307            
308 0           delete( $self->{config}->{$section}->{$variable} );
309            
310 0           return 1;
311             }
312              
313             =head2 valueGet
314              
315             This returns a value that has been set for a variable.
316              
317             Two arguments are taken. The first is the section. If not
318             specified, "_" is used. The second and required one is the
319             variable name.
320              
321             As long as the section exists, which it always will for '_',
322             and a variable name is specified, this won't error.
323              
324             If a value does not exist, undef is returned.
325              
326             my $value=$foo->valueGet( $section, $variable );
327              
328             =cut
329              
330             sub valueGet{
331 0     0 1   my $self=$_[0];
332 0           my $section=$_[1];
333 0           my $variable=$_[2];
334            
335             #blank any previous errors
336 0 0         if(!$self->errorblank){
337 0           return undef;
338             }
339            
340             # make sure a variable is specifed
341 0 0         if ( ! defined( $variable ) ){
342 0           $self->{error}='6';
343 0           $self->{erroRstring}='No variable name specified';
344 0           $self->warn;
345 0           return undef;
346             }
347            
348             #default to _
349 0 0         if ( ! defined( $section ) ){
350 0           $section='_';
351             }
352            
353             # make sure the section exists
354 0 0         if ( ! defined( $self->{config}->{$section} ) ){
355 0           $self->{error}='5';
356 0           $self->{erroRstring}='The section "'.$section.'" does not exist';
357 0           $self->warn;
358 0           return undef;
359             }
360            
361 0           return $self->{config}->{$section}->{$variable};
362             }
363              
364             =head2 valueSet
365              
366             This sets a new value for the config.
367              
368             Third arguments are taken. The first is the section. If not
369             specified, "_" is used. The second and required one is the
370             variable name. The third and required is the the value.
371              
372             If the specified section does not exist, a new one will be created.
373              
374             Neither the section or variable name can match /[\t \n\=\#\;]/.
375              
376             my $value=$foo->valueSet( $section, $variable, $value );
377              
378             =cut
379              
380             sub valueSet{
381 0     0 1   my $self=$_[0];
382 0           my $section=$_[1];
383 0           my $variable=$_[2];
384 0           my $value=$_[2];
385            
386             #blank any previous errors
387 0 0         if(!$self->errorblank){
388 0           return undef;
389             }
390              
391             # make sure a variable is specifed
392 0 0         if ( ! defined( $variable ) ){
393 0           $self->{error}=6;
394 0           $self->{erroRstring}='No variable name specified';
395 0           $self->warn;
396 0           return undef;
397             }
398              
399             #default to _
400 0 0         if ( ! defined( $section ) ){
401 0           $section='_';
402             }
403              
404             #makes sure a valid section and variable is specified
405 0 0         if ( $variable =~ /[\t \n\=\#\;]/ ){
406 0           $self->{error}=7;
407 0           $self->{errorString}='The variable,"'.$variable.'", matched /[\t \n\=\#\;]/';
408 0           $self->warn;
409 0           return undef;
410             }
411 0 0         if ( $section =~ /[\t \n\=\#\;]/ ){
412 0           $self->{error}=7;
413 0           $self->{errorString}='The section,"'.$section.'", matched /[\t \n\=\#\;]/';
414 0           $self->warn;
415 0           return undef;
416             }
417              
418             #set it if no section has been created yet
419 0 0         if ( ! defined( $self->{config}->{$section} ) ){
420 0           $self->{config}->{$section}={ $variable=>$value };
421             }
422              
423 0           return 1;
424             }
425              
426             =head2 write
427              
428             Writes the config out to the Toader config file.
429              
430             $foo->write;
431             if ( $foo->error ){
432             warn('error:'.$foo->error.': '.$foo->errorString);
433             }
434              
435             =cut
436              
437             sub write{
438 0     0 1   my $self=$_[0];
439            
440             #blank any previous errors
441 0 0         if(!$self->errorblank){
442 0           return undef;
443             }
444              
445             #try to write the config out and error if it does not
446 0 0         if ( ! $self->{config}->write( $self->{configFile} ) ){
447 0           $self->{error}=8;
448 0           $self->{errorString}='Failed to write the config out';
449 0           $self->warn;
450 0           return undef;
451             }
452              
453             #if VCS is not usable, stop here
454 0 0         if ( ! $self->{VCSusable} ){
455 0           return 1;
456             }
457              
458             #if it is under VCS, we have nothing to do
459 0           my $underVCS=$self->{vcs}->underVCS( $self->{configFile} );
460 0 0         if ( $self->{vcs}->error ){
461 0           $self->{error}=11;
462             $self->{errorString}='Toader::VCS->underVCS errored. error="'.
463 0           $self->{vcs}->error.'" errorString="'.$self->{vcs}->errorString.'"';
464 0           $self->warn;
465 0           return undef;
466             }
467 0 0         if ( $underVCS ){
468 0           return 1;
469             }
470              
471             #add it as if we reach here it is not under VCS and VCS is being used
472 0           $self->{vcs}->add( $self->{configFile} );
473 0 0         if ( $self->{vcs}->error ){
474 0           $self->{error}=12;
475             $self->{errorString}='Toader::VCS->add errored. error="'.
476 0           $self->{vcs}->error.'" errorString="'.$self->{vcs}->errorString.'"';
477 0           $self->warn;
478 0           return undef;
479             }
480              
481 0           return 1;
482             }
483              
484             =head1 ERROR CODES/FLAGS
485              
486             =head2 1, noToaderObj
487              
488             No L object specified.
489              
490             =head2 2, notAtoaderObj
491              
492             The specified object is not a L object.
493              
494             =head2 3, noDirSet
495              
496             The L object did not return a directory.
497              
498             =head2 4, configReadFailed
499              
500             Failed to read the config file.
501              
502             =head2 5, sectionDoesNotExist
503              
504             The section does not exist.
505              
506             =head2 6, noVariableSpecified
507              
508             No variable name specified.
509              
510             =head2 7, invalidVariableOrSection
511              
512             Variable or section matched /[\t \n\=\#\;]/.
513              
514             =head2 8, configWriteFailed
515              
516             Failed to write the config out.
517              
518             =head2 9, getVCSerrored
519              
520             L->getVCS errored.
521              
522             =head2 10, VCSusableErrored
523              
524             L->usable errored.
525              
526             =head2 11, underVCSerrored
527              
528             L->underVCS errored.
529              
530             =head2 12, VCSaddErrored
531              
532             L->add errored.
533              
534             =head1 AUTHOR
535              
536             Zane C. Bowers-Hadley, C<< >>
537              
538             =head1 BUGS
539              
540             Please report any bugs or feature requests to C, or through
541             the web interface at L. I will be notified, and then you'll
542             automatically be notified of progress on your bug as I make changes.
543              
544              
545              
546              
547             =head1 SUPPORT
548              
549             You can find documentation for this module with the perldoc command.
550              
551             perldoc Toader::Config
552              
553              
554             You can also look for information at:
555              
556             =over 4
557              
558             =item * RT: CPAN's request tracker
559              
560             L
561              
562             =item * AnnoCPAN: Annotated CPAN documentation
563              
564             L
565              
566             =item * CPAN Ratings
567              
568             L
569              
570             =item * Search CPAN
571              
572             L
573              
574             =back
575              
576              
577             =head1 ACKNOWLEDGEMENTS
578              
579              
580             =head1 LICENSE AND COPYRIGHT
581              
582             Copyright 2011 Zane C. Bowers-Hadley.
583              
584             This program is free software; you can redistribute it and/or modify it
585             under the terms of either: the GNU General Public License as published
586             by the Free Software Foundation; or the Artistic License.
587              
588             See http://dev.perl.org/licenses/ for more information.
589              
590              
591             =cut
592              
593             1; # End of Toader::Config