File: Config.pm

package info (click to toggle)
libcpan-mini-inject-perl 1.012-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: perl: 732; makefile: 4
file content (273 lines) | stat: -rw-r--r-- 5,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use v5.16;
package CPAN::Mini::Inject::Config;

use strict;
use warnings;

our $VERSION = '0.38';

use Carp;
use File::Spec::Functions qw(rootdir catfile);

=head1 NAME

CPAN::Mini::Inject::Config - Config for CPAN::Mini::Inject

=head1 SYNOPSIS

	my $config = CPAN::Mini::Inject::Config->new;

=head1 DESCRIPTION

=head2 Configuration

This is the default class dealing with the default L<CPAN::Mini::Inject>
config. The simplest config is a key-value file:

	local: t/local/CPAN
	remote : http://localhost:11027
	repository: t/local/MYCPAN
	dirmode: 0775
	passive: yes

This module digests that and returns it as a hash reference. Any module
that wants to use a different sort of config structure needs to return
the same hash:

	{
	local      => 't/local/CPAN',
	remote     => 'http://localhost:11027',
	repository => 't/local/MYCPAN',
	dirmode    => '0775',
	passive    => 'yes',
	}

=over 4

=item * dirmode

Set the permissions of created directories to the specified mode. The default
value is based on umask if supported.

=item * force

passthrough to L<CPAN::Mini>.

=item * local

(required) location to store local CPAN::Mini mirror

=item * log_level

passthrough to L<CPAN::Mini>

=item * module_filters

passthrough to L<CPAN::Mini>

=item * passive

Enable passive FTP.

=item * remote

(required) CPAN site(s) to mirror from. Multiple sites can be listed space separated.

=item * repository

Location to store modules to add to the local CPAN::Mini mirror.

=item * skip_cleanup

passthrough to L<CPAN::Mini>

=item * skip_perl

passthrough to L<CPAN::Mini>

=item * trace

passthrough to L<CPAN::Mini>

=back

=head2 Methods

=over 4

=item C<new>

=cut

sub new { bless { file => undef }, $_[0] }

=item C<< config_file( [FILE] ) >>

=cut

sub config_file {
  my ( $self, $file ) = @_;

  if ( @_ == 2 ) {
    croak( "Could not read file [$file]!" ) unless -r $file;
    $self->{file} = $file;
  }

  $self->{file};
}

=item C<< load_config() >>

loadcfg accepts a L<CPAN::Mini::Inject> config file or if not defined
will search the following four places in order:

=over 4

=item * file pointed to by the environment variable C<MCPANI_CONFIG>

=item * F<$HOME/.mcpani/config>

=item * F</usr/local/etc/mcpani>

=item * F</etc/mcpani>

=back

loadcfg sets the instance variable cfgfile to the file found or undef if
none is found.

 print "$mcpi->{cfgfile}\n"; # /etc/mcpani

=cut

sub load_config {
  my $self = shift;

  my $cfgfile = shift || $self->_find_config;

  croak "$0: unable to find config file" unless $cfgfile;
  $self->config_file( $cfgfile );

  return $cfgfile;
}

sub _find_config {
  my ( @files ) = (
    $ENV{MCPANI_CONFIG},
    (
      defined $ENV{HOME}
      ? catfile( $ENV{HOME}, qw(.mcpani config) )
      : ()
    ),
    catfile( rootdir(), qw(usr local etc mcpani) ),
    catfile( rootdir(), qw(etc mcpani) ),
  );

  for my $file ( @files ) {
    next unless defined $file;
    next unless -r $file;

    return $file;
  }

  return;
}

=item C<< parse_config() >>

parsecfg reads the config file stored in the instance variable cfgfile and
creates a hash in config with each setting.

  $mcpi->{config}{remote} # CPAN sites to mirror from.

parsecfg expects the config file in the following format:

 local: /www/CPAN
 remote: http://cpan.metacpan.org/
 repository: /work/mymodules
 passive: yes
 dirmode: 0755

If either local or remote are not defined parsecfg croaks.

=cut

sub parse_config {
  my $self = shift;

  my $file = shift;

  my %required = map { $_, 1 } qw(local remote);

  $self->load_config( $file ) unless $self->config_file;

  if ( -r $self->config_file ) {
    open my ( $fh ), "<", $self->config_file
     or croak sprintf "$0: cannot open config file <%s>: $!", $self->config_file;

    while ( <$fh> ) {
      next if /^\s*#/;
      chomp;
      if( /^\s*([^:\s]+)\s*:\s*(.*?)\s*$/ ) {
      	$self->{$1} = $2;
      	delete $required{$1} if defined $required{$1};
      } else {
      	carp sprintf "$0: %s:%d ignoring invalid configuration line: %s\n",
      		$self->config_file, $., $_;
      }
    }

    close $fh;

    if( keys %required ) {
      croak sprintf "$0: missing required parameter(s): %s", join ' ', keys %required;
    }
  }

  return $self;
}

=item C<< get( DIRECTIVE ) >>

Return the value for the named configuration directive.

=cut

sub get { $_[0]->{ $_[1] } }

=item C<< set( DIRECTIVE, VALUE ) >>

Sets the value for the named configuration directive.

=cut

sub set { $_[0]->{ $_[1] } = $_[2] }

=back

=head1 AUTHOR

Shawn Sorichetti C<< <ssoriche@coloredblocks.net> >>

=head1 SOURCE AVAILABILITY

The main repository is on GitHub:

	https://github.com/briandfoy/cpan-mini-inject

There are also backup repositories on several other services:

	https://bitbucket.org/briandfoy/cpan-mini-inject
	https://codeberg.org/briandfoy/cpan-mini-inject
	https://gitlab.com/briandfoy/cpan-mini-inject

=head1 COPYRIGHT & LICENSE

Copyright 2004 Shawn Sorichetti, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

__PACKAGE__;