Utilities/INI_loader.pm

filter: Statementstmt 85.3% 64 / 75 Branchbran 57.1% 16 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 80.0% 92 / 115 CC 18 SCAR 21.0
CC
18
Cov
78%
load_file
13.1
load_dir
8.3
new
2.1
CRAP 8.2
« Utilities/Options.pm ↑ Summary Utilities.pm »
1 package Net::DHCP::Config::Utilities::INI_loader;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Net::DHCP::Config::Utilities::Subnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::DHCP::Config::Utilities::INI_loader - Loads subnet configurations from a INI file.
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 3 our $VERSION = '0.0.1';
21  
22  
23 =head1 SYNOPSIS
24  
25 use Net::DHCP::Config::Utilities::INI_loader;
26 use Net::DHCP::Config::Utilities;
27
28 my $dhcp_util = Net::DHCP::Config::Utilities->new;
29
30 my $loader = Net::DHCP::Config::Utilities::INI_loader->new( $dhcp_util );
31
32 eval{
33 $loader->load_file( $file );
34 };
35 if ( $@ ){
36 # do something upon error
37 die( $@ );
38 }
39  
40 =head1 METHODS
41  
42 =head2 new
43  
44 This initiates the object.
45  
46 One argument is required and that is a Net::DHCP::Config::Utilities
47 object to operate on.
48  
49 use Net::DHCP::Config::Utilities;
50
51 my $dhcp_util = Net::DHCP::Config::Utilities->new;
52
53 my $loader = Net::DHCP::Config::Utilities::INI_loader->new( $dhcp_util );
54  
55 =cut
56  
57 sub new {
58 3 ▶ my $obj=$_[1];
Subroutine new: called CC=2 SCAR=7.2
Pod documented
59  
60 3 ▶ if ( ref( $obj ) ne 'Net::DHCP::Config::Utilities' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj ne "Net::DHCP::Config::Utilities") 0 3 3
61 0 die( 'No Net::DHCP::Config::Utilities object passed or wrong ref type' );
62 }
63  
64 3 my $self={
65 obj=>$obj,
66 };
67 3 bless $self;
68  
69 3 return $self;
70 }
71  
72 =head2 load_file
73  
74 This loads a specific file in question.
75  
76 One argument is taken and that is the path to the INI file.
77  
78 If this encounter any errors, it will die.
79  
80 eval{
81 $load->load_file( $file );
82 };
83 if ( $@ ){
84 # do something upon error
85 die( $@ );
86 }
87  
88 =cut
89  
90 sub load_file{
91 8 ▶ my $self=$_[0];
Subroutine load_file: called CC=11 SCAR=25.8
Pod documented
92 8 my $file=$_[1];
93  
94 8 ▶ if ( ! defined( $file ) ){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $file) { } 0 8 8
elsif (not -f $file) { } 0 8 8
95 0 die( 'No file specified' );
96 }elsif( ! -f $file ){
97 0 die( '"'.$file.'" is not a file or does not exist' );
98 }
99  
100 8 my $ini = Config::Tiny->read( $file );
101 8 ▶ if ( ! defined( $ini ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ini) 0 8 8
102 0 die( 'Failed to read "'.$file.'"' );
103 }
104  
105 8 foreach my $key ( keys( %{ $ini } ) ){
106 8 ▶ if ( $key ne '_' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "_") 8 0 8
107 # set base here, incase it is explicitly set later in the section
108 8 my $options={
109 base=>$key,
110 };
111  
112 # process each one as we need to real in the range variables
113 8 foreach my $option_key ( keys( %{ $ini->{$key} } ) ){
114 35 ▶ if ( $option_key =~ /^range/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($option_key =~ /^range/) { } 3 32 35
115 # each range variable needs to be treated specially
116 # as they all need loaded into a array
117 3 ▶ if ( !defined $options->{ranges} ){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $options->{'ranges'}) { } 3 0 3
118 3 $options->{ranges}=[ $ini->{$key}{$option_key} ];
119 }else{
120 0 push( @{ $options->{ranges} }, $ini->{$key}{$option_key} );
121 }
122 }else{
123 32 $options->{$option_key} = $ini->{$key}{$option_key};
124 }
125 }
126  
127 8 my $subnet;
128 8 eval{
129 8 $subnet = Net::DHCP::Config::Utilities::Subnet->new( $options );
130 };
131 8 ▶ if ( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
132 0 die('Failed to create a subnet for the section "'.$key.'"... '.$@);
133 }
134  
135 8 eval{
136 8 $self->{obj}->subnet_add( $subnet );
137 };
138 8 ▶ if ( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 7 8
139 1 die( 'Failed to add the subnet in the section "'.$key.'"... '.$@ );
140 }
141 }
142 }
143  
144 7 return 1;
145 }
146  
147 =head2 load_dir
148  
149 This loads the specified directory.
150  
151 Two arguments are taken.
152  
153 The first and required is the directory to load.
154  
155 The second and optional is the name glob to use. If none
156 is specified then '*.dhcp.ini' is used.
157  
158 Upon error, this will die.
159  
160 my $loaded;
161 eval{
162 $loaded = $load_dir->load_dir{ $dir };
163 };
164 if( $@ ){
165 die( 'Failed to load... '.$@ );
166 }else{
167 print "Loaded ".$loaded." files.";
168 }
169  
170 =cut
171  
172 sub load_dir{
173 2 ▶ my $self=$_[0];
Subroutine load_dir: called CC=7 SCAR=21.2
Pod documented
174 2 my $dir=$_[1];
175 2 my $name=$_[2];
176  
177 2 ▶ if ( ! defined( $dir ) ){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $dir) { } 0 2 2
elsif (not -d $dir) { } 0 2 2
178 0 die( 'No directory specified' );
179 }elsif( ! -d $dir ){
180 0 die( '"'.$dir.'" is not a directory or does not exist' );
181 }
182  
183 2 ▶ if ( !defined( $name ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 2 0 2
184 2 $name='*.dhcp.ini';
185 }
186  
187 2 my @files=File::Find::Rule->file()
188 ->name( $name )
189 ->in( $dir );
190  
191 2 ▶ if( ! defined( $files[0] ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $files[0]) 0 2 2
192 0 return 0;
193 }
194  
195 2 my $count=1;
196 2 my $total=$#files + 1;
197 2 foreach my $file ( @files ){
198 4 eval{
199 4 $self->load_file( $file );
200 };
201 4 ▶ if ( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
202 0 die( 'Failed on "'.$file.'", '.$count.' of '.$total.'... '.$@ );
203 }
204  
205 4 $count++;
206 }
207  
208 2 return $total;
209 }
210  
211 =head1 INI EXPECTATIONS
212  
213 Each sesction of a INI file is treated as its own subnet.
214  
215 The variable/values taken must be understood by L<Net::DHCP::Config::Utilities::Subnet>.
216  
217 If the variable base is not specified, the section name is used.
218  
219 Any variable matching /^range/ is added to the ranges array used when creating the subnet.
220  
221 [10.0.0.0]
222 mask=255.255.0.0
223 dns=10.0.0.1 , 10.0.10.1
224 desc=a /16
225 routers=10.0.0.1
226
227 [foo]
228 base=192.168.0.0
229 mask=255.255.0.0
230 dns=10.0.0.1 , 10.0.10.1
231 routers=192.168.0.1
232 range=192.168.0.100 192.168.0.200
233  
234 =head1 AUTHOR
235  
236 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
237  
238 =head1 BUGS
239  
240 Please report any bugs or feature requests to C<bug-net-dhcp-config-utilities at rt.cpan.org>, or through
241 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-DHCP-Config-Utilities>. I will be notified, and then you'll
242 automatically be notified of progress on your bug as I make changes.
243  
244  
245  
246  
247 =head1 SUPPORT
248  
249 You can find documentation for this module with the perldoc command.
250  
251 perldoc Net::DHCP::Config::Utilities
252  
253  
254 You can also look for information at:
255  
256 =over 4
257  
258 =item * RT: CPAN's request tracker (report bugs here)
259  
260 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-DHCP-Config-Utilities>
261  
262 =item * AnnoCPAN: Annotated CPAN documentation
263  
264 L<http://annocpan.org/dist/Net-DHCP-Config-Utilities>
265  
266 =item * CPAN Ratings
267  
268 L<https://cpanratings.perl.org/d/Net-DHCP-Config-Utilities>
269  
270 =item * Search CPAN
271  
272 L<https://metacpan.org/release/Net-DHCP-Config-Utilities>
273  
274 =back
275  
276  
277 =head1 ACKNOWLEDGEMENTS
278  
279  
280 =head1 LICENSE AND COPYRIGHT
281  
282 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
283  
284 This is free software, licensed under:
285  
286 The Artistic License 2.0 (GPL Compatible)
287  
288  
289 =cut
290  
291 3 1; # End of Net::DHCP::Config::Utilities
« Utilities/Options.pm ↑ Summary Utilities.pm »