lib/Rex/Helper/Rexfile/ParamLookup.pm

filter: Statementstmt 100% 55 / 55 Branchbran 87.5% 21 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 95.4% 83 / 87 CC 13 SCAR 22.2
CC
13
Cov
96%
param_lookup
13.0
BEGIN
1.0
BEGIN
1.0
CRAP 9.2
« lib/Rex/Inventory/Hal/Object.pm ↑ Summary lib/Rex/Interface/Exec/Local.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Helper::Rexfile::ParamLookup;
6  
7 32 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 32 our $VERSION = '1.16.1'; # VERSION
11  
12 32 ▶use Devel::Caller;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 require Rex::Exporter;
15 32 require Rex::Commands;
16  
17 32 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 32 ▶use vars qw (@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 32 @EXPORT = qw(param_lookup);
21  
22 10 ▶sub param_lookup {
Subroutine param_lookup: called CC=13 SCAR=25.7
Pod undocumented
23 10 my ( $key, $default ) = @_;
24  
25 10 my $ret;
26  
27 10 my ($caller_pkg) = caller(0);
28  
29 10 my @args = Devel::Caller::caller_args(1);
30 10 ▶ if ( ref $args[0] eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "HASH") 7 3 10
31 7 ▶ if ( exists $args[0]->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args[0]{$key}) 5 2 7
32 5 $ret = $args[0]->{$key};
33 }
34 }
35  
36 10 ▶ if ( !$ret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 5 5 10
37  
38 # check if cmdb is loaded
39 5 my ($use_cmdb) = grep { m/CMDB\.pm/ } keys %INC;
40 5 ▶ if ($use_cmdb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_cmdb) 5 0 5
41  
42 # look inside cmdb
43 5 my $cmdb_key = "${caller_pkg}::$key";
44 5 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
45  
46 5 ▶ if ( !$ret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 5 0 5
47  
48 # check in resource
49 5 ▶ if ( Rex::Resource->is_inside_resource ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Resource"->is_inside_resource) 2 3 5
50 2 $cmdb_key =
51 Rex::Resource->get_current_resource->display_name . "::$key";
52 2 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
53 }
54  
55 5 ▶ if ( !$ret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 4 1 5
56  
57 # check in task
58 4 my $task = Rex::Commands::task();
59 4 ▶ if ($task) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task) 4 0 4
60 4 my $task_name = $task->{name};
61 4 $task_name =~ s/:/::/;
62 4 $cmdb_key = $task_name . "::$key";
63 4 $ret = Rex::Commands::get( Rex::CMDB::cmdb($cmdb_key) );
64 }
65 }
66 }
67  
68 5 ▶ if ( !$ret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 4 1 5
69  
70 # check in global namespace
71 4 $ret = Rex::Commands::get( Rex::CMDB::cmdb($key) );
72 }
73 }
74 }
75  
76 10 ▶ if ( !$ret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 3 7 10
77 3 $ret = $default;
78 }
79  
80 10 ▶ if ( Rex::Resource->is_inside_resource ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Resource"->is_inside_resource) 4 6 10
81 4 Rex::Resource->get_current_resource()->set_parameter( $key => $ret );
82 }
83  
84 10 ▶ if ( !Rex::Resource->is_inside_resource ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Rex::Resource"->is_inside_resource) 6 4 10
85 6 Rex::Commands::task()->set_opt( $key => $ret );
86 }
87  
88 10 return $ret;
89 }
90  
91 32 1;
92  
93 =pod
94  
95 =head1 NAME
96  
97 Rex::Helper::Rexfile::ParamLookup - A command to manage task parameters.
98  
99 A command to manage task parameters. Additionally it register the parameters as template values.
100  
101 This module also looks inside a CMDB (if present) for a valid key.
102  
103  
104 =head1 SYNOPSIS
105  
106 task "setup", sub {
107 my $var = param_lookup "param_name", "default_value";
108 };
109  
110 =head1 LOOKUP
111  
112 First I<param_lookup> checks the task parameters for a valid parameter. If none is found and if a CMDB is used, it will look inside the cmdb.
113  
114 If your module is named "Rex::NTP" than it will first look if the key "Rex::NTP::param_name" exists. If it doesn't exists it checks for the key "param_name".
115  
116 =cut
« lib/Rex/Inventory/Hal/Object.pm ↑ Summary lib/Rex/Interface/Exec/Local.pm »