File Coverage

blib/lib/INI/ReadPath.pm
Criterion Covered Total %
statement 29 35 82.8
branch 4 12 33.3
condition 1 3 33.3
subroutine 8 8 100.0
pod 0 2 0.0
total 42 60 70.0


line stmt bran cond sub pod time code
1             package INI::ReadPath;
2             $INI::ReadPath::VERSION = '1';
3 2     2   28160 use strict;
  2         4  
  2         77  
4 2     2   12 use warnings;
  2         4  
  2         61  
5 2     2   1879 use Config::INI::Reader;
  2         133909  
  2         75  
6 2     2   1818 use Mouse;
  2         65689  
  2         14  
7 2     2   23323 use Template;
  2         64538  
  2         688  
8              
9             =head1 NAME
10              
11             INI::ReadPath - In Jenkins grep dist.ini config file and assign something
12             inside of the config to an environment variable.
13              
14             =head1 USAGE
15              
16             Let's say your ini file is dist.ini
17              
18             And we want to package name
19              
20             PAKCAGE_NAME=$(read_ini.pl --file dist.ini --path ini.name )
21              
22             =cut
23              
24             has file => (
25             is => "ro",
26             isa => "Str",
27             );
28              
29             has string => (
30             is => "ro",
31             isa => "Str",
32             );
33              
34             has config => (
35             is => "ro",
36             isa => "HashRef",
37             lazy_build => 1,
38             );
39              
40             sub _build_config {
41 2     2   4 my $self = shift;
42 2 50 33     8 my $ini_str = $self->from_file || $self->string
43             or die "No data";
44 2         23 my $config = Config::INI::Reader->read_string($ini_str);
45 1         256 $config->{ini} = delete $config->{_};
46 1         11 return $config;
47             }
48              
49             sub from_file {
50 2     2 0 4 my $self = shift;
51 2 50       32 my $file = $self->file
52             or return;
53 0 0       0 return if !-f $file;
54 0 0       0 open my $FH, "<", $file
55             or return;
56 0         0 local $/;
57 0         0 my $string = <$FH>;
58 0         0 close $FH;
59 0         0 return $string;
60             }
61              
62             sub get {
63 2     2 0 3428 my $self = shift;
64 2         20 my $config = $self->config;
65 1 50       6 my $path = shift
66             or return $config;
67 1         21 my $tt = Template->new;
68 1         46864 my $value = q{};
69 1 50       10 $tt->process( \"[%$path%]", $config, \$value )
70             or die $tt->error;
71 1         50034 return $value;
72             }
73              
74             1;