vuser
view release on metacpan or search on metacpan
my %cfg;
tie %cfg, 'Config::IniFiles', (-file => $config_file);
if (@Config::IniFiles::errors) {
warn "There were errors loading $config_file\n";
foreach my $error (@Config::IniFiles::errors) {
warn "$error\n";
}
die "Please correct the errors and try again\n";
}
$DEBUG = check_bool $cfg{'vuser'}{'debug'};
$DEBUG = 1 if ($debug);
if ($debug) {
use Data::Dumper;
print Dumper \%cfg;
}
our $log = VUser::Log->new(\%cfg, 'vuser');
## Load extensions
if ($cfg{'vuser'}{'include paths'}) {
my @inc_paths = ();
if (ref $cfg{'vuser'}{'include paths'} eq 'ARRAY') {
@inc_paths = @{ $cfg{'vuser'}{'include paths'} };
} else {
@inc_paths = ($cfg{'vuser'}{'include paths'});
}
my @paths = map { split(/:|$/m) } @inc_paths;
foreach my $path (@paths) {
my $p = strip_ws($path);
$log->log(LOG_INFO, "Adding '$p' to \@INC");
push @INC, $p;
}
}
my $eh = new VUser::ExtHandler (\%cfg);
$eh->load_extensions(\%cfg);
$eh->register_task('version', '', \&version, -10);
$log->log(LOG_DEBUG, "Config loaded from $config_file");
my $keyword = shift @ARGV || 'help';
my $action = shift @ARGV;
# Actions cannot start with -
if (defined $action
and $action =~ /^-/) {
unshift @ARGV, $action;
$action = '';
}
$action = '' unless defined $action;
my $exit_code = 0;
# Ok. Now it's time to do the action.
my $real_user = 'unknown';
my $ent;
eval { $ent = getpwuid($<); }; # Get the user running this.
$real_user = $ent->name if (defined $ent);
$log->log(LOG_NOTICE, "%s running %s %s", $real_user, $keyword, $action);
my @rs;
eval { @rs = $eh->run_tasks($keyword, $action, \%cfg); };
if ($@) {
# Should we change the exit code if there are warnings
# but everything completes?
$exit_code = 0;
warn $@;
}
if ($debug) {
print "RS: "; use Data::Dumper; print Dumper \@rs;
print("Show rs ($cfg{'vuser'}{'show result set'}) ? ",
(check_bool($cfg{'vuser'}{'show result set'})? "Yes":"No"),
"\n"
);
}
## Check for errors
my @errors = VUser::ResultSet::get_all_errors(@rs);
if (@errors) {
# Thar be errors here
foreach my $error (@errors) {
$exit_code = $error->{error_code};
foreach my $msg ($error->{errors}) {
$log->log(LOG_ERROR, "error(%d): %s",
$error->{error_code},
$msg
);
}
}
}
## Display results
if (check_bool($cfg{'vuser'}{'show result set'}) and @rs) {
$cfg{'vuser'}{'display format'} = $format if defined $format;
my $display = VUser::ResultSet::Display->new(\%cfg);
$display->display(@rs);
}
eval { $eh->cleanup(%cfg); };
exit $exit_code;
sub version
{
my $cfg = shift;
my $opts = shift;
my $rs = VUser::ResultSet->new();
$rs->add_meta(VUser::Meta->new('name' => 'extension',
'type' => 'string',
'description' => 'Extension name'));
$rs->add_meta(VUser::Meta->new('name' => 'version',
'type' => 'string',
'description' => 'Version number'));
$rs->add_data(['vuser', $VERSION]);
( run in 0.947 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )