File Coverage

blib/lib/Bot/Cobalt/Frontend/RC.pm
Criterion Covered Total %
statement 37 48 77.0
branch 6 12 50.0
condition 4 15 26.6
subroutine 10 12 83.3
pod 2 2 100.0
total 59 89 66.2


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Frontend::RC;
2             $Bot::Cobalt::Frontend::RC::VERSION = '0.021002';
3 1     1   821 use strictures 2;
  1         1079  
  1         33  
4              
5 1     1   139 use Carp;
  1         1  
  1         48  
6              
7 1     1   3 use Cwd ();
  1         1  
  1         10  
8 1     1   2 use File::Spec ();
  1         1  
  1         11  
9              
10 1     1   386 use Try::Tiny;
  1         1574  
  1         43  
11              
12 1     1   344 use Bot::Cobalt::Serializer;
  1         4  
  1         30  
13              
14 1     1   14 use parent 'Exporter::Tiny';
  1         1  
  1         6  
15              
16             our @EXPORT_OK = qw/
17             rc_read
18             rc_write
19             /;
20              
21             sub rc_read {
22 1     1 1 2 my ($rcfile) = @_;
23 1 50       3 croak "rc_read needs a rcfile path" unless $rcfile;
24              
25             my $generic_crappy_err = sub {
26 0     0   0 warn
27             "Errors reported during rcfile parse\n",
28             "You may have an old, incompatible, or broken rcfile.\n",
29             "Path: $rcfile\n",
30             "Try running cobalt2-installer\n" ;
31 1         3 };
32              
33 1         2 my $rc_err;
34             my $rc_h = try {
35 1     1   59 Bot::Cobalt::Serializer->new->readfile($rcfile)
36             } catch {
37 0     0   0 $generic_crappy_err->();
38 0         0 $rc_err = $_;
39             undef
40 1 50       4 } or croak "Could not rc_read(); readfile said $rc_err";
  0         0  
41            
42 1 50 33     39 unless ($rc_h && ref $rc_h eq 'HASH') {
43 0         0 $generic_crappy_err->();
44 0         0 croak "rc_read ($rcfile) expected to receive a hash"
45             }
46              
47 1         2 my ($BASE, $ETC, $VAR) = @$rc_h{'BASE', 'ETC', 'VAR'};
48            
49 1 50 33     6 unless ($BASE && $ETC && $VAR) {
      33        
50 0         0 warn "rc_read; could not find BASE, ETC, VAR\n";
51 0         0 warn "BASE: $BASE\nETC: $ETC\nVAR: $VAR\n";
52 0         0 croak "Cannot continue without a valid rcfile"
53             }
54            
55 1         6 return ($BASE, $ETC, $VAR)
56             }
57              
58             sub rc_write {
59 1     1 1 1481 my ($rcfile, $basepath) = @_;
60 1 50 33     7 croak "rc_write needs rc file path and base directory path"
61             unless $rcfile and $basepath;
62              
63 1 50       6 unless ( File::Spec->file_name_is_absolute($basepath) ) {
64 0   0     0 my $homedir = $ENV{HOME} || Cwd::cwd();
65 0         0 $basepath = File::Spec->catdir( $homedir, $basepath );
66             }
67              
68 1         14 my $rc_h = {
69             BASE => $basepath,
70             ETC => File::Spec->catdir( $basepath, 'etc' ),
71             VAR => File::Spec->catdir( $basepath, 'var' ),
72             };
73              
74 1         14 Bot::Cobalt::Serializer->new->writefile(
75             $rcfile, $rc_h
76             );
77              
78 1         12 $basepath
79             }
80              
81             1;
82             __END__