lib/Project/Easy/Helper/Config.pm

filter: Statementstmt 35.2% 44 / 125 Branchbran 18.0% 13 / 72 Conditioncond 26.6% 8 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 2 totaltotal 27.7% 69 / 249 CC 47 SCAR 55.0
CC
47
Cov
29%
config
254.0
config
254.0
BEGIN
1.0
CRAP 243.9
↑ Summary blib/lib/Project/Easy/Daemon.pm »
1 package Project::Easy::Helper;
2  
3 6 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Project::Easy::Config::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶sub config {
Subroutine config: called CC=24 SCAR=55.4
Pod undocumented
8
9 4 ▶ my @params = @ARGV;
Subroutine config: called CC=24 SCAR=55.4
Pod undocumented
10 4 ▶ @params = @_
Branch: 2 branches
BranchTrueFalseTotal
if scalar @_ 0 0 0
if scalar @_ 0 3 3
11 if scalar @_;
12
13 4 my ($package, $libs) = &_script_wrapper(); # Project name and "libs" path
14
15 4 my $core = $::project; # Project singleton
16 4 my $config = $core->config;
17
18 4 my $templates = file->__data__files (); # Got all "data files" at end of file
19
20 # Local config absolute path
21 0 my $config_file = $core->fixup_path_instance ($core->instance);
22 0 my @config_serializer = ();
23
24 # first, we need to detect scope of config
25 0 ▶ if ($params[0] =~ /--(project|global)/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($params[0] =~ /--(project|global)/) { } 0 0 0
elsif ($params[0] =~ /-{1,2}f(?:ile)?/) { } 0 0 0
if ($params[0] =~ /--(project|global)/) { } 0 3 3
elsif ($params[0] =~ /-{1,2}f(?:ile)?/) { } 0 3 3
26 0 shift @params;
27
28 # Global config absolute path
29 0 $config_file = $core->conf_path;
30 0 push @config_serializer, 'undef_keys_in_patch';
31 } elsif ($params[0] =~ /-{1,2}f(?:ile)?/) {
32 0 shift @params;
33 0 $config_file = Project::Easy::Config::File->new (shift @params);
34
35 # try to load config
36 0 local $@;
37 0 $config = eval {$config_file->deserialize;};
38 0 ▶ die "config file ($config_file) access error: $!"
Branch: 2 branches
BranchTrueFalseTotal
if $! 0 0 0
if $! 0 0 0
39 if $!;
40  
41 0 ▶ die "config file ($config_file) cannot be deserialized"
Branch: 2 branches
BranchTrueFalseTotal
if $@ 0 0 0
if $@ 0 0 0
42 if $@;
43 }
44
45 0 my ($key, $command, @remains) = @params;
46
47 0 ▶ unless (defined $key) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $key) 0 0 0
unless (defined $key) 0 3 3
48 0 print "requested config file name is: ", $config_file, "\n";
49 0 print $templates->{'config-usage'}, "\n";
50 0 return;
51 }
52
53 0 ▶ if ($key eq '-e') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq "-e") 0 0 0
if ($key eq "-e") 0 3 3
54 0 system ($ENV{EDITOR}, $config_file);
55 0 return;
56 }
57
58 # $key = "key1.key2.key3..."
59 # $key_eval = "{key1}->{key2}->{key3}..."
60 0 my $key_path = "{" . join ('}->{', split (/\./, $key)) . '}';
61 0 my $key_eval = "\$config->$key_path";
62  
63 0 my $struct = eval $key_eval;
64 0 my $ref_struct = ref $struct;
65  
66 0 ▶ if (!defined $command or ($command eq 'dump' and !$ref_struct)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $command or $command eq "dump" and not $ref_struct) 0 0 0
if (not defined $command or $command eq "dump" and not $ref_struct) 0 3 3
Condition16%2/12
not defined $command or $command eq "dump" and not $ref_struct
l!l&&r!l&&!r
000
$command eq "dump" and not $ref_struct
!ll&&!rl&&r
000
not defined $command or $command eq "dump" and not $ref_struct
l!l&&r!l&&!r
003
$command eq "dump" and not $ref_struct
!ll&&!rl&&r
300
MC/DC0%0/3
not defined $command or $command eq "dump" and not $ref_struct
not defined $command $command eq "dump" not $ref_struct
MC/DC0%0/3
not defined $command or $command eq "dump" and not $ref_struct
not defined $command $command eq "dump" not $ref_struct
Truth table0%0/4
not(A) or B and not(C)
A= not defined $commandB= $command eq "dump"C= not $ref_struct
ABCresult
1XX1
0111
00X0
0100
Truth table25%1/4
not(A) or B and not(C)
A= not defined $commandB= $command eq "dump"C= not $ref_struct
ABCresult
1XX1
0111
00X0
0100
67 0 print "'$key' => ";
68
69 0 ▶ if ($ref_struct eq 'HASH') {
Branch: 6 branches
BranchTrueFalseTotal
if ($ref_struct eq 'HASH') { } 0 0 0
elsif ($ref_struct eq 'ARRAY') { } 0 0 0
elsif (not $ref_struct) { } 0 0 0
if ($ref_struct eq 'HASH') { } 0 0 0
elsif ($ref_struct eq 'ARRAY') { } 0 0 0
elsif (not $ref_struct) { } 0 0 0
70 0 print "HASH with keys: ", join ', ', keys %$struct;
71
72 } elsif ($ref_struct eq 'ARRAY') {
73 0 print "ARRAY of ", scalar @$struct;
74
75 } elsif (!$ref_struct) {
76 0 ▶ print "'", (defined $struct ? $struct : 'null'), "'";
Branch: 2 branches
BranchTrueFalseTotal
defined $struct ? : 0 0 0
defined $struct ? : 0 0 0
77 }
78 0 print "\n";
79 0 return 1;
80 }
81  
82 0 my $conf_package = $package->conf_package; # Project::Easy::Config
83  
84 # Init serializer to parse config file
85 0 my $serializer_json = $conf_package->serializer ('json');
86
87 0 ▶ if ($command =~ /^(?:--)?dump$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command =~ /^(?:--)?dump$/) 0 0 0
if ($command =~ /^(?:--)?dump$/) 0 3 3
88 0 print "\"$key\" => ";
89 0 print $serializer_json->dump_struct ($struct);
90 0 print "\n";
91 0 return 1;
92 }
93
94 # set or = can: create new key (any depth), modify existing
95 # template can: create new key (any depth)
96 0 ▶ if ($command eq 'set' or $command eq '=' or $command eq 'template') {
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq "set" or $command eq "=" or $command eq "template") 0 0 0
if ($command eq "set" or $command eq "=" or $command eq "template") 3 0 3
Condition33%4/12
$command eq "set" or $command eq "=" or $command eq "template"
l!l&&r!l&&!r
000
$command eq "set" or $command eq "="
l!l&&r!l&&!r
000
$command eq "set" or $command eq "=" or $command eq "template"
l!l&&r!l&&!r
210
$command eq "set" or $command eq "="
l!l&&r!l&&!r
021
MC/DC0%0/3
$command eq "set" or $command eq "=" or $command eq "template"
$command eq "set" $command eq "=" $command eq "template"
MC/DC0%0/3
$command eq "set" or $command eq "=" or $command eq "template"
$command eq "set" $command eq "=" $command eq "template"
Truth table0%0/4
A or B or C
A= $command eq "set"B= $command eq "="C= $command eq "template"
ABCresult
1XX1
01X1
0011
0000
Truth table50%2/4
A or B or C
A= $command eq "set"B= $command eq "="C= $command eq "template"
ABCresult
1XX1
01X1
0011
0000
97
98 0 ▶ die "you must supply value for modify config"
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @remains 0 0 0
unless scalar @remains 0 3 3
99 unless scalar @remains;
100
101 # check for legitimity
102 0 ▶ die "you cannot set/template complex value such as HASH/ARRAY. remove old key first"
Branch: 2 branches
BranchTrueFalseTotal
if $ref_struct 0 0 0
if $ref_struct 0 3 3
103 if $ref_struct;
104
105 0 ▶ die "you cannot update scalar value with template. remove old key first"
Branch: 2 branches
BranchTrueFalseTotal
if $command eq "template" and defined $struct 0 0 0
if $command eq "template" and defined $struct 0 3 3
Condition33%2/6
$command eq "template" and defined $struct
!ll&&!rl&&r
000
$command eq "template" and defined $struct
!ll&&!rl&&r
210
MC/DC0%0/2
$command eq "template" and defined $struct
$command eq "template" defined $struct
MC/DC0%0/2
$command eq "template" and defined $struct
$command eq "template" defined $struct
Truth table0%0/3
A and B
A= $command eq "template"B= defined $struct
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $command eq "template"B= defined $struct
ABresult
0X0
100
111
106 if $command eq 'template' and defined $struct; # any setter
107
108 # patch creation for config files
109
110 0 my $fixup_struct = {};
111
112 0 ▶ if ($command eq 'template') {
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq 'template') { } 0 0 0
if ($command eq 'template') { } 1 2 3
113  
114 my $template = $serializer_json->parse_string (
115 0 $templates->{'template-' . $remains[0]}
116 );
117
118 0 eval "\$fixup_struct->$key_path = \$template";
119 } else {
120 0 eval "\$fixup_struct->$key_path = \$remains[0]";
121 }
122
123
124 # storing modified config
125 0 $config_file->patch ($fixup_struct, @config_serializer);
126 #warn(Dumper($config_file->contents));
127  
128 0 return 1;
129 }
130  
131 0 print $templates->{'config-usage'}, "\n";
132
133 0 return;
134 }
135  
136  
137 6 1;
138  
139 __DATA__
↑ Summary blib/lib/Project/Easy/Daemon.pm »