File Coverage

blib/lib/OTRS/OPM/Installer/Utils/Config.pm
Criterion Covered Total %
statement 42 43 97.6
branch 13 16 81.2
condition 5 6 83.3
subroutine 8 8 100.0
pod n/a
total 68 73 93.1


line stmt bran cond sub pod time code
1             package OTRS::OPM::Installer::Utils::Config;
2             $OTRS::OPM::Installer::Utils::Config::VERSION = '0.05';
3             # ABSTRACT: Read config file for OTRS::OPM::Installer
4              
5 16     16   526645 use strict;
  16         80  
  16         495  
6 16     16   161 use warnings;
  16         40  
  16         480  
7              
8 16     16   90 use Carp qw(croak);
  16         33  
  16         816  
9 16     16   127 use File::Basename;
  16         54  
  16         997  
10 16     16   3051 use File::HomeDir;
  16         29517  
  16         843  
11 16     16   109 use File::Spec;
  16         34  
  16         354  
12 16     16   2873 use Moo;
  16         60557  
  16         108  
13              
14             has rc_config => ( is => 'ro', lazy => 1, default => \&_rc_config );
15             has conf => ( is => 'ro' );
16              
17             sub _rc_config {
18 5     5   10080 my ($self) = @_;
19              
20 5         68 my $dot_file = File::Spec->catfile(
21             File::HomeDir->my_home,
22             '.opminstaller.rc'
23             );
24              
25 5 100 100     463 if ( $self->conf && -f $self->conf ) {
    100          
26 2         12 $dot_file = $self->conf;
27             }
28             elsif ( $self->conf ) {
29 1         185 croak 'Config file ' . $self->conf . ' does not exist';
30             }
31              
32 4         11 my %config;
33 4 100 66     240 if ( -f $dot_file && open my $fh, '<', $dot_file ) {
34 3         87 while ( my $line = <$fh> ) {
35 9         23 chomp $line;
36 9 50       30 next if $line =~ m{\A\s*\#};
37 9 50       35 next if $line =~ m{\A\s*\z};
38              
39 9         51 my ($key, $value) = split /\s*=\s*/, $line;
40 9         33 $key = lc $key;
41              
42 9 100       43 if ( $key eq 'repository' ) {
    50          
43 6         23 push @{ $config{$key} }, $value;
  6         34  
44             }
45             elsif ( $key eq 'otrs_path' ) {
46 3 100       31 if ( !File::Spec->file_name_is_absolute( $value ) ) {
47 1         28 my $dir = dirname $dot_file;
48 1         30 $value = File::Spec->rel2abs(
49             File::Spec->catdir( $dir, $value ),
50             );
51             }
52              
53 3         44 $config{$key} = $value;
54             }
55             else {
56 0         0 $config{$key} = $value;
57             }
58             }
59             }
60              
61 4         65 return \%config;
62             }
63              
64             1;
65              
66             __END__