| 1 | package Serge::Application; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Find qw(find); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Spec::Functions qw(rel2abs catfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Getopt::Long qw(:config no_auto_abbrev no_ignore_case pass_through); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Serge::Config::Collector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Serge; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | $self->{commands} = {}; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | return $self; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub run { | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=18 SCAR=58.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | my ($help, $debug, $version); | |||||||||||||||||||||||||||
| 29 | 0 | my $result = GetOptions( | |||||||||||||||||||||||||||
| 30 | 'help' => \$help, | ||||||||||||||||||||||||||||
| 31 | 'debug' => \$debug, | ||||||||||||||||||||||||||||
| 32 | 'version' => \$version, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | my $command = shift @ARGV; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # print out version when passing the flag to a bare `serge` command | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ($version && $command eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$version and $command eq ""
MC/DC0%0/2
$version and $command eq ""
$version
$command eq ""
Truth table0%0/3
A and B
A= $versionB= $command eq ""
| |||||||||||||||||||||||||||||
| 39 | 0 | print "Serge $Serge::VERSION\n"; | |||||||||||||||||||||||||||
| 40 | 0 | exit(0); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if (!$result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | $self->error("Failed to parse some command-line parameters."); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $self->{debug} = 1; | |||||||||||||||||||||||||||
| 49 | 0 | eval('use Carp::Always;'); | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | warn "Hint: please install 'Carp::Always' module to get extended backtrace information on die()\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # convert --help parameter anywhere in the path | ||||||||||||||||||||||||||||
| 54 | # to an equivalent of 'serge help <rest of params>' | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | unshift @ARGV, 'help' if $help; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | $self->load_command_plugins; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my $handler = $self->{commands}->{$command}; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (!$handler) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $self->error("Unknown command: $command\n"); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $handler->{plugin}->{debug} = 1 if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my @commands = ($command); | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | if (exists $handler->{combine_with}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | my %combine; | |||||||||||||||||||||||||||
| 70 | 0 | @combine{@{$handler->{combine_with}}} = @{$handler->{combine_with}}; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | while (scalar @ARGV && exists $combine{$ARGV[0]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar @ARGV and exists $combine{$ARGV[0]}
MC/DC0%0/2
scalar @ARGV and exists $combine{$ARGV[0]}
scalar @ARGV
exists $combine{$ARGV[0]}
Truth table0%0/3
A and B
A= scalar @ARGVB= exists $combine{$ARGV[0]}
| |||||||||||||||||||||||||||||
| 72 | 0 | push @commands, shift @ARGV; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | eval { | |||||||||||||||||||||||||||
| 77 | map { | ||||||||||||||||||||||||||||
| 78 | 0 | $handler->{plugin}->init($_); | |||||||||||||||||||||||||||
| 79 | } @commands; | ||||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $self->error($@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($handler->{need_config}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | eval { | |||||||||||||||||||||||||||
| 85 | 0 | $self->{config_collector} = Serge::Config::Collector->new(@ARGV); | |||||||||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $self->error($@, 3, 1); # exit code = 3, show no synopsis | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my @a = $self->get_config_files; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (scalar @a == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $self->error("This command expects configuration files to work against, but none were provided"); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | eval { | |||||||||||||||||||||||||||
| 98 | map { | ||||||||||||||||||||||||||||
| 99 | 0 | $handler->{plugin}->validate_data($_); | |||||||||||||||||||||||||||
| 100 | } @commands; | ||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $self->error($@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $funcref = $handler->{handler}; | |||||||||||||||||||||||||||
| 106 | # run the commands in the context of the plugin object | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | return &$funcref($handler->{plugin}, exists $handler->{combine_with} ? \@commands : $command); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub load_command_plugins { | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_command_plugins: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my @plugins; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # find plugins in the 'Command' subfolder relative to the location of the current file (Application.pm) | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | find(sub { | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if(-f $_ && /\.pm$/) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
Condition0%0/3
-f $_ and /\.pm$/
MC/DC0%0/2
-f $_ and /\.pm$/
-f $_
/\.pm$/
Truth table0%0/3
A and B
A= -f $_B= /\.pm$/
| |||||||||||||||||||||||||||||
| 119 | 0 | $_ =~ s/\.pm$//i; | |||||||||||||||||||||||||||
| 120 | 0 | push @plugins, $_; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 0 | }, catfile(dirname(rel2abs(__FILE__)), 'Command')); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | foreach my $plugin (@plugins) { | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | print "Loading command plugin: $plugin\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $class = 'Serge::Command::'.$plugin; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $p; | |||||||||||||||||||||||||||
| 130 | 0 | eval('use '.$class.'; $p = '.$class.'->new($self);'); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | die "Can't create instance for '$class': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $exported_commands = $p->get_commands; | |||||||||||||||||||||||||||
| 134 | 0 | foreach my $command (keys %$exported_commands) { | |||||||||||||||||||||||||||
| 135 | 0 | my $handler = $exported_commands->{$command}; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | die "Definition for '$command' command already exists" if exists $self->{commands}->{$command}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | die "No 'handler' parameter defined for '$command' command handler" unless exists $handler->{handler}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | $handler->{plugin} = $p; | |||||||||||||||||||||||||||
| 141 | 0 | $self->{commands}->{$command} = $handler; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub get_config_files { | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_config_files: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 0 | return $self->{config_collector}->get_config_files; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub get_config_object { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_config_object: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 153 | 0 | return $self->{config_collector}->get_config_object(@_); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub known_command { | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | my ($self, $command) = @_; | ||||||||||||||||||||||||||
|
Subroutine
known_command: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 158 | 0 | return exists $self->{commands}->{$command}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub show_synopsis { | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
show_synopsis: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | print qq| | |||||||||||||||||||||||||||
| 165 | Usage: | ||||||||||||||||||||||||||||
| 166 | serge <command> [command-specific-options] [--debug] | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Get help: | ||||||||||||||||||||||||||||
| 169 | serge help [command] | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |; | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub error { | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | my ($self, $message, $exitstatus, $no_synopsis) = @_; | ||||||||||||||||||||||||||
|
Subroutine
error: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | $exitstatus = 1 unless defined $exitstatus; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 0 | chomp $message; | |||||||||||||||||||||||||||
| 178 | 0 | print $message."\n"; | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | $self->show_synopsis unless $no_synopsis; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | exit($exitstatus); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 1 | 1; | |||||||||||||||||||||||||||