File Coverage

blib/lib/App/CELL/Util.pm
Criterion Covered Total %
statement 41 45 91.1
branch 8 10 80.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 3 3 100.0
total 63 71 88.7


line stmt bran cond sub pod time code
1             # *************************************************************************
2             # Copyright (c) 2014-2020, 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             package App::CELL::Util;
34              
35 17     17   94 use strict;
  17         29  
  17         475  
36 17     17   73 use warnings;
  17         25  
  17         392  
37 17     17   303 use 5.012;
  17         48  
38              
39 17     17   96 use Data::Dumper;
  17         23  
  17         815  
40 17     17   6514 use Date::Format;
  17         107256  
  17         1056  
41 17     17   7974 use Params::Validate qw( :all );
  17         132059  
  17         2883  
42              
43             =head1 NAME
44              
45             App::CELL::Util - generalized, reuseable functions
46              
47              
48              
49             =head1 SYNOPSIS
50              
51             use App::CELL::Util qw( utc_timestamp is_directory_viable );
52              
53             # utc_timestamp
54             print "UTC time is " . utc_timestamp() . "\n";
55              
56             # is_directory_viable
57             my $status = is_directory_viable( $dir_foo );
58             print "$dir_foo is a viable directory" if $status->ok;
59             if ( $status->not_ok ) {
60             my $problem = $status->payload;
61             print "$dir_foo is not viable because $problem\n";
62             }
63              
64             =cut
65              
66              
67             =head1 EXPORTS
68              
69             This module provides the following public functions:
70              
71             =over
72              
73             =item C
74              
75             =item C
76              
77             =item C
78              
79             =back
80              
81             =cut
82              
83 17     17   158 use Exporter qw( import );
  17         47  
  17         3783  
84             our @EXPORT_OK = qw( is_directory_viable stringify_args utc_timestamp );
85              
86              
87              
88             =head1 PACKAGE VARIABLES
89              
90             =cut
91              
92             our $not_viable_reason = '';
93              
94              
95              
96             =head1 FUNCTIONS
97              
98              
99             =head2 is_directory_viable
100              
101             Run viability checks on a directory. Takes: full path to directory. Returns
102             true (directory viable) or false (directory not viable). If the directory
103             is not viable, it sets the package variable
104             C<< $App::CELL::Util::not_viable_reason >>.
105              
106             =cut
107              
108             sub is_directory_viable {
109              
110 15     15 1 1101 my $confdir = shift;
111 15         34 my $problem = '';
112              
113             CRIT_CHECK: {
114 15 100       25 if ( not -e $confdir ) {
  15         213  
115 3         17 $problem = "does not exist";
116 3         17 last CRIT_CHECK;
117             }
118 12 50       150 if ( not -d $confdir ) {
119 0         0 $problem = "exists but not a directory";
120 0         0 last CRIT_CHECK;
121             }
122 12 50 33     332 if ( not -r $confdir or not -x $confdir ) {
123 0         0 $problem = "directory exists but insufficient permissions";
124 0         0 last CRIT_CHECK;
125             }
126             } # CRIT_CHECK
127              
128 15 100       65 if ( $problem ) {
129 3         28 $not_viable_reason = $problem;
130 3         14 return 0;
131             }
132              
133 12         59 return 1;
134             }
135              
136              
137             =head2 stringify_args
138              
139             Convert args (or any data structure) into a string -- useful for error
140             reporting.
141              
142             =cut
143              
144             sub stringify_args {
145 179     179 1 300 my $args = shift;
146 179         268 local $Data::Dumper::Indent = 0;
147 179         233 local $Data::Dumper::Terse = 1;
148 179         206 my $args_as_string;
149 179 100       342 if ( ref $args ) {
150 176         396 $args_as_string = Dumper( $args );
151             } else {
152 3         6 $args_as_string = $args;
153             }
154 179         9427 return $args_as_string;
155             }
156              
157              
158             =head2 utc_timestamp
159              
160             =cut
161              
162             sub utc_timestamp {
163 3     3 1 21 return uc time2str("%Y-%m-%d %H:%M %Z", time, 'GMT');
164             }
165              
166              
167             # END OF App::CELL::Util.pm
168             1;