lib/BugCli/Config.pm untested

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 294 CC 23 SCAR 36.6
CC
23
Cov
0%
print_config
90.0
write_config
56.0
run_config
42.0
CRAP 39.0
1 package BugCli::Config;
2  
3 push @BugCli::ISA, __PACKAGE__
4 unless grep { $_ eq __PACKAGE__ } @BugCli::ISA;
5  
6 use Config::Tiny;
7 use Text::Table;
8 use Term::ANSIColor qw(:constants);
9 use strict;
10  
11 our (@keys) =
12 qw|users commands product_product_id product_component_id server query product|;
13  
14 BEGIN {
15 $Term::ANSIColor::AUTORESET = 1;
16  
17 }
18  
19 sub config_show_users {
20 print "Section: User list\n";
21 my $tb = Text::Table->new(qw|Id Login|);
22 $tb->add( $_, $BugCli::uid_to_login{$_} )
23 foreach ( keys %BugCli::uid_to_login );
24 print $tb;
25 }
26  
27 sub config_show_commands {
28 }
29  
30 sub config_show_product {
31 my ($self) = @_;
32 print "Section: Product\n";
33 print "Current Product Id: $BugCli::config->{'product'}->{'product_id'}\n";
34 print
35 "Current Component Id: $BugCli::config->{'product'}->{'component_id'}\n";
36 print "Other variants: \n";
37 $self->config_show_product_product_id(1);
38 $self->config_show_product_component_id(1);
39 }
40  
41 sub config_show_product_product_id {
42  
43 my ( $self, $dont ) = @_;
44 print "Section: Products List\n";
45 my ($t) = Text::Table->new(qw|Id Name Description|);
46 my ($products) = $BugCli::dbh->fetch_select(
47 "columns" => [qw|id name description|],
48 "table" => "products"
49 );
50 foreach my $p ( @{$products} ) {
51 $t->add( $$p{id}, $$p{name}, $$p{description} );
52 }
53  
54 print $t;
55 }
56  
57 sub config_show_product_component_id {
58 my ( $self, $dont ) = @_;
59 print "Config Section: Component list (for selected product)\n";
60 my ($t) = Text::Table->new(qw|Id Name Description|);
61 my ($comps) = $BugCli::dbh->fetch_select(
62 "columns" => [qw|id name description|],
63 "table" => "components",
64 "criteria" =>
65 { "product_id" => $BugCli::config->{'product'}->{'product_id'} }
66 );
67  
68 foreach my $c ( @{$comps} ) {
69 $t->add( $$c{id}, $$c{name}, $$c{description} );
70 }
71  
72 print $t;
73  
74 }
75  
76 sub config_show_server {
77 my ($self) = @_;
78 print GREEN
79 . "Section:"
80 . WHITE
81 . " Bugzilla Server Options"
82 . RESET . "\n";
83 my ($tb) = Text::Table->new(qw|Option Value Description|);
84 $tb->add(
85 'server.host',
86 $BugCli::config->{server}->{host},
87 "MySQL server address"
88 );
89 $tb->add(
90 'server.username',
91 $BugCli::config->{server}->{username},
92 "MySQL Username for accessing the database"
93 );
94 $tb->add(
95 'server.password',
96 $BugCli::config->{server}->{password},
97 "MySQL access password"
98 );
99 $tb->add(
100 'server.table',
101 $BugCli::config->{server}->{table},
102 "Name of the MySQL table that holds the BugZilla"
103 );
104 $tb->add(
105 'server.login',
106 $BugCli::config->{server}->{login},
107 "Bugzilla login, usually email"
108 );
109 $tb->rule( '-', '+', "|" );
110 print $tb;
111  
112 }
113  
114 sub config_show_query {
115 my ( $self, $dont ) = @_;
116 my ( $i, $qname ) = ( 'Description/Code', 'Option' );
117 open Z2, ">-";
118  
119 format Z2 =
120 @<<<<<<<<<<... ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121 $qname , $i
122 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<...
123 $i
124 .
125 print GREEN . "\nSection: " . WHITE . "Available queries" . RESET . "\n"
126 unless defined $dont;
127 write Z2;
128  
129 foreach $qname ( keys %{ $BugCli::config->{query} } ) {
130 if ( exists $BugCli::config->{"query.descr"}->{$qname} ) {
131 $i = $BugCli::config->{"query.descr"}->{$qname};
132 }
133 else {
134 $i = $BugCli::config->{query}->{$qname};
135 }
136 $qname = "query.$qname" unless defined $dont;
137 write Z2;
138 }
139 close Z2;
140  
141 }
142  
143 # read_config (return true only if mandatory config options are set) {{{
144 sub read_config {
145 $BugCli::config = Config::Tiny->read($ENV{HOME} . '/bugcli.cfg');
146 return
147 exists $BugCli::config->{server}
148 && exists $BugCli::config->{server}->{host}
149 && exists $BugCli::config->{product}
150 && exists $BugCli::config->{server}->{username}
151 && exists $BugCli::config->{server}->{table}
152 && exists $BugCli::config->{product}->{product_id}
153 && exists $BugCli::config->{product}->{component_id}
154 && exists $BugCli::config->{server}->{login};
155 }
156  
157 # }}}
158  
159 # write_config (little wrap-up for writing config values) {{{
160 sub write_config {
161 my ( $self, $ahost, $auser, $apass, $atbl, $alogin ) = @_;
162 $BugCli::config = Config::Tiny->new() if(not defined $BugCli::config or !scalar(keys %{$BugCli::config}));
163 $BugCli::config->{server}->{host} = $ahost if defined $ahost;
164 $BugCli::config->{server}->{username} = $auser if defined $auser;
165 $BugCli::config->{server}->{password} = $apass if defined $apass;
166 $BugCli::config->{server}->{table} = $atbl if defined $atbl;
167 $BugCli::config->{server}->{login} = $alogin if defined $alogin;
168 $BugCli::config->write($ENV{HOME} . '/bugcli.cfg');
169  
170 }
171  
172 # }}}
173  
174 sub print_config {
175 my ( $self, $cmd, $cat, $val ) = @_;
176 no strict 'refs';
177 my ( @v, $f );
178 if ( defined $cat ) {
179 if ( $cat =~ /^(.*?)\.(.*?)$/ and not exists $BugCli::config->{$cat} ) {
180 $cat = $1;
181 $val = $2;
182 }
183 $f = "BugCli::Config::config_show_$cat";
184 $f .= "_$val" if defined $val;
185 if ( defined &$f ) {
186 push @v, $f;
187 }
188 else {
189 if ( defined $val ) {
190 if( exists $BugCli::config->{$cat}->{$val} ) {
191 print "Section: $cat Option: $val\n";
192 print "Value: $BugCli::config->{$cat}->{$val}\n";
193 }else {
194 print "Sorry, such sub-option '$val' doesn't exist! \n";
195 }
196
197 }
198 else {
199 print "Section: $cat \n";
200 print "Option: $_ Value: $BugCli::config->{$cat}->{$_}\n"
201 foreach ( keys %{ $BugCli::config->{$cat} } );
202 }
203 }
204 }
205 else {
206 @v = map { "BugCli::Config::config_show_$_" } @keys if not defined $cat;
207 }
208 foreach my $k (@v) {
209 if ( defined &$k ) { &$k($self); }
210 }
211  
212 }
213  
214 # run_config (asks for config settings for whole groups, or partial values) {{{
215 sub run_config {
216 my ( $self, $param ) = @_;
217 print_config(@_), return if defined $param and $param eq 'show';
218 my ( $ahost, $auser, $apass, $atbl, $alogin ) = (
219 $BugCli::config->{server}->{host} || '',
220 $BugCli::config->{server}->{username} || '',
221 $BugCli::config->{server}->{password} || '',
222 $BugCli::config->{server}->{table} || '',
223 $BugCli::config->{server}->{login} || ''
224 );
225 if ( $param && defined $BugCli::config ) { #ok. something specific...
226 if ( $param =~ /^(.*?)\.(.*)$/ ) {
227 print "Current value of $param is $BugCli::config->{$1}->{$2}.\n"
228 if exists $BugCli::config->{$1}->{$2};
229 $BugCli::config->{$1}->{$2} =
230 $self->prompt( "Please enter new value for $param:",
231 $BugCli::config->{$1}->{$2} );
232 }
233 else { print "No such config group found, sorry...\n"; }
234 return;
235 }
236 print "Sorry, no config found. Starting configuration. \n"
237 if not read_config();
238  
239 $ahost = $self->prompt( "MySQL Bugzilla Host: ($ahost)", $ahost );
240 $auser = $self->prompt( "MySQL Username: ($auser)", $auser );
241 $apass = $self->prompt( "MySQL Password: ($apass)", $apass );
242 $atbl = $self->prompt( "MySQL Bugzilla Table Name: ($atbl)", $atbl );
243 $alogin = $self->prompt( "User login (email): ($alogin)", $alogin );
244 $self->write_config( $ahost, $auser, $apass, $atbl, $alogin );
245 print "Trying to init your configuration...\n";
246 $self->init_mysql();
247  
248 my ($products) = $BugCli::dbh->fetch_select(
249 "columns" => [qw|id name description|],
250 "table" => "products"
251 );
252 print "Please select a product you're gonna work with: \n\n";
253 my ($t) = Text::Table->new(qw|Id Name Description|);
254 foreach my $p ( @{$products} ) {
255 $t->add( $$p{id}, $$p{name}, $$p{description} );
256 }
257 print $t;
258 my ($pid) = $self->prompt("Please select a product's id: ");
259 my ($comps) = $BugCli::dbh->fetch_select(
260 "columns" => [qw|id name description|],
261 "table" => "components",
262 "criteria" => { "product_id" => $pid }
263 );
264 $t->clear();
265  
266 print "\nPlease select a component to work on: ";
267 foreach my $c ( @{$comps} ) {
268 $t->add( $$c{id}, $$c{name}, $$c{description} );
269 }
270  
271 print $t;
272 my ($cid) = $self->prompt("Please select a component's id: ");
273 $BugCli::config->{product}->{product_id} = $pid;
274 $BugCli::config->{product}->{component_id} = $cid;
275 $BugCli::config->write($ENV{HOME} . '/bugcli.cfg');
276 BugCli::init_mysql();
277  
278 }
279  
280 # }}}
281  
282 # unload_defaults (extracts default config options) {{{
283 sub unload_defaults {
284 $BugCli::config->{"bind"}->{"c1"} = "bugs my";
285 $BugCli::config->{"command"}->{"c1"} = '\cQ';
286 $BugCli::config->{"query"}->{"all"} = "SELECT * FROM bugs";
287 $BugCli::config->{"query"}->{"allnew"} =
288 "SELECT * FROM bugs where resolution != 'FIXED'";
289 $BugCli::config->{"query"}->{'my'} =
290 "SELECT * FROM bugs where assigned_to=\%uid and (bug_status='NEW' or bug_status='ASSIGNED' or bug_status='REOPENED')";
291 $BugCli::config->{"query"}->{"myall"} =
292 "SELECT * FROM bugs where assigned_to=\%uid";
293 $BugCli::config->{"query"}->{"regexp"} =
294 "SELECT * FROM bugs where short_desc REGEXP '\%regexp'";
295 $BugCli::config->{"query.descr"}->{"my"} =
296 "All un-resolved bugs, owned by me (default key Ctrl-Q)";
297 }
298  
299 # }}}
300  
301 1;
302