MToken/Config.pm

filter: Statementstmt 33.0% 38 / 115 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 50.0% 12 / 24 Podpod 100% 8 / 8 totaltotal 25.0% 58 / 232 CC 29 SCAR 42.3
CC
29
Cov
21%
new
210.0
save
42.0
_loadconfig
42.0
CRAP 68.7
« MToken/Client.pm ↑ Summary MToken/Server.pm »
1 package MToken::Config; # $Id: Config.pm 103 2021-10-10 11:04:34Z minus $
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 MToken::Config - MToken local configuration
10  
11 =head1 VERSION
12  
13 Version 1.03
14  
15 =head1 SYNOPSIS
16  
17 use MToken::Config;
18  
19 my $config = MToken::Config->new(
20 file => '/my/device/mtoken.conf',
21 name => 'foo', # Optional. See device config file first
22 );
23  
24 my $foo = $config->get('foo');
25  
26 my $foo = $config->set('foo' => 'bar');
27  
28 my $status = $config->save(); # Local file only
29  
30 =head1 DESCRIPTION
31  
32 The module works with the local configuration data
33  
34 =head1 METHODS
35  
36 =over 8
37  
38 =item B<new>
39  
40 my $config = WWW::MLite::Config->new(
41 file => '/my/device/mtoken.conf',
42 name => 'foo', # Optional. See device config file first
43 );
44  
45 Returns configuration object
46  
47 =item B<get>
48  
49 my $value = $config->get( 'key' );
50  
51 Returns value by keyname
52  
53 =item B<getall, conf, config>
54  
55 my %config = $config->getall;
56  
57 Returns all configuration pairs - key and value
58  
59 =item B<is_loaded>
60  
61 print $self->is_loaded ? 'loaded' : 'not loaded';
62  
63 Returns status of local config
64  
65 =item B<set>
66  
67 $config->set( 'key', $value );
68  
69 Set new value for key. Returns status of the operation
70  
71 =item B<save>
72  
73 $config->save;
74  
75 Save current configuration to local_file and returns status of the operation
76  
77 =back
78  
79 =head1 HISTORY
80  
81 See C<Changes> file
82  
83 =head1 AUTHOR
84  
85 Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
86  
87 =head1 COPYRIGHT
88  
89 Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved
90  
91 =head1 LICENSE
92  
93 This program is free software; you can redistribute it and/or
94 modify it under the same terms as Perl itself.
95  
96 See C<LICENSE> file and L<https://dev.perl.org/licenses/>
97  
98 =cut
99  
100 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 ▶use Config::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 1 ▶use CTK::Util qw/preparedir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 1 ▶use MToken::Const qw/ :GENERAL :MATH /;
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 $VERSION = '1.03';
111  
112 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
113 1 ALLOWED_KEYS => [qw/
114 token
115 server_url
116 gpgbin opensslbin
117 fingerprint
118 /],
119 1 };
120  
121 0 ▶sub new {
Subroutine new: not called CC=14 SCAR=53.5
Pod documented
122 0 my $class = shift;
123 0 my %args = (@_);
124 0 my $is_loaded = 0;
125  
126 my $config_file = $args{file} || $args{config_file} || $args{device_file}
127 0 ▶ || File::Spec->catfile(cwd(), DIR_PRIVATE, DEVICE_CONF_FILE);
Condition0%0/9
$args{'file'} || $args{'config_file'} || $args{'device_file'} || 'File::Spec'->catfile(cwd(), '.mtoken', 'mtoken.conf')
l!l&&r!l&&!r
000
$args{"file"} or $args{"config_file"} or $args{"device_file"}
l!l&&r!l&&!r
000
$args{"file"} or $args{"config_file"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$args{'file'} || $args{'config_file'} || $args{'device_file'} || 'File::Spec'->catfile(cwd(), '.mtoken', 'mtoken.conf')
$args{"file"} $args{"config_file"} $args{"device_file"} 'File::Spec'->catfile(cwd(), '.mtoken', 'mtoken.conf')
Truth table0%0/5
A or B or C || D
A= $args{"file"}B= $args{"config_file"}C= $args{"device_file"}D= 'File::Spec'->catfile(cwd(), '.mtoken', 'mtoken.conf')
ABCDresult
1XXX1
01XX1
001X1
00011
00000
128  
129 # Load device config
130 0 my %dev = _loadconfig($config_file);
131 0 ▶ my $name = $dev{token} || $dev{token_name} || $dev{name};
Condition0%0/6
$dev{'token'} || $dev{'token_name'} || $dev{'name'}
l!l&&r!l&&!r
000
$dev{"token"} or $dev{"token_name"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$dev{'token'} || $dev{'token_name'} || $dev{'name'}
$dev{"token"} $dev{"token_name"} $dev{'name'}
Truth table0%0/4
A or B || C
A= $dev{"token"}B= $dev{"token_name"}C= $dev{'name'}
ABCresult
1XX1
01X1
0011
0000
132 0 ▶ $is_loaded = 1 if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
133  
134  
135 # Get path of local config
136 0 ▶ $name ||= $args{name} || "noname";
Condition0%0/5
$name ||= $args{"name"} || "noname"
l!l&&r!l&&!r
000
$args{'name'} || 'noname'
l!l
00
MC/DC0%0/2
$name ||= $args{"name"} || "noname"
$name $args{'name'}
Truth table0%0/3
A ||= B
A= $nameB= $args{'name'}
ABresult
1X1
011
000
137 0 $name =~ s/\s+//g;
138 0 $name =~ s/[^a-z0-9]//g;
139 0 ▶ $name ||= "noname";
Condition0%0/2
$name ||= "noname"
l!l
00
MC/DC0%0/1
$name ||= "noname"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
140 0 my $local_dir = File::Spec->catdir(File::HomeDir->my_data(), PROJECTNAMEL);
141 0 my $local_file = File::Spec->catfile($local_dir, sprintf("%s.conf", $name));
142 0 my %lkl = ();
143 0 ▶ if (-f $local_file) { # Ok! File exists, try load local config
Branch: 1 branch
BranchTrueFalseTotal
if (-f $local_file) 0 0 0
144 0 %lkl = _loadconfig($local_file);
145 0 ▶ while (my ($k,$v) = each %lkl) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %lkl) 0 0 0
146 0 ▶ $dev{$k} //= $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
Condition0%0/3
$dev{$k} //= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$dev{$k} //= $v
$dev{$k} $v
Truth table0%0/3
A //= B
A= $dev{$k}B= $v
ABresult
1X1
011
000
147 }
148 }
149  
150 # Set data
151 0 my %cfg = (
152 name => $name,
153 is_loaded => $is_loaded,
154 device_config_file => $config_file,
155 local_config_dir => $local_dir,
156 local_config_file => $local_file,
157 _config => {%dev},
158 );
159  
160 0 my $self = bless { %cfg }, $class;
161 0 return $self;
162 }
163 0 ▶sub save {
Subroutine save: not called CC=6 SCAR=37.4
Pod documented
164 0 my $self = shift;
165 0 ▶ if (!$self->{name} || $self->{name} eq "noname") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"name"} or $self->{"name"} eq "noname") 0 0 0
Condition0%0/3
not $self->{"name"} or $self->{"name"} eq "noname"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"name"} or $self->{"name"} eq "noname"
not $self->{"name"} $self->{"name"} eq "noname"
Truth table0%0/3
not(A) or B
A= not $self->{"name"}B= $self->{"name"} eq "noname"
ABresult
1X1
011
000
166 0 carp("Can't use nonamed devices");
167 0 return FALSE;
168 }
169  
170 0 my %svh = ();
171 0 foreach my $k (@{(ALLOWED_KEYS)}) {
172 0 my $v = $self->get($k);
173 0 ▶ $svh{$k} = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
174 }
175 0 ▶ preparedir($self->{local_config_dir}, 0755) or do {
Branch: 1 branch
BranchTrueFalseTotal
preparedir($self->{"local_config_dir"}, 493) or do { carp(sprintf(q[Can't prepare directory %s], $self->{"local_config_dir"})); return 0 } 0 0 0
176 0 carp(sprintf("Can't prepare directory %s", $self->{local_config_dir}));
177 0 return FALSE;
178 };
179  
180 0 return _saveconfig($self->{local_config_file}, {%svh});
181 }
182 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
183 0 my $self = shift;
184 0 my $key = shift;
185 0 ▶ return undef unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
186 0 return $self->{_config}{$key};
187 }
188 0 ▶sub getall {
Subroutine getall: not called CC=1 SCAR=6.9
Pod documented
189 0 my $self = shift;
190 0 my $lh = $self->{_config};
191 0 return (%$lh);
192 }
193 0 ▶sub conf { goto &getall };
Subroutine conf: not called CC=1 SCAR=6.9
Pod documented
194 0 ▶sub config { goto &getall };
Subroutine config: not called CC=1 SCAR=6.9
Pod documented
195 0 ▶sub set {
Subroutine set: not called CC=2 SCAR=17.9
Pod documented
196 0 my $self = shift;
197 0 my $key = shift;
198 0 my $val = shift;
199 0 ▶ unless ($key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 0 0
200 0 carp("Key not specified");
201 0 return FALSE;
202 }
203 0 $self->{_config}{$key} = $val;
204 0 return TRUE;
205 }
206 0 ▶sub is_loaded {
Subroutine is_loaded: not called CC=1 SCAR=6.9
Pod documented
207 0 my $self = shift;
208 0 $self->{is_loaded};
209 }
210  
211 0 ▶sub _loadconfig {
Subroutine _loadconfig: not called CC=6 SCAR=37.4
212 0 my $file = shift;
213 0 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 0 0
214 0 carp("Filename not specified!");
215 0 return ();
216 }
217 0 my %config = ();
218  
219 # Load
220 0 ▶ if ($file && -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and -f $file) 0 0 0
Condition0%0/3
$file and -f $file
!ll&&!rl&&r
000
MC/DC0%0/2
$file and -f $file
$file -f $file
Truth table0%0/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
221 0 my $gconf;
222 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
223 0 $gconf = Config::General->new(
224 -ConfigFile => $file,
225 -ApacheCompatible => TRUE,
226 -LowerCaseNames => TRUE,
227 -AutoTrue => TRUE,
228 );
229 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 0 carp($_);
231 0 };
232 0 ▶ if ($gconf && $gconf->can('getall')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gconf and $gconf->can("getall")) 0 0 0
Condition0%0/3
$gconf and $gconf->can("getall")
!ll&&!rl&&r
000
MC/DC0%0/2
$gconf and $gconf->can("getall")
$gconf $gconf->can("getall")
Truth table0%0/3
A and B
A= $gconfB= $gconf->can("getall")
ABresult
0X0
100
111
233 0 %config = ($gconf->getall);
234 }
235 }
236  
237 0 return %config;
238 }
239 0 ▶sub _saveconfig {
Subroutine _saveconfig: not called CC=4 SCAR=30.0
240 0 my $file = shift;
241 0 my $config = shift;
242 0 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 0 0
243 0 carp("Filename not specified!");
244 0 return 0;
245 }
246 0 ▶ unless ($config && ref($config) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config and ref $config eq "HASH") 0 0 0
Condition0%0/3
$config and ref $config eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$config and ref $config eq "HASH"
$config ref $config eq "HASH"
Truth table0%0/3
A and B
A= $configB= ref $config eq "HASH"
ABresult
0X0
100
111
247 0 carp("No configuration data for saving");
248 0 return 0;
249 }
250  
251 0 my $conf = Config::General->new(
252 -ConfigHash => $config,
253 -ApacheCompatible => TRUE,
254 -LowerCaseNames => TRUE,
255 -AutoTrue => TRUE,
256 );
257 0 $conf->save_file($file);
258 0 return 1;
259 }
260  
261 1 1;
262  
263 __END__
« MToken/Client.pm ↑ Summary MToken/Server.pm »