File Coverage

lib/App/LDAP/Config.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package App::LDAP::Config;
2              
3 1     1   787 use Modern::Perl;
  1         2  
  1         7  
4 1     1   1733 use Moose;
  0            
  0            
5             use MooseX::Singleton;
6              
7             our @locations = qw(
8             $ENV{HOME}/.ldaprc
9             /etc/ldap.conf
10             /etc/ldap/ldap.conf
11             /usr/local/etc/ldap.conf
12             );
13              
14             our @has_scope = qw(
15             nss_base_passwd
16             nss_base_shadow
17             nss_base_group
18             nss_base_hosts
19             sudoers_base
20             );
21              
22             sub read {
23             my ($class, ) = @_;
24             my $self = $class->new;
25             my @locations = grep { -f $_ } @locations;
26             die "no config file found" unless scalar @locations;
27             $self->read_config_file(@locations);
28             $self;
29             }
30              
31             sub read_config_file {
32             my ($self, $file) = @_;
33             open my $config, "<", $file;
34             $self->config_from_line($_) while <$config>;
35             }
36              
37             sub config_from_line {
38             my ($self, $line) = @_;
39             return if $line =~ /(^#|^\n)/;
40              
41             my ($key, $value) = split /\s+/, $line;
42             $self->{$key} = (
43             $key ~~ @has_scope ?
44             [split /\?/, $value] :
45             $value
46             );
47             }
48              
49             1;
50              
51             =pod
52              
53             =head1 NAME
54              
55             App::LDAP::Config - loader of config files
56              
57             =head1 DESCRIPTION
58              
59             This module would be called automatically in App::LDAP::run() to load the configurations.
60              
61             =cut