blib/lib/Clustericious/Config/Helpers.pm

filter: Statementstmt 69.1% 65 / 94 Branchbran 44.4% 8 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 73.0% 19 / 26 Podpod 100% 12 / 12 totaltotal 69.3% 104 / 150 CC 10 SCAR 13.2
CC
10
Cov
65%
address
20.0
_do_merges
3.2
dir
2.0
CRAP 3.8
« blib/lib/Test/Clustericious/Config.pm ↑ Summary blib/lib/Clustericious/Command/which.pm »
1 package Clustericious::Config::Helpers;
2  
3 72 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 72 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 72 ▶use Hash::Merge qw/merge/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 72 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 72 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 72 ▶use JSON::MaybeXS qw( encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 72 ▶use Clustericious::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Helpers for clustericious config files.
14 72 our $VERSION = '1.29'; # VERSION
15  
16  
17 72 our @mergeStack;
18 72 our @EXPORT = qw( extends_config get_password home file dir hostname hostname_full json yaml address public_address interface );
19  
20  
21 sub extends_config
22 11 ▶{
Subroutine extends_config: called CC=1 SCAR=0.0
Pod documented
23 11 my($name, @args) = @_;
24 11 push @mergeStack, Clustericious::Config->new($name, \@args);
25 11 return '';
26 }
27  
28 #
29 #
30 # do_merges:
31 #
32 # Called after reading all config files, to process extends_config
33 # directives.
34 #
35 150 ▶sub _do_merges {
Subroutine _do_merges: called CC=3 SCAR=11.5
36 150 my($class, $data) = @_;
37  
38 150 ▶ return $data unless @mergeStack;
Branch: 1 branch
BranchTrueFalseTotal
unless @Clustericious::Config::Helpers::mergeStack 139 11 150
39  
40 # Nested extends_config's form a tree which we traverse depth first.
41 11 Hash::Merge::set_behavior( 'RIGHT_PRECEDENT' );
42 11 my %so_far = %{ shift @mergeStack };
43 11 ▶ while (my $c = shift @mergeStack)
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = shift @Clustericious::Config::Helpers::mergeStack) 0 11 11
44 {
45 0 my %h = %$c;
46 0 %so_far = %{ merge( \%so_far, \%h ) };
47 }
48 11 %$data = %{ merge( \%so_far, $data ) };
49 }
50  
51  
52 sub get_password
53 3 ▶{
Subroutine get_password: called CC=1 SCAR=0.0
Pod documented
54 3 return Clustericious::Config::Callback::Password->new->to_yaml;
55 }
56  
57  
58 sub home (;$)
59 12 ▶{
Subroutine home: called CC=2 SCAR=6.9
Pod documented
60 12 require File::Glob;
61 12 ▶ $_[0] ? File::Glob::bsd_glob("~$_[0]") : File::Glob::bsd_glob('~');
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 11 12
62 }
63  
64  
65 sub file
66 1 ▶{
Subroutine file: called CC=2 SCAR=7.0
Pod documented
67 1 eval { require Path::Class::File };
68 1 ▶ croak "file helper requires Path::Class" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
69 1 Path::Class::File->new(@_);
70 }
71  
72  
73 sub dir
74 9 ▶{
Subroutine dir: called CC=2 SCAR=7.1
Pod documented
75 9 require Path::Class::Dir;
76 9 ▶ croak "dir helper requires Path::Class" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
77 9 Path::Class::Dir->new(@_);
78 }
79  
80  
81 sub hostname
82 1 ▶{
Subroutine hostname: called CC=2 SCAR=7.0
Pod documented
83 1 state $hostname;
84
85 1 ▶ unless(defined $hostname)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hostname) 1 0 1
86 {
87 1 require Sys::Hostname;
88 1 $hostname = Sys::Hostname::hostname();
89 1 $hostname =~ s/\..*$//;
90 }
91
92 1 $hostname;
93 }
94  
95  
96 sub hostname_full
97 1 ▶{
Subroutine hostname_full: called CC=1 SCAR=0.0
Pod documented
98 1 require Sys::Hostname;
99 1 Sys::Hostname::hostname();
100 }
101  
102  
103 sub json ($)
104 1 ▶{
Subroutine json: called CC=1 SCAR=0.0
Pod documented
105 1 encode_json($_[0]);
106 }
107  
108  
109 sub yaml ($)
110 1 ▶{
Subroutine yaml: called CC=1 SCAR=0.0
Pod documented
111 1 require YAML::XS;
112 1 local $YAML::UseHeader = 0;
113 1 my $str = YAML::XS::Dump($_[0]);
114 1 $str =~ s{^---\n}{};
115 1 $str;
116 }
117  
118  
119 # TODO: for now the filtering of loop back only works on Linux
120 # and any system where the loopback interface is lo
121  
122 sub address (;$)
123 0 ▶{
Subroutine address: not called CC=4 SCAR=30.0
Pod documented
124 0 my($if) = @_;
125
126 0 require Sys::HostAddr;
127
128 0 ▶ my $filter = sub { !/^lo$/ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
129
130 0 ▶ if(defined $if)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $if) 0 0 0
131 {
132 0 ▶ if(ref $if eq 'Regexp')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $if eq 'Regexp') { } 0 0 0
elsif (ref $if eq 'ARRAY') { } 0 0 0
133 {
134 0 ▶ $filter = sub { $_ =~ $if };
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 }
136 elsif(ref $if eq 'ARRAY')
137 {
138 0 my %if = map { $_ => 1 } @$if;
139 0 ▶ $filter = sub { $if{$_} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 }
141 else
142 {
143 0 ▶ $filter = sub { $_ eq $if };
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 }
145 }
146  
147 0 my @if = grep { $filter->() } @{ Sys::HostAddr->new(ipv=>4)->interfaces };
148 0 map { @{ $_->addresses } } map { Sys::HostAddr->new(ipv => 4, interface => $_) } @if;
149 }
150  
151  
152 sub public_address ()
153 0 ▶{
Subroutine public_address: not called CC=1 SCAR=6.9
Pod documented
154 0 require Sys::HostAddr;
155 0 Sys::HostAddr->new(ipv=>4)->public;
156 }
157  
158  
159 sub interface ()
160 0 ▶{
Subroutine interface: not called CC=1 SCAR=6.9
Pod documented
161 0 require Sys::HostAddr;
162 0 grep !/^lo$/, @{ Sys::HostAddr->new(ipv=>4)->interfaces };
163 }
164  
165  
166 72 1;
167  
168 __END__
« blib/lib/Test/Clustericious/Config.pm ↑ Summary blib/lib/Clustericious/Command/which.pm »