AnsibleModule

 view release on metacpan or  search on metacpan

lib/AnsibleModule.pm  view on Meta::CPAN

package AnsibleModule;

use Mojo::Base -base;

our $VERSION = '0.4';

=for comment

We want JSON
WANT_JSON

=cut

use Mojo::JSON qw/decode_json encode_json/;
use Mojo::File qw/path/;
use POSIX qw/locale_h/;
use Carp qw/croak/;


has argument_spec           => sub { +{} };
has bypass_checks           => sub {0};
has no_log                  => sub {0};
has check_invalid_arguments => sub {1};
has mutually_exclusive      => sub { [] };
has required_together       => sub { [] };
has required_one_of         => sub { [] };
has supports_check_mode     => sub {0};
has required_if             => sub { [] };
has aliases                 => sub { {} };


has params => sub {
  my $self = shift;
  return {} unless @ARGV;
  my $args = path($ARGV[0])->slurp;
  my $json = decode_json($args);
  return $json if defined $json;
  my $params = {};
  for my $arg (split $args) {
    my ($k, $v) = split '=', $arg;
    $self->fail_json(
      {msg => 'This module requires key=value style argument: ' . $arg})
      unless defined $v;
    $self->fail_json({msg => "Duplicate parameter: $k"})
      if exists $params->{$k};
    $params->{$k} = $v;
  }
  return $params;
};

has _legal_inputs => sub {
  {CHECKMODE => 1, NO_LOG => 1};
};

has check_mode => sub {0};

sub new {
  my $self = shift->SUPER::new(@_);
  setlocale(LC_ALL, "");
  $self->_check_argument_spec();
  $self->_check_params();
  unless ($self->bypass_checks) {
    $self->_check_arguments();
    $self->_check_required_together();
    $self->_check_required_one_of();
    $self->_check_required_if();
  }
  $self->_log_invocation() unless $self->no_log();
  $self->_set_cwd();
  return $self;
}

sub exit_json {
  my $self = shift;
  my $args = ref $_[0] ? $_[0] : {@_};
  $args->{changed} //= 0;
  print encode_json($args);
  exit 0;
}

sub fail_json {
  my $self = shift;
  my $args = ref $_[0] ? $_[0] : {@_};
  croak("Implementation error --  msg to explain the error is required")
    unless defined($args->{'msg'});
  $args->{failed} = 1;
  print encode_json($args);
  exit 1;
}

sub _check_argument_spec {
  my $self = shift;
  for my $arg (keys(%{$self->argument_spec})) {
    $self->_legal_inputs->{$arg}++;
    my $spec = $self->argument_spec->{$arg};

    # Check required
    $self->fail_json(msg =>
        "internal error: required and default are mutually exclusive for $arg")
      if defined $spec->{default} && $spec->{required};

    # Check aliases
    $spec->{aliases} //= [];
    $self->fail_json({msg => "internal error: aliases must be an arrayref"})
      unless ref $spec->{aliases} && ref $spec->{aliases} eq 'ARRAY';

    # Set up aliases
    for my $alias (@{$spec->{aliases}}) {
      $self->_legal_inputs->{$alias}++;
      $self->aliases->{$alias} = $arg;
      $self->params->{$arg}    = $self->params->{$alias}
        if exists $self->params->{$alias};
    }

    # Fallback to default value
    $self->params->{$arg} //= $spec->{default} if exists $spec->{default};
  }
}



( run in 1.023 second using v1.01-cache-2.11-cpan-f56aa216473 )