lib/Rex/Commands/PkgConf.pm

filter: Statementstmt 61.7% 21 / 34 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 100% 2 / 2 totaltotal 63.0% 29 / 46 CC 2 SCAR 8.0
CC
2
Cov
58%
set_pkgconf
6.0
get_pkgconf
2.0
BEGIN
1.0
CRAP 2.2
« lib/Rex/Shared/Var.pm ↑ Summary blib/lib/Rex/Helper/File/Spec.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::PkgConf - Configure packages
8  
9 =head1 DESCRIPTION
10  
11 With this module you can configure packages. Currently it only supports Debian
12 (using debconf), but it is designed to be extendable.
13  
14 =head1 SYNOPSIS
15  
16 my %options = get_pkgconf('postfix');
17 say $options{'postfix/relayhost'}->{value};
18  
19 # Only obtain one value
20 my %options = get_pkgconf('postfix', 'postfix/relayhost');
21 say $options{'postfix/relayhost'}->{value};
22  
23 # Set options
24 set_pkgconf("postfix", [
25 {question => 'chattr', type => 'boolean', value => 'false'},
26 {question => 'relayhost', type => 'string', value => 'relay.example.com'},
27 ]);
28  
29 # Don't update if it's already set
30 set_pkgconf("mysql-server-5.5", [
31 {question => 'mysql-server/root_password', type => 'string', value => 'mysecret'},
32 {question => 'mysql-server/root_password_again', type => 'string', value => 'mysecret'},
33 ], no_update => 1);
34  
35 =head1 EXPORTED FUNCTIONS
36  
37 =cut
38  
39 package Rex::Commands::PkgConf;
40  
41 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 our $VERSION = '1.16.1'; # VERSION
45  
46 1 ▶use Rex::PkgConf;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 require Rex::Exporter;
50  
51 1 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 @EXPORT = qw(get_pkgconf set_pkgconf);
55  
56 =head2 get_pkgconf($package, [$question])
57  
58 Use this to query existing package configurations.
59  
60 Without a question specified, it will return all options for
61 the specified package as a hash.
62  
63 With a question specified, it will return only that option
64  
65 Each question is returned with the question as the key, and
66 the value as a hashref. The hashref contains the keys: question,
67 value and already_set. already_set is true if the question has
68 already been answered.
69  
70 # Only obtain one value
71 my %options = get_pkgconf('postfix', 'postfix/relayhost');
72 say $options{'postfix/relayhost'}->{question};
73 say $options{'postfix/relayhost'}->{value};
74 say $options{'postfix/relayhost'}->{already_set};
75  
76 =cut
77  
78 0 ▶sub get_pkgconf {
Subroutine get_pkgconf: not called CC=1 SCAR=6.9
Pod documented
79 0 my ($package) = @_;
80  
81 Rex::get_current_connection()->{reporter}
82 0 ->report_resource_start( type => "pkgconf", name => $package );
83  
84 0 my $pkgconf = Rex::PkgConf->get;
85  
86 0 $pkgconf->get_options($package);
87 }
88  
89 =head2 set_pkgconf($package, $values, [%options])
90  
91 Use this to set package configurations.
92  
93 At least the package name and values must be specified. Values
94 must be an array ref, with each item containing a hashref with
95 the attributes specified that are required by the package
96 configuration program.
97  
98 For example, for debconf, this must be the question, the type
99 and answer. In this case, the types can be any accetable debconf
100 type: string, boolean, select, multiselect, note, text, password.
101  
102 Optionally the option "no_update" may be true, in which case the
103 question will not be updated if it has already been set.
104  
105 See the synopsis for examples.
106  
107 =cut
108  
109 0 ▶sub set_pkgconf {
Subroutine set_pkgconf: not called CC=2 SCAR=17.9
Pod documented
110 0 my ( $package, $values, %options ) = @_;
111  
112 Rex::get_current_connection()->{reporter}
113 0 ->report_resource_start( type => "pkgconf", name => $package );
114  
115 0 my $pkgconf = Rex::PkgConf->get;
116  
117 0 my $return = $pkgconf->set_options( $package, $values, %options );
118  
119 0 ▶ if ( $return->{changed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return->{'changed'}) { } 0 0 0
120 Rex::get_current_connection()->{reporter}->report(
121 0 changed => 1,
122 0 message => "Configuration values updated: @{$return->{names}}",
123 );
124 }
125 else {
126 0 Rex::get_current_connection()->{reporter}->report( changed => 0, );
127 }
128  
129 Rex::get_current_connection()->{reporter}
130 0 ->report_resource_end( type => "pkgconf", name => $package );
131 }
132  
133 1 1;
« lib/Rex/Shared/Var.pm ↑ Summary blib/lib/Rex/Helper/File/Spec.pm »