Inject/Config.pm

filter: Statementstmt 96.2% 51 / 53 Branchbran 85.7% 24 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 91.6% 11 / 12 Podpod 100% 6 / 6 totaltotal 91.3% 95 / 104 CC 17 SCAR 15.7
CC
17
Cov
92%
parse_config
9.0
_find_config
5.4
load_config
3.0
CRAP 4.8
« Inject.pm ↑ Summary
1 13 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package CPAN::Mini::Inject::Config;
3  
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $VERSION = '0.38';
8  
9 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use File::Spec::Functions qw(rootdir catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 CPAN::Mini::Inject::Config - Config for CPAN::Mini::Inject
15  
16 =head1 SYNOPSIS
17  
18 my $config = CPAN::Mini::Inject::Config->new;
19  
20 =head1 DESCRIPTION
21  
22 =head2 Configuration
23  
24 This is the default class dealing with the default L<CPAN::Mini::Inject>
25 config. The simplest config is a key-value file:
26  
27 local: t/local/CPAN
28 remote : http://localhost:11027
29 repository: t/local/MYCPAN
30 dirmode: 0775
31 passive: yes
32  
33 This module digests that and returns it as a hash reference. Any module
34 that wants to use a different sort of config structure needs to return
35 the same hash:
36  
37 {
38 local => 't/local/CPAN',
39 remote => 'http://localhost:11027',
40 repository => 't/local/MYCPAN',
41 dirmode => '0775',
42 passive => 'yes',
43 }
44  
45 =over 4
46  
47 =item * dirmode
48  
49 Set the permissions of created directories to the specified mode. The default
50 value is based on umask if supported.
51  
52 =item * force
53  
54 passthrough to L<CPAN::Mini>.
55  
56 =item * local
57  
58 (required) location to store local CPAN::Mini mirror
59  
60 =item * log_level
61  
62 passthrough to L<CPAN::Mini>
63  
64 =item * module_filters
65  
66 passthrough to L<CPAN::Mini>
67  
68 =item * passive
69  
70 Enable passive FTP.
71  
72 =item * remote
73  
74 (required) CPAN site(s) to mirror from. Multiple sites can be listed space separated.
75  
76 =item * repository
77  
78 Location to store modules to add to the local CPAN::Mini mirror.
79  
80 =item * skip_cleanup
81  
82 passthrough to L<CPAN::Mini>
83  
84 =item * skip_perl
85  
86 passthrough to L<CPAN::Mini>
87  
88 =item * trace
89  
90 passthrough to L<CPAN::Mini>
91  
92 =back
93  
94 =head2 Methods
95  
96 =over 4
97  
98 =item C<new>
99  
100 =cut
101  
102 22 ▶sub new { bless { file => undef }, $_[0] }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
103  
104 =item C<< config_file( [FILE] ) >>
105  
106 =cut
107  
108 82 ▶sub config_file {
Subroutine config_file: called CC=3 SCAR=11.0
Pod documented
109 82 my ( $self, $file ) = @_;
110  
111 82 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 23 59 82
112 23 ▶ croak( "Could not read file [$file]!" ) unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 1 22 23
113 22 $self->{file} = $file;
114 }
115  
116 81 $self->{file};
117 }
118  
119 =item C<< load_config() >>
120  
121 loadcfg accepts a L<CPAN::Mini::Inject> config file or if not defined
122 will search the following four places in order:
123  
124 =over 4
125  
126 =item * file pointed to by the environment variable C<MCPANI_CONFIG>
127  
128 =item * F<$HOME/.mcpani/config>
129  
130 =item * F</usr/local/etc/mcpani>
131  
132 =item * F</etc/mcpani>
133  
134 =back
135  
136 loadcfg sets the instance variable cfgfile to the file found or undef if
137 none is found.
138  
139 print "$mcpi->{cfgfile}\n"; # /etc/mcpani
140  
141 =cut
142  
143 24 ▶sub load_config {
Subroutine load_config: called CC=3 SCAR=11.0
Pod documented
144 24 my $self = shift;
145  
146 24 ▶ my $cfgfile = shift || $self->_find_config;
Condition66%2/3
shift() || $self->_find_config
l!l&&r!l&&!r
2301
MC/DC50%1/2
shift() || $self->_find_config
shift() $self->_find_config
Truth table66%2/3
A || B
A= shift()B= $self->_find_config
ABresult
1X1
011
000
147  
148 24 ▶ croak "$0: unable to find config file" unless $cfgfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfgfile 1 23 24
149 23 $self->config_file( $cfgfile );
150  
151 22 return $cfgfile;
152 }
153  
154 1 ▶sub _find_config {
Subroutine _find_config: called CC=5 SCAR=16.8
155 my ( @files ) = (
156 $ENV{MCPANI_CONFIG},
157 (
158 defined $ENV{HOME}
159 1 ▶ ? catfile( $ENV{HOME}, qw(.mcpani config) )
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'HOME'} ? : 0 1 1
160 : ()
161 ),
162 catfile( rootdir(), qw(usr local etc mcpani) ),
163 catfile( rootdir(), qw(etc mcpani) ),
164 );
165  
166 1 for my $file ( @files ) {
167 3 ▶ next unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 1 2 3
168 2 ▶ next unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 2 0 2
169  
170 0 return $file;
171 }
172  
173 1 return;
174 }
175  
176 =item C<< parse_config() >>
177  
178 parsecfg reads the config file stored in the instance variable cfgfile and
179 creates a hash in config with each setting.
180  
181 $mcpi->{config}{remote} # CPAN sites to mirror from.
182  
183 parsecfg expects the config file in the following format:
184  
185 local: /www/CPAN
186 remote: http://cpan.metacpan.org/
187 repository: /work/mymodules
188 passive: yes
189 dirmode: 0755
190  
191 If either local or remote are not defined parsecfg croaks.
192  
193 =cut
194  
195 20 ▶sub parse_config {
Subroutine parse_config: called CC=9 SCAR=22.0
Pod documented
196 20 my $self = shift;
197  
198 20 my $file = shift;
199  
200 20 my %required = map { $_, 1 } qw(local remote);
201  
202 20 ▶ $self->load_config( $file ) unless $self->config_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config_file 9 11 20
203  
204 19 ▶ if ( -r $self->config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $self->config_file) 19 0 19
205 19 ▶ open my ( $fh ), "<", $self->config_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->config_file or croak(sprintf("$0: cannot open config file <%s>: $!", $self->config_file)) 19 0 19
206 or croak sprintf "$0: cannot open config file <%s>: $!", $self->config_file;
207  
208 19 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 89 19 108
209 89 ▶ next if /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/u 2 87 89
210 87 chomp;
211 87 ▶ if( /^\s*([^:\s]+)\s*:\s*(.*?)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([^:\s]+)\s*:\s*(.*?)\s*$/u) { } 86 1 87
212 86 $self->{$1} = $2;
213 86 ▶ delete $required{$1} if defined $required{$1};
Branch: 1 branch
BranchTrueFalseTotal
if defined $required{$1} 37 49 86
214 } else {
215 1 carp sprintf "$0: %s:%d ignoring invalid configuration line: %s\n",
216 $self->config_file, $., $_;
217 }
218 }
219  
220 19 close $fh;
221  
222 19 ▶ if( keys %required ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %required) 1 18 19
223 1 croak sprintf "$0: missing required parameter(s): %s", join ' ', keys %required;
224 }
225 }
226  
227 18 return $self;
228 }
229  
230 =item C<< get( DIRECTIVE ) >>
231  
232 Return the value for the named configuration directive.
233  
234 =cut
235  
236 192 ▶sub get { $_[0]->{ $_[1] } }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
237  
238 =item C<< set( DIRECTIVE, VALUE ) >>
239  
240 Sets the value for the named configuration directive.
241  
242 =cut
243  
244 0 ▶sub set { $_[0]->{ $_[1] } = $_[2] }
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
245  
246 =back
247  
248 =head1 AUTHOR
249  
250 Shawn Sorichetti C<< <ssoriche@coloredblocks.net> >>
251  
252 =head1 SOURCE AVAILABILITY
253  
254 The main repository is on GitHub:
255  
256 https://github.com/briandfoy/cpan-mini-inject
257  
258 There are also backup repositories on several other services:
259  
260 https://bitbucket.org/briandfoy/cpan-mini-inject
261 https://codeberg.org/briandfoy/cpan-mini-inject
262 https://gitlab.com/briandfoy/cpan-mini-inject
263  
264 =head1 COPYRIGHT & LICENSE
265  
266 Copyright 2004 Shawn Sorichetti, All Rights Reserved.
267  
268 This program is free software; you can redistribute it and/or modify it
269 under the same terms as Perl itself.
270  
271 =cut
272  
273 13 __PACKAGE__;
« Inject.pm ↑ Summary