File Coverage

blib/lib/App/Dochazka/CLI/Commands/Activity.pm
Criterion Covered Total %
statement 29 59 49.1
branch 2 28 7.1
condition 0 4 0.0
subroutine 10 10 100.0
pod 1 1 100.0
total 42 102 41.1


line stmt bran cond sub pod time code
1             # *************************************************************************
2             # Copyright (c) 2014-2016, SUSE LLC
3             #
4             # All rights reserved.
5             #
6             # Redistribution and use in source and binary forms, with or without
7             # modification, are permitted provided that the following conditions are met:
8             #
9             # 1. Redistributions of source code must retain the above copyright notice,
10             # this list of conditions and the following disclaimer.
11             #
12             # 2. Redistributions in binary form must reproduce the above copyright
13             # notice, this list of conditions and the following disclaimer in the
14             # documentation and/or other materials provided with the distribution.
15             #
16             # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17             # used to endorse or promote products derived from this software without
18             # specific prior written permission.
19             #
20             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30             # POSSIBILITY OF SUCH DAMAGE.
31             # *************************************************************************
32             #
33             # Activity commands
34             package App::Dochazka::CLI::Commands::Activity;
35              
36 20     20   246 use 5.012;
  20         43  
37 20     20   63 use strict;
  20         21  
  20         332  
38 20     20   60 use warnings;
  20         21  
  20         415  
39              
40 20     20   58 use App::CELL qw( $CELL );
  20         21  
  20         1301  
41 20     20   76 use App::Dochazka::CLI qw( $debug_mode );
  20         18  
  20         1238  
42 20     20   8339 use App::Dochazka::CLI::Util qw( parse_test rest_error );
  20         44  
  20         1269  
43 20     20   104 use Data::Dumper;
  20         26  
  20         772  
44 20     20   75 use Exporter 'import';
  20         22  
  20         449  
45 20     20   65 use Web::MREST::CLI qw( send_req );
  20         25  
  20         6750  
46              
47              
48              
49              
50             =head1 NAME
51              
52             App::Dochazka::CLI::Commands::Activity - Activity commands
53              
54              
55              
56              
57             =head1 PACKAGE VARIABLES
58              
59             =cut
60              
61             our @EXPORT_OK = qw( activity_all );
62              
63              
64              
65              
66             =head1 FUNCTIONS
67              
68             The functions in this module are called from the parser when it recognizes a command.
69              
70             =cut
71              
72             =head2 activity_all
73              
74             ACTIVITY
75             ACTIVITY ALL
76             ACTIVITY ALL DISABLED
77              
78             =cut
79              
80             sub activity_all {
81 3 50   3 1 11 print "Entering " . __PACKAGE__ . "::activity_all\n" if $debug_mode;
82 3         6 my ( $ts, $th ) = @_;
83              
84             # parse test
85 3 50       23 return parse_test( $ts, $th ) if $ts eq 'PARSE_TEST';
86              
87             # print debug info
88 0 0         if ( $debug_mode ) {
89 0           print "Entering " . __PACKAGE__ . "::activity_all\n";
90 0           print Dumper( $th );
91             }
92              
93 0           my $disabled = $th->{'DISABLED'}; # boolean
94 0 0         my $uri = $disabled
95             ? "activity/all/disabled"
96             : "activity/all";
97              
98 0           my $status = send_req( 'GET', $uri );
99 0 0         return rest_error( $status, "Retrieve activity list" ) unless $status->ok;
100 0 0         if ( $status->{'count'} == 0 ) {
101 0           my $message = "No activities in database (maybe try SHOW ACTIVITY ALL DISABLED)";
102 0           return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', payload => $message );
103             }
104              
105             # 1st pass, determine maximum length of each field
106 0           my ( $max_aid, $max_code, $max_desc ) = ( 0, 0, 0 );
107 0           foreach my $act_h ( @{ $status->payload } ) {
  0            
108             my ( $l_aid, $l_code, $l_desc ) = (
109             length( $act_h->{'aid'} ),
110             length( $act_h->{'code'} ),
111 0           length( $act_h->{'long_desc'} ),
112             );
113 0 0         $max_aid = $l_aid if $l_aid > $max_aid;
114 0 0         $max_code = $l_code if $l_code > $max_code;
115 0 0         if ( defined $l_desc ) {
116 0 0         $max_desc = $l_desc if $l_desc > $max_desc;
117             } else {
118 0           $max_desc = 0;
119             }
120             }
121              
122             # 2nd pass: assemble the table
123 0           my $r = "\n";
124 0           foreach my $act_h ( @{ $status->payload } ) {
  0            
125 0 0         my $format = $disabled ? '%1s' : '';
126 0           $format .=
127             "%" . ($max_aid + 1) . "s " .
128             "%-" . ($max_code) . "s " .
129             "%-" . ($max_desc) . "s\n";
130 0 0         if ( $disabled ) {
131 0 0         my $d = ( $act_h->{'disabled'} ) ? '*' : ' ';
132             $r .= sprintf $format, $d, $act_h->{'aid'}, $act_h->{'code'},
133 0   0       ( $act_h->{'long_desc'} || '(not set)' );
134             } else {
135             $r .= sprintf $format, $act_h->{'aid'}, $act_h->{'code'},
136 0   0       ( $act_h->{'long_desc'} || '(not set)' );
137             }
138             }
139 0 0         $r .= "\nActivities marked with an asterisk (*) in the first column are disabled\n"
140             if $disabled;
141              
142 0           return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', payload => $r );
143             }
144              
145              
146             1;