blib/lib/CPAN/Mini/Inject/Config.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 89.2% 25 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 6 / 6 totaltotal 94.8% 92 / 97 SCAR 28.4
CC
17
Cov
95%
parse_config
9.0
_find_config
5.0
config_file
3.0
CRAP 17.0
« blib/lib/CPAN/Mini/Inject.pm ↑ Summary Build »
1 package CPAN::Mini::Inject::Config;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use File::Spec::Functions qw(rootdir catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 CPAN::Mini::Inject::Config - Config for CPAN::Mini::Inject
12  
13 =head1 VERSION
14  
15 Version 0.35
16  
17 =cut
18  
19 14 our $VERSION = '0.35';
20  
21 =head2 C<new>
22  
23 =cut
24  
25 18 ▶sub new { bless { file => undef }, $_[0] }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
26  
27 =head2 C<< config_file( [FILE] ) >>
28  
29 =cut
30  
31 sub config_file {
32 71 ▶ my ( $self, $file ) = @_;
Subroutine config_file: called CC=3 SCAR=11.0
Pod documented
33  
34 71 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 20 51 71
35 20 ▶ croak( "Could not read file [$file]!" ) unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 20 20
36 20 $self->{file} = $file;
37 }
38  
39 71 $self->{file};
40 }
41  
42 =head2 C<< load_config() >>
43  
44 loadcfg accepts a CPAN::Mini::Inject config file or if not defined
45 will search the following four places in order:
46  
47 =over 4
48  
49 =item * file pointed to by the environment variable MCPANI_CONFIG
50  
51 =item * $HOME/.mcpani/config
52  
53 =item * /usr/local/etc/mcpani
54  
55 =item * /etc/mcpani
56  
57 =back
58  
59  
60 loadcfg sets the instance variable cfgfile to the file found or undef if
61 none is found.
62  
63 print "$mcpi->{cfgfile}\n"; # /etc/mcpani
64  
65 =cut
66  
67 sub load_config {
68 21 ▶ my $self = shift;
Subroutine load_config: called CC=3 SCAR=11.0
Pod documented
69  
70 21 ▶ my $cfgfile = shift || $self->_find_config;
Condition100%3/3
shift() || $self->_find_config
l!l&&r!l&&!r
1821
MC/DC100%2/2
shift() || $self->_find_config
shift() $self->_find_config
Truth table100%3/3
A || B
A= shift()B= $self->_find_config
ABresult
1X1
011
000
71  
72 21 ▶ croak 'Unable to find config file' unless $cfgfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfgfile 1 20 21
73 20 $self->config_file( $cfgfile );
74  
75 20 return $cfgfile;
76 }
77  
78 sub _find_config {
79 my ( @files ) = (
80 $ENV{MCPANI_CONFIG},
81 (
82 defined $ENV{HOME}
83 3 ▶ ? catfile( $ENV{HOME}, qw(.mcpani config) )
Subroutine _find_config: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'HOME'} ? : 2 1 3
84 : ()
85 ),
86 catfile( rootdir(), qw(usr local etc mcpani) ),
87 catfile( rootdir(), qw(etc mcpani) ),
88 );
89  
90 3 for my $file ( @files ) {
91 6 ▶ next unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 2 4 6
92 4 ▶ next unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 2 2 4
93  
94 2 return $file;
95 }
96  
97 1 return;
98 }
99  
100 =head2 C<< parse_config() >>
101  
102 parsecfg reads the config file stored in the instance variable cfgfile and
103 creates a hash in config with each setting.
104  
105 $mcpi->{config}{remote} # CPAN sites to mirror from.
106  
107 parsecfg expects the config file in the following format:
108  
109 local: /www/CPAN
110 remote: ftp://ftp.cpan.org/pub/CPAN ftp://ftp.kernel.org/pub/CPAN
111 repository: /work/mymodules
112 passive: yes
113 dirmode: 0755
114  
115 Description of options:
116  
117 =over 4
118  
119 =item * local
120  
121 location to store local CPAN::Mini mirror (*REQUIRED*)
122  
123 =item * remote
124  
125 CPAN site(s) to mirror from. Multiple sites can be listed space separated.
126 (*REQUIRED*)
127  
128 =item * repository
129  
130 Location to store modules to add to the local CPAN::Mini mirror.
131  
132 =item * passive
133  
134 Enable passive FTP.
135  
136 =item * dirmode
137  
138 Set the permissions of created directories to the specified mode. The default
139 value is based on umask if supported.
140  
141 =back
142  
143 If either local or remote are not defined parsecfg croaks.
144  
145 =cut
146  
147 sub parse_config {
148 17 ▶ my $self = shift;
Subroutine parse_config: called CC=9 SCAR=22.0
Pod documented
149  
150 17 my $file = shift;
151  
152 17 my %required = map { $_, 1 } qw(local remote);
153  
154 17 ▶ $self->load_config( $file ) unless $self->config_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config_file 8 9 17
155  
156 17 ▶ if ( -r $self->config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $self->config_file) 17 0 17
157 17 ▶ open my ( $fh ), "<", $self->config_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->config_file or croak("Could not open config file: $!") 0 17 17
158 or croak( "Could not open config file: $!" );
159  
160 17 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 79 17 96
161 79 ▶ next if /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 2 77 79
162 77 ▶ $self->{$1} = $2 if /^\s*([^:\s]+)\s*:\s*(.*?)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*([^:\s]+)\s*:\s*(.*?)\s*$/ 76 1 77
163 77 ▶ delete $required{$1} if defined $required{$1};
Branch: 1 branch
BranchTrueFalseTotal
if defined $required{$1} 33 44 77
164 }
165  
166 17 close $fh;
167  
168 17 ▶ croak 'Required parameter(s): '
Branch: 1 branch
BranchTrueFalseTotal
if keys %required 1 16 17
169 . join( ' ', keys %required )
170 . ' missing.'
171 if keys %required;
172 }
173  
174 16 return $self;
175 }
176  
177 =head2 C<< get( DIRECTIVE ) >>
178  
179 Return the value for the named configuration directive.
180  
181 =cut
182  
183 127 ▶sub get { $_[0]->{ $_[1] } }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
184  
185 =head2 C<< set( DIRECTIVE, VALUE ) >>
186  
187 Sets the value for the named configuration directive.
188  
189 =cut
190  
191 0 ▶sub set { $_[0]->{ $_[1] } = $_[2] }
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
192  
193 =head1 CURRENT MAINTAINER
194  
195 Christian Walde C<< <walde.christian@googlemail.com> >>
196  
197 =head1 BUGS
198  
199 Please report any bugs or feature requests to
200 C<bug-cpan-mini-inject@rt.cpan.org>, or through the web interface at
201 L<http://rt.cpan.org>. I will be notified, and then you'll automatically
202 be notified of progress on your bug as I make changes.
203  
204 =cut
205  
206 14 1;
« blib/lib/CPAN/Mini/Inject.pm ↑ Summary Build »