CASCM-Wrapper

 view release on metacpan or  search on metacpan

lib/CASCM/Wrapper.pm  view on Meta::CPAN

#######################
# MODULE METHODS
#######################

# Constructor
sub new {
    my $class = shift;
    my $options_ref = shift || {};

    my $self = {};
    bless $self, $class;
  return $self->_init($options_ref);
} ## end sub new

# Set Context
sub set_context {
    my $self = shift;
    my $context = shift || {};

    if ( ref $context ne 'HASH' ) {
        $self->_err("Context must be a hash reference");
      return;
    } ## end if ( ref $context ne 'HASH')

    $self->{_context} = $context;
  return 1;
} ## end sub set_context

# load context
sub load_context {
    my $self = shift;
    my $file
      = shift || ( $self->_err("File required but missing") and return );

    if ( not -f $file ) { $self->_err("File $file does not exist"); return; }

    eval {
        require Config::Tiny;
        Config::Tiny->import();
      return 1;
    } or do {
        $self->_err(
            "Please install Config::Tiny if you'd like to load context files"
        );
      return;
    };

    my $config = Config::Tiny->read($file)
      or do { $self->_err("Error reading $file") and return; };

    my $context = {};
    foreach ( keys %{$config} ) {
        if   ( $_ eq '_' ) { $context->{global} = $config->{$_}; }
        else               { $context->{$_}     = $config->{$_}; }
    } ## end foreach ( keys %{$config} )

  return $self->set_context($context);
} ## end sub load_context

# Update Context
sub update_context {
    my $self = shift;
    my $new = shift || {};

    if ( ref $new ne 'HASH' ) {
        $self->_err("Context must be a hash reference");
      return;
    } ## end if ( ref $new ne 'HASH')

    my $context = $self->get_context();

    foreach my $type ( keys %{$new} ) {
        foreach my $key ( keys %{ $new->{$type} } ) {
            $context->{$type}->{$key} = $new->{$type}->{$key};
        }
    } ## end foreach my $type ( keys %{$new...})

  return $self->set_context($context);
} ## end sub update_context

# Parse logs
sub parse_logs {
    my $self = shift;
    if (@_) {
        $self->{_options}->{parse_logs} = shift;
        if ( $self->{_options}->{parse_logs} ) {
            eval {
                require Log::Any;
              return 1;
            }
              or croak
              "Error loading Log::Any. Please install it if you'd like to parse logs";
        } ## end if ( $self->{_options}...)
    } ## end if (@_)
  return $self->{_options}->{parse_logs};
} ## end sub parse_logs

# Dry Run
sub dry_run {
    my $self = shift;
    if (@_) { $self->{_options}->{dry_run} = shift; }
  return $self->{_options}->{dry_run};
} ## end sub dry_run

# Get context
sub get_context {
    my ( $self, $cmd ) = @_;
    my $context = {};
    if ($cmd) {
        $context = {

            # Global
            $self->{_context}->{global}
            ? %{ $self->{_context}->{global} }
            : (),

            # Command specific
            $self->{_context}->{$cmd} ? %{ $self->{_context}->{$cmd} } : (),
        };
    } ## end if ($cmd)
    else {
        $context = $self->{_context};
    }

  return $context;
} ## end sub get_context

# Get error message
sub errstr { return shift->{_errstr}; }

# Get return code
sub exitval { return shift->{_exitval}; }

# Make argument string
sub make_arg_str {
    my ( $self, @args ) = @_;
    my @quoted;
    foreach my $arg (@args) {
      next unless defined $arg;



( run in 1.336 second using v1.01-cache-2.11-cpan-13bb782fe5a )