File Coverage

blib/lib/CPAN/Mini/Inject/Config.pm
Criterion Covered Total %
statement 44 45 97.7
branch 23 26 88.4
condition 3 3 100.0
subroutine 10 11 90.9
pod 6 6 100.0
total 86 91 94.5


line stmt bran cond sub pod time code
1             package CPAN::Mini::Inject::Config;
2              
3 13     13   84 use strict;
  13         30  
  13         363  
4 13     13   58 use warnings;
  13         25  
  13         335  
5              
6 13     13   57 use Carp;
  13         25  
  13         763  
7 13     13   5027 use File::Spec::Functions qw(rootdir catfile);
  13         8431  
  13         7471  
8              
9             =head1 NAME
10              
11             CPAN::Mini::Inject::Config - Config for CPAN::Mini::Inject
12              
13             =head1 VERSION
14              
15             Version 0.37
16              
17             =cut
18              
19             our $VERSION = '0.37';
20              
21             =head2 C
22              
23             =cut
24              
25 16     16 1 118 sub new { bless { file => undef }, $_[0] }
26              
27             =head2 C<< config_file( [FILE] ) >>
28              
29             =cut
30              
31             sub config_file {
32 63     63 1 138 my ( $self, $file ) = @_;
33              
34 63 100       145 if ( @_ == 2 ) {
35 18 50       368 croak( "Could not read file [$file]!" ) unless -r $file;
36 18         98 $self->{file} = $file;
37             }
38              
39 63         859 $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 19     19 1 46 my $self = shift;
69              
70 19   100     74 my $cfgfile = shift || $self->_find_config;
71              
72 19 100       93 croak 'Unable to find config file' unless $cfgfile;
73 18         79 $self->config_file( $cfgfile );
74              
75 18         52 return $cfgfile;
76             }
77              
78             sub _find_config {
79             my ( @files ) = (
80             $ENV{MCPANI_CONFIG},
81             (
82             defined $ENV{HOME}
83 3 100   3   33 ? catfile( $ENV{HOME}, qw(.mcpani config) )
84             : ()
85             ),
86             catfile( rootdir(), qw(usr local etc mcpani) ),
87             catfile( rootdir(), qw(etc mcpani) ),
88             );
89              
90 3         9 for my $file ( @files ) {
91 6 100       14 next unless defined $file;
92 4 100       51 next unless -r $file;
93              
94 2         11 return $file;
95             }
96              
97 1         4 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 15     15 1 42 my $self = shift;
149              
150 15         38 my $file = shift;
151              
152 15         45 my %required = map { $_, 1 } qw(local remote);
  30         159  
153              
154 15 100       62 $self->load_config( $file ) unless $self->config_file;
155              
156 15 50       43 if ( -r $self->config_file ) {
157 15 50       154 open my ( $fh ), "<", $self->config_file
158             or croak( "Could not open config file: $!" );
159              
160 15         350 while ( <$fh> ) {
161 73 100       217 next if /^\s*#/;
162 71 100       659 $self->{$1} = $2 if /^\s*([^:\s]+)\s*:\s*(.*?)\s*$/;
163 71 100       365 delete $required{$1} if defined $required{$1};
164             }
165              
166 15         145 close $fh;
167              
168 15 100       136 croak 'Required parameter(s): '
169             . join( ' ', keys %required )
170             . ' missing.'
171             if keys %required;
172             }
173              
174 14         65 return $self;
175             }
176              
177             =head2 C<< get( DIRECTIVE ) >>
178              
179             Return the value for the named configuration directive.
180              
181             =cut
182              
183 125     125 1 1612 sub get { $_[0]->{ $_[1] } }
184              
185             =head2 C<< set( DIRECTIVE, VALUE ) >>
186              
187             Sets the value for the named configuration directive.
188              
189             =cut
190              
191 0     0 1   sub set { $_[0]->{ $_[1] } = $_[2] }
192              
193             =head1 CURRENT MAINTAINER
194              
195             Christian Walde C<< >>
196              
197             =head1 BUGS
198              
199             Please report any bugs or feature requests to
200             C, or through the web interface at
201             L. 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             1;