| 1 | package Narada::Config; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 25 | our $VERSION = 'v2.3.4'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use Export::Attrs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 25 | ▶ | use Narada; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 25 | ▶ | use Path::Tiny 0.053; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 25 | ▶ | use constant NARADA => eval { local $SIG{__DIE__}; Narada::detect() } || q{}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
eval { || ''
MC/DC100%1/1
eval {
do {
local $SIG{'__DIE__'};
Narada::detect()
}
} || ''
eval {
do {
local $SIG{'__DIE__'};
Narada::detect()
}
}
Truth table100%2/2
A
A= eval {
do {
local $SIG{'__DIE__'};
Narada::detect()
}
}
| |||||||||||||||||||||||||||||
| 14 | 25 | ▶ | use constant DB_DIR => NARADA eq 'narada-1' ? 'db' : 'mysql'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 25 | ▶ | use constant MAXPERM => 0666; ## no critic (ProhibitLeadingZeros) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 25 | my $VAR_NAME = qr{\A(?:(?![.][.]?/)[\w.-]+/)*(?![.][.]?\z)[\w.-]+\z}xms; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub get_config :Export { | ||||||||||||||||||||||||||||
| 21 | 96 | ▶ | my ($var) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_config: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 96 | ▶ | croak 'Usage: get_config(NAME)' if @_ != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 92 | ▶ | $var =~ /$VAR_NAME/xms or croak "Bad config: $var"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 66 | return undef if !NARADA; ## no critic (ProhibitExplicitReturnUndef) | |||||||||||||||||||||||||||
| 25 | 63 | return path("config/$var")->slurp_utf8; | |||||||||||||||||||||||||||
| 26 | 25 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub get_config_line :Export { | ||||||||||||||||||||||||||||
| 29 | 30 | ▶ | my ($val) = get_config(@_); | ||||||||||||||||||||||||||
|
Subroutine
get_config_line: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 28 | ▶ | return $val if !defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 26 | $val =~ s/\n\s*\z//xms; | |||||||||||||||||||||||||||
| 32 | 26 | ▶ | croak 'Config contain more than one line' if $val =~ /\n/xms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 24 | return $val; | |||||||||||||||||||||||||||
| 34 | 25 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub get_db_config :Export { | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | my %db; | ||||||||||||||||||||||||||
|
Subroutine
get_db_config: called CC=7 SCAR=33.5
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 1 | $db{db} = eval { get_config_line(DB_DIR.'/db') }; | |||||||||||||||||||||||||||
| 39 | 1 | ▶ | if (!defined $db{db} || !length $db{db}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $db{"db"} and length $db{"db"}
MC/DC0%0/2
defined $db{"db"} and length $db{"db"}
defined $db{"db"}
length $db{"db"}
Truth table33%1/3
A and B
A= defined $db{"db"}B= length $db{"db"}
| |||||||||||||||||||||||||||||
| 40 | 1 | return; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | $db{login}= get_config_line(DB_DIR.'/login'); | |||||||||||||||||||||||||||
| 43 | 0 | $db{pass} = get_config_line(DB_DIR.'/pass'); | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | $db{host} = eval { get_config_line(DB_DIR.'/host') } || q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
eval { || ''
MC/DC0%0/1
eval {
do {
get_config_line('mysql/host')
}
} || ''
eval {
do {
get_config_line('mysql/host')
}
}
Truth table0%0/2
A
A= eval {
do {
get_config_line('mysql/host')
}
}
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $db{port} = eval { get_config_line(DB_DIR.'/port') } || q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
eval { || ''
MC/DC0%0/1
eval {
do {
get_config_line('mysql/port')
}
} || ''
eval {
do {
get_config_line('mysql/port')
}
}
Truth table0%0/2
A
A= eval {
do {
get_config_line('mysql/port')
}
}
| |||||||||||||||||||||||||||||
| 46 | 0 | $db{dsn_nodb} = 'dbi:mysql:'; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | $db{dsn_nodb} .= ';host='.$db{host} if $db{host}; ## no critic (ProhibitPostfixControls) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $db{dsn_nodb} .= ';port='.$db{port} if $db{port}; ## no critic (ProhibitPostfixControls) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $db{dsn} = $db{dsn_nodb}.';database='.$db{db}; | |||||||||||||||||||||||||||
| 50 | 0 | return \%db; | |||||||||||||||||||||||||||
| 51 | 25 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub set_config :Export { | ||||||||||||||||||||||||||||
| 54 | 43 | ▶ | my ($var, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_config: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 43 | ▶ | croak 'Usage: set_config(NAME, VALUE)' if @_ != 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 37 | ▶ | $var =~ /$VAR_NAME/xms or croak "Bad config: $var"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 15 | croak 'Narada::Config was loaded not in narada directory' if !NARADA; | |||||||||||||||||||||||||||
| 58 | 14 | my $cfg = path("config/$var"); | |||||||||||||||||||||||||||
| 59 | 14 | $cfg->parent->mkpath; | |||||||||||||||||||||||||||
| 60 | 12 | $cfg->spew_utf8($val); | |||||||||||||||||||||||||||
| 61 | 10 | $cfg->chmod(MAXPERM & ~umask); | |||||||||||||||||||||||||||
| 62 | 10 | return; | |||||||||||||||||||||||||||
| 63 | 25 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 25 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =encoding utf8 | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 NAME | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Narada::Config - manage project configuration | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 VERSION | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | This document describes Narada::Config version v2.3.4 | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | use Narada::Config; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | my $version = get_config_line("version"); | ||||||||||||||||||||||||||||
| 86 | my $exclude = get_config("backup/exclude"); | ||||||||||||||||||||||||||||
| 87 | set_config("log/output", "/dev/stderr"); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | my $db = get_db_config(); | ||||||||||||||||||||||||||||
| 90 | my $dbh = DBI->connect($db->{dsn}, $db->{login}, $db->{pass}); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Project's configuration kept in files under directory 'config/' in | ||||||||||||||||||||||||||||
| 96 | project root directory. Usually single file contain value for single | ||||||||||||||||||||||||||||
| 97 | variable (variable name is file name itself). | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | While it's possible to manage project configuration with reading/writing | ||||||||||||||||||||||||||||
| 100 | to these files, this module provide easier way to do this. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 INTERFACE | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =over | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =item B<get_config>( VARIABLE_NAME ) | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | VARIABLE_NAME must contain only [/A-Za-z0-9._-]. | ||||||||||||||||||||||||||||
| 110 | It must not contain "./" or "../". | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Return: contents of file 'config/VARIABLE_NAME' as scalar. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =item B<get_config_line>( VARIABLE_NAME ) | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Suitable for reading files which contain single string (with/without \n at end). | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | VARIABLE_NAME must contain only [/A-Za-z0-9._-]. | ||||||||||||||||||||||||||||
| 119 | It must not contain "./" or "../". | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Return: contents of file 'config/VARIABLE_NAME' as scalar without last \n | ||||||||||||||||||||||||||||
| 122 | (if any). Raise exception if file contain more than one line. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item B<get_db_config>() | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Helper for reading database configuration from C<config/db/*>. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Return: nothing if database not configured, or hashref with keys: | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | {db} contents of config/db/db | ||||||||||||||||||||||||||||
| 131 | {login} contents of config/db/login | ||||||||||||||||||||||||||||
| 132 | {pass} contents of config/db/pass | ||||||||||||||||||||||||||||
| 133 | {host} contents of config/db/host | ||||||||||||||||||||||||||||
| 134 | {port} contents of config/db/port | ||||||||||||||||||||||||||||
| 135 | {dsn_nodb} 'dbi:mysql:;host=$host;port=$port' | ||||||||||||||||||||||||||||
| 136 | {dsn} 'dbi:mysql:;host=$host;port=$port;database=$db' | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item B<set_config>( VARIABLE_NAME, VARIABLE_VALUE ) | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | See limitation for VARIABLE_VALUE above. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Atomically write VARIABLE_VALUE (scalar) to file 'config/VARIABLE_NAME'. | ||||||||||||||||||||||||||||
| 143 | If 'config/VARIABLE_NAME' doesn't exist it will be created (including | ||||||||||||||||||||||||||||
| 144 | parent directories if needed). | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Return: nothing. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =back | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 DIAGNOSTICS | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =over | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =item C<< bad config: %s >> | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Thrown by set_config() and get_config() on wrong VARIABLE_NAME. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =back | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 CONFIGURATION AND ENVIRONMENT | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Narada::Config requires configuration files and directories provided by | ||||||||||||||||||||||||||||
| 165 | Narada framework. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 Bugs / Feature Requests | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Please report any bugs or feature requests through the issue tracker | ||||||||||||||||||||||||||||
| 173 | at L<https://github.com/powerman/Narada/issues>. | ||||||||||||||||||||||||||||
| 174 | You will be notified automatically of any progress on your issue. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 Source Code | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | This is open source software. The code repository is available for | ||||||||||||||||||||||||||||
| 179 | public review and contribution under the terms of the license. | ||||||||||||||||||||||||||||
| 180 | Feel free to fork the repository and submit pull requests. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | L<https://github.com/powerman/Narada> | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | git clone https://github.com/powerman/Narada.git | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 Resources | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =over | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =item * MetaCPAN Search | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | L<https://metacpan.org/search?q=Narada> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | L<http://cpanratings.perl.org/dist/Narada> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | L<http://annocpan.org/dist/Narada> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =item * CPAN Testers Matrix | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | L<http://matrix.cpantesters.org/?dist=Narada> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =item * CPANTS: A CPAN Testing Service (Kwalitee) | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | L<http://cpants.cpanauthors.org/dist/Narada> | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =back | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | Alex Efros E<lt>powerman@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | This software is Copyright (c) 2008-2016 by Alex Efros E<lt>powerman@cpan.orgE<gt>. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | The MIT (X11) License | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =cut | ||||||||||||||||||||||||||||