blib/lib/CASCM/CLI.pm

filter: Statementstmt 29.0% 43 / 148 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 53.8% 14 / 26 Podpod 0.0% 0 / 1 totaltotal 22.8% 57 / 250 CC 30 SCAR 45.5
CC
30
Cov
20%
run
240.0
_init_context
132.0
_load_context
20.0
CRAP 94.5
↑ Summary
1 package CASCM::CLI;
2  
3 #######################
4 # LOAD MODULES
5 #######################
6 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use CASCM::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Log::Any::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Hash::Merge qw(merge);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Getopt::Mini ( later => 1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Log::Any::Adapter::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Object::Tiny qw(cascm exitval context);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #######################
24 # VERSION
25 #######################
26 1 our $VERSION = '0.1.1';
27  
28 #######################
29 # RUNNER
30 #######################
31 0 ▶sub run {
Subroutine run: not called CC=15 SCAR=54.8
Pod undocumented
32 0 my ( $self, @args ) = @_;
33  
34 # Initialize
35 0 $self->_init();
36 0 local @ARGV = ();
37  
38 # Parse main arguments
39 0 my $main_options = {};
40 0 ▶ GetOptionsFromArray( \@args, $main_options, $self->_main_opt_spec() )
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@args, $main_options, $self->_main_opt_spec) or $self->_print_bad_opts 0 0 0
41 or $self->_print_bad_opts();
42  
43 # Get Subcommand
44 0 my $subcmd = shift @args;
45 0 ▶ if ( $subcmd and ( $subcmd !~ m{^[a-z]+$}xi ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcmd and not $subcmd =~ /^[a-z]+$/xi) 0 0 0
Condition0%0/3
$subcmd and not $subcmd =~ /^[a-z]+$/xi
!ll&&!rl&&r
000
MC/DC0%0/2
$subcmd and not $subcmd =~ /^[a-z]+$/xi
$subcmd not $subcmd =~ /^[a-z]+$/xi
Truth table0%0/3
A and not(B)
A= $subcmdB= not $subcmd =~ /^[a-z]+$/xi
ABresult
0X0
100
111
46 0 $self->_print_bad_subcmd($subcmd);
47 }
48  
49 # Get Subcommand options
50 0 my %sub_options = getopt(
51 hungry_flags => 1,
52 argv => [@args],
53 );
54 0 ▶ delete $sub_options{_argv} if exists $sub_options{_argv};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sub_options{"_argv"} 0 0 0
55  
56 # Get Subcommand arguments
57 0 my @sub_args;
58 0 ▶ if ( exists $sub_options{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sub_options{""}) 0 0 0
59 0 ▶ if ( ref( $sub_options{''} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub_options{''} eq 'ARRAY') { } 0 0 0
60 0 push( @sub_args, @{ $sub_options{''} } );
61 }
62 else {
63 0 push( @sub_args, $sub_options{''} );
64 }
65 0 delete $sub_options{''};
66 } ## end if ( exists $sub_options...)
67  
68 # Make lowercase
69 0 ▶ $subcmd = '' if not defined $subcmd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subcmd 0 0 0
70 0 $subcmd = lc($subcmd);
71  
72 # Check for help
73 0 ▶ if ( ( $subcmd eq 'help' ) or ( $main_options->{help} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcmd eq "help" or $main_options->{"help"}) 0 0 0
Condition0%0/3
$subcmd eq "help" or $main_options->{"help"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$subcmd eq "help" or $main_options->{"help"}
$subcmd eq "help" $main_options->{"help"}
Truth table0%0/3
A or B
A= $subcmd eq "help"B= $main_options->{"help"}
ABresult
1X1
011
000
74 0 $self->_print_help();
75 0 exit 0;
76 } ## end if ( ( $subcmd eq 'help'...))
77  
78 # Check for version
79 0 ▶ if ( ( $subcmd eq 'version' ) or ( $main_options->{version} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcmd eq "version" or $main_options->{"version"}) 0 0 0
Condition0%0/3
$subcmd eq "version" or $main_options->{"version"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$subcmd eq "version" or $main_options->{"version"}
$subcmd eq "version" $main_options->{"version"}
Truth table0%0/3
A or B
A= $subcmd eq "version"B= $main_options->{"version"}
ABresult
1X1
011
000
80 0 $self->_print_version();
81 0 exit 0;
82 } ## end if ( ( $subcmd eq 'version'...))
83  
84 # Check for Subcommand
85 0 ▶ if ( not $subcmd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subcmd) 0 0 0
86 0 $self->_print_help();
87 0 exit 1;
88 } ## end if ( not $subcmd )
89  
90 # Initialize Logger
91 0 $self->_init_logger();
92  
93 # Initialize context
94 0 ▶ $self->_init_context( $main_options->{context} || '' );
Condition0%0/2
$main_options->{'context'} || ''
l!l
00
MC/DC0%0/1
$main_options->{'context'} || ''
$main_options->{'context'}
Truth table0%0/2
A
A= $main_options->{'context'}
Aresult
11
00
95  
96 # Initialize CASCM
97 0 $self->_init_cascm();
98  
99 # Check if subcommand is supported
100 0 ▶ if ( not $self->cascm()->can($subcmd) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->cascm->can($subcmd)) 0 0 0
101 0 $self->_print_bad_subcmd($subcmd);
102 }
103  
104 # Run subcommand
105 0 $self->cascm()->$subcmd( {%sub_options}, @sub_args );
106 0 $self->{exitval} = $self->cascm()->exitval();
107  
108 0 return 1;
109 } ## end sub run
110  
111 #######################
112 # INTERNAL
113 #######################
114  
115 # Initialize
116 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
117 0 my ($self) = @_;
118  
119 # Setup getopt long
120 0 Getopt::Long::Configure('default');
121 0 Getopt::Long::Configure('pass_through');
122 0 Getopt::Long::Configure('no_auto_abbrev');
123  
124 # Set exit value
125 0 $self->{exitval} = 0;
126  
127 0 return 1;
128 } ## end sub _init
129  
130 # Main option spec
131 0 ▶sub _main_opt_spec {
Subroutine _main_opt_spec: not called CC=1 SCAR=6.9
132  
133 return (
134 0 'help', # Print Help
135 'version', # Print Version
136 'context=s', # Set Context file
137 );
138  
139 } ## end sub _main_opt_spec
140  
141  
142 0 ▶sub _print_bad_opts {
Subroutine _print_bad_opts: not called CC=1 SCAR=6.9
143 0 print STDERR "Invalid Options. See 'hv --help'\n";
144 0 exit 1;
145 } ## end sub _print_bad_opts
146  
147  
148 0 ▶sub _print_bad_subcmd {
Subroutine _print_bad_subcmd: not called CC=1 SCAR=6.9
149 0 my ( $self, $cmd ) = @_;
150 0 print STDERR "Invalid command '$cmd'. See 'hv --help'\n";
151 0 exit 1;
152 } ## end sub _print_bad_subcmd
153  
154  
155 0 ▶sub _print_help {
Subroutine _print_help: not called CC=1 SCAR=6.9
156 0 my ($self) = @_;
157  
158 0 $self->_print_version();
159  
160 0 print <<'_EO_HELP';
161 USAGE: hv [options] command [command_options] [arguments]
162  
163 Options:
164  
165 help Print this message
166 version Print version information
167 context Specify the context file
168  
169 Commands:
170  
171 This is typically your Harvest CLI command
172 Please see the documentation of CASCM::Wrapper for the list of
173 supported commands
174 Command options and arguments are passed through to the harvest CLI
175  
176 --
177 _EO_HELP
178  
179 0 return 1;
180 } ## end sub _print_help
181  
182  
183 0 ▶sub _print_version {
Subroutine _print_version: not called CC=1 SCAR=6.9
184 0 my ($self) = @_;
185 0 print "hv version-${VERSION}\n";
186 0 return 1;
187 } ## end sub _print_version
188  
189  
190 0 ▶sub _init_logger {
Subroutine _init_logger: not called CC=1 SCAR=6.9
191 0 my ($self) = @_;
192  
193 Log::Any::Adapter->set(
194 'Callback',
195 min_level => 'info',
196 0 ▶ logging_cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
197 0 my ( $method, $self, $format, @params ) = @_;
198 0 chomp( $format, @params );
199 0 $method = uc($method);
200 0 ▶ if ( ( $method eq 'WARNING' ) or ( $method eq 'ERROR' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'WARNING' or $method eq 'ERROR') { } 0 0 0
Condition0%0/3
$method eq "WARNING" or $method eq "ERROR"
l!l&&r!l&&!r
000
MC/DC0%0/2
$method eq "WARNING" or $method eq "ERROR"
$method eq "WARNING" $method eq "ERROR"
Truth table0%0/3
A or B
A= $method eq "WARNING"B= $method eq "ERROR"
ABresult
1X1
011
000
201 0 print STDERR "[$method] $format\n";
202 }
203 else {
204 0 print "[$method] $format\n";
205 }
206 },
207 0 );
208  
209 0 return 1;
210 } ## end sub _init_logger
211  
212  
213 0 ▶sub _init_context {
Subroutine _init_context: not called CC=11 SCAR=48.8
214 0 my ( $self, $main_ctx_file ) = @_;
215  
216 # Check for system-wide context in $CASCM_HOME
217 0 my $system_context = {};
218 0 ▶ my $cascm_home = $ENV{CA_SCM_HOME} || $ENV{HARVEST_HOME} || '';
Condition0%0/5
$ENV{'CA_SCM_HOME'} || $ENV{'HARVEST_HOME'} || ''
l!l
00
$ENV{"CA_SCM_HOME"} or $ENV{"HARVEST_HOME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'CA_SCM_HOME'} || $ENV{'HARVEST_HOME'} || ''
$ENV{"CA_SCM_HOME"} $ENV{"HARVEST_HOME"}
Truth table0%0/3
A or B
A= $ENV{"CA_SCM_HOME"}B= $ENV{"HARVEST_HOME"}
ABresult
1X1
011
000
219 0 ▶ if ($cascm_home) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cascm_home) 0 0 0
220 0 my $system_ctx_file = File::Spec->catfile( $cascm_home, 'hvcontext' );
221 0 ▶ if ( -e $system_ctx_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $system_ctx_file) 0 0 0
222 0 $system_context = $self->_load_context($system_ctx_file);
223 }
224 } ## end if ($cascm_home)
225  
226 # Check for user's context file
227 0 my $user_context = {};
228 0 my $user_ctx_file;
229 0 ▶ if ( $ENV{HVCONTEXT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'HVCONTEXT'}) { } 0 0 0
230 0 $user_ctx_file = $ENV{HVCONTEXT};
231 }
232 else {
233 0 my $homedir = File::HomeDir->my_home();
234 0 ▶ if ( $homedir and -e $homedir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($homedir and -e $homedir) 0 0 0
Condition0%0/3
$homedir and -e $homedir
!ll&&!rl&&r
000
MC/DC0%0/2
$homedir and -e $homedir
$homedir -e $homedir
Truth table0%0/3
A and B
A= $homedirB= -e $homedir
ABresult
0X0
100
111
235 0 $user_ctx_file = File::Spec->catfile( $homedir, '.hvcontext' );
236 }
237 } ## end else [ if ( $ENV{HVCONTEXT} )]
238 0 ▶ if ( -e $user_ctx_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $user_ctx_file) 0 0 0
239 0 $user_context = $self->_load_context($user_ctx_file);
240 }
241  
242 # Check for current context
243 0 my $current_context = {};
244 0 my $current_ctx_file;
245 0 ▶ if ($main_ctx_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main_ctx_file) { } 0 0 0
246 0 $current_ctx_file = $main_ctx_file;
247 }
248 else {
249 0 $current_ctx_file = '.hvcontext';
250 }
251 0 ▶ if ( -e $current_ctx_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $current_ctx_file) 0 0 0
252 0 $current_context = $self->_load_context($current_ctx_file);
253 }
254  
255 # Merge Context
256 0 my $current_and_user = merge( $current_context, $user_context );
257 0 my $context = merge( $current_and_user, $system_context );
258  
259 0 $self->{context} = $context;
260 0 return 1;
261 } ## end sub _init_context
262  
263  
264 0 ▶sub _load_context {
Subroutine _load_context: not called CC=4 SCAR=30.0
265 0 my ( $self, $file ) = @_;
266  
267 0 ▶ my $config = Config::Tiny->read($file)
Branch: 1 branch
BranchTrueFalseTotal
my $config = "Config::Tiny"->read($file) or die "ERROR: Failed to read $file" 0 0 0
268 or die "ERROR: Failed to read $file";
269  
270 0 my $context = {};
271 0 foreach ( keys %{$config} ) {
272 0 ▶ if ( $_ eq '_' ) { $context->{global} = $config->{$_}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq '_') { } 0 0 0
273 0 else { $context->{$_} = $config->{$_}; }
274 } ## end foreach ( keys %{$config} )
275  
276 0 return $context;
277 } ## end sub _load_context
278  
279  
280 0 ▶sub _init_cascm {
Subroutine _init_cascm: not called CC=1 SCAR=6.9
281 0 my ($self) = @_;
282 0 my $cascm = CASCM::Wrapper->new(
283 {
284 parse_logs => 1,
285 }
286 );
287 0 $cascm->set_context( $self->context() );
288 0 $self->{cascm} = $cascm;
289 0 return 1;
290 } ## end sub _init_cascm
291  
292 #######################
293 1 1;
294  
295 __END__
↑ Summary