| 1 | package App::Netsync::Configurator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | App::Netsync::Configurator - configuration file support | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | This package makes using a configuration file simple. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =over 2 | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =item F<foobar.ini> | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | [testGroup] | ||||||||||||||||||||||||||||
| 18 | fooSetting = barValue | ||||||||||||||||||||||||||||
| 19 | barSetting = bazValue | ||||||||||||||||||||||||||||
| 20 | bazSetting = foo,bar,baz | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =item F<foobar.pl> | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use feature 'say'; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | use App::Netsync::Configurator; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | configurate 'foobar.ini'; | ||||||||||||||||||||||||||||
| 31 | say App::Netsync::Configurator::config('testGroup','fooSetting'); | ||||||||||||||||||||||||||||
| 32 | say (App::Netsync::Configurator::config('testGroup','bazSetting'))[2]; | ||||||||||||||||||||||||||||
| 33 | say {App::Netsync::Configurator::config('testGroup')}->{'barSetting'}; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =back | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $ perl foobar.pl | ||||||||||||||||||||||||||||
| 38 | > barValue | ||||||||||||||||||||||||||||
| 39 | > baz | ||||||||||||||||||||||||||||
| 40 | > bazValue | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | use feature 'say'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use autodie; #XXX Is autodie adequate? | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use Config::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | our ($SCRIPT,$VERSION); | |||||||||||||||||||||||||||
| 56 | 1 | our %config; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 1 | ($SCRIPT) = fileparse ($0,"\.[^.]*"); | |||||||||||||||||||||||||||
| 60 | 1 | ($VERSION) = version->declare('v4.0.0'); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 63 | 1 | our @ISA = ('Exporter'); | |||||||||||||||||||||||||||
| 64 | 1 | our @EXPORT_OK = ('configurate'); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 METHODS | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head2 configurate | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | reads a configuration file into the App::Netsync::Configurator namespace | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | I<Note: It will return any configurations in the file found under the E<lt>script nameE<gt> group.> | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | B<Arguments> | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | I<[ ( $file [, \%overrides [, \%defaults ] ] ) ]> | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =over 3 | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =item file | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | a configuration file (.ini) to use | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | default: F</etc/E<lt>script nameE<gt>/E<lt>script nameE<gt>.ini> | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item overrides | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | settings that should override the configuration | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item defaults | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | default settings | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =back | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub configurate { | ||||||||||||||||||||||||||
|
Subroutine
configurate: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | warn 'too many arguments' if @_ > 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | my ($file,$overrides,$defaults) = @_; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | $file //= '/etc/'.$SCRIPT.'/'.$SCRIPT.'.ini'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$file //= '/etc/' . $App::Netsync::Configurator::SCRIPT . '/' . $App::Netsync::Configurator::SCRIPT . '.ini'
MC/DC0%0/1
$file //= '/etc/' . $App::Netsync::Configurator::SCRIPT . '/' . $App::Netsync::Configurator::SCRIPT . '.ini'
$file
Truth table0%0/2
A
A= $file
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $overrides //= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$overrides //= {}
MC/DC0%0/1
$overrides //= {}
$overrides
Truth table0%0/2
A
A= $overrides
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $defaults //= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$defaults //= {}
MC/DC0%0/1
$defaults //= {}
$defaults
Truth table0%0/2
A
A= $defaults
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 0 | open (my $ini,'<',$file); | |||||||||||||||||||||||||||
| 109 | 0 | my $parser = Config::Simple->new($file); | |||||||||||||||||||||||||||
| 110 | 0 | my $syntax = $parser->guess_syntax($ini); | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | unless (defined $syntax and $syntax eq 'ini') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $syntax and $syntax eq "ini"
MC/DC0%0/2
defined $syntax and $syntax eq "ini"
defined $syntax
$syntax eq "ini"
Truth table0%0/3
A and B
A= defined $syntaxB= $syntax eq "ini"
| |||||||||||||||||||||||||||||
| 112 | 0 | say 'The configuration file "'.$file.'" is malformed.'; | |||||||||||||||||||||||||||
| 113 | 0 | return undef; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | close $ini; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | $config{$_} = $defaults->{$_} foreach keys %$defaults; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 0 | my %imports; | |||||||||||||||||||||||||||
| 122 | 0 | Config::Simple->import_from($file,\%imports); | |||||||||||||||||||||||||||
| 123 | 0 | foreach (keys %imports) { | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | $config{$_} = $imports{$_} unless ref $imports{$_} and not defined $imports{$_}[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $imports{$_} and not defined $imports{$_}[0]
MC/DC0%0/2
ref $imports{$_} and not defined $imports{$_}[0]
ref $imports{$_}
not defined $imports{$_}[0]
Truth table0%0/3
A and not(B)
A= ref $imports{$_}B= not defined $imports{$_}[0]
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | $config{$_} = $overrides->{$_} foreach keys %$overrides; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my %settings; | |||||||||||||||||||||||||||
| 131 | 0 | foreach (keys %config) { | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | $settings{$+{'setting'}} = $config{$_} if /^$SCRIPT\.(?<setting>.*)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 0 | return %settings; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 config | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | returns an individual setting or group of settings | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | I<Note: configurate needs to be run first!> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | B<Arguments> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | I<( $group [, $query ] )> | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =over 3 | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item group | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | the group of the configuration(s) to retrieve | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =item query | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | the name of the configuration to retrieve | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =back | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | warn 'too few arguments' if @_ < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | warn 'too many arguments' if @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | my ($group,$query) = @_; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | return $config{$group.'.'.$query} if defined $query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | my $responses; | |||||||||||||||||||||||||||
| 170 | 0 | foreach (keys %config) { | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | if (/^(?<grp>[^.]*)\.(?<qry>.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $responses->{$+{'qry'}} = $config{$_} if $+{'grp'} eq $group; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 0 | return $responses; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 dump | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | prints the current configuration (use sparingly) | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | I<Note: configurate needs to be run first!> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | warn 'too many arguments' if @_ > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | say $_.' = '.($config{$_} // 'undef') foreach sort keys %config; | ||||||||||||||||||||||||||
|
Condition0%0/2
$App::Netsync::Configurator::config{$_} // "undef"
MC/DC0%0/1
$App::Netsync::Configurator::config{$_} // "undef"
$App::Netsync::Configurator::config{$_}
Truth table0%0/2
A
A= $App::Netsync::Configurator::config{$_}
| |||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | David Tucker, C<< <dmtucker at ucsc.edu> >> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 BUGS | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Please report any bugs or feature requests to C<bug-netsync at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 201 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-Netsync>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 202 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | perldoc App::Netsync | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | You can also look for information at: | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =over 4 | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-Netsync> | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | L<http://annocpan.org/dist/App-Netsync> | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | L<http://cpanratings.perl.org/d/App-Netsync> | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | L<http://search.cpan.org/dist/App-Netsync/> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =back | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Copyright 2013 David Tucker. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 237 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 238 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =cut | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 1 | 1; | |||||||||||||||||||||||||||