| 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
| |||||||||||||||||||||
| 35 | 20 | ▶ | croak( "Could not read file [$file]!" ) unless -r $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
MC/DC100%2/2
shift() || $self->_find_config
shift()
$self->_find_config
Truth table100%3/3
A || B
A= shift()B= $self->_find_config
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 21 | ▶ | croak 'Unable to find config file' unless $cfgfile; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 92 | 4 | ▶ | next unless -r $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 17 | ▶ | if ( -r $self->config_file ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 17 | ▶ | open my ( $fh ), "<", $self->config_file | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | or croak( "Could not open config file: $!" ); | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 17 | ▶ | while ( <$fh> ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 79 | ▶ | next if /^\s*#/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 77 | ▶ | $self->{$1} = $2 if /^\s*([^:\s]+)\s*:\s*(.*?)\s*$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | 77 | ▶ | delete $required{$1} if defined $required{$1}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 17 | close $fh; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 17 | ▶ | croak 'Required parameter(s): ' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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; | |||||||||||||||||||