App-FargateStack
view release on metacpan or search on metacpan
lib/App/FargateStack/Builder/Utils.pm view on Meta::CPAN
my ( $self, $api, %options ) = @_;
my $class = $self->get( lc $api );
return $class
if $class && !keys %options;
if ( !$class ) {
my $normalized_name = $api;
$normalized_name =~ s/[\-]//xsm;
my $class_path = sprintf 'App/%s.pm', $normalized_name;
require $class_path;
my $class_name = sprintf 'App::%s', $normalized_name;
$self->set( lc $api, $class_name->new( _service_name => lc $api, %{ $self->get_global_options }, %options ) );
$class = $self->get( lc $api );
}
foreach ( keys %options ) {
next if !defined $options{$_};
$class->set( $_, $options{$_} );
}
return $class;
}
########################################################################
sub maybe_color {
########################################################################
my ( $self, $color, $text ) = @_;
return $text
if !$self->get_color;
return colored( $text, $color );
}
########################################################################
sub create_default {
########################################################################
my ( $self, $what, @args ) = @_;
require App::FargateStack::Constants;
croak "no default for $what\n"
if !exists $App::FargateStack::Constants::DEFAULT_NAMES{$what};
my $default = $App::FargateStack::Constants::DEFAULT_NAMES{$what};
return ref $default ? $default->( $self, @args ) : $default;
}
########################################################################
sub confirm {
########################################################################
my ( $prompt, @args ) = @_;
print sprintf "$prompt [y/N] ", @args;
chomp( my $answer = <STDIN> );
return $answer =~ /^y(es)?$/xsmi;
}
########################################################################
sub common_args {
########################################################################
my ( $self, @args ) = @_;
my $config = $self->get_config;
my $var_pool = {
config => $config,
cache => $self->get_cache,
dryrun => $self->get_dryrun,
# these just avoid a bunch of hash digging
tasks => $config->{tasks},
cluster => $config->{cluster},
security_groups => $config->{security_groups},
route53 => $config->{route53},
alb => $config->{alb},
app => $config->{app},
subnets => $config->{subnets},
role => $config->{role},
events_role => $config->{events_role},
};
$self->set__var_pool($var_pool);
return $var_pool
if !@args;
my @invalid_args = grep { !exists $var_pool->{$_} } @args;
croak sprintf "invalid argument(s): %s\n", join q{,}, @invalid_args
if @invalid_args;
return @{$var_pool}{@args};
}
########################################################################
sub display_diffs {
########################################################################
my ( $self, $old, $new, $options ) = @_;
my $json = JSON->new->canonical->pretty->allow_blessed->convert_blessed;
my $old_str = $json->encode($old);
my $new_str = $json->encode($new);
# normalize line endings + ensure trailing newline (helps diff output)
for my $str ( $old_str, $new_str ) {
$str =~ s/\r\n/\n/xsmg;
next if $str =~ /\n\z/xsm;
$str .= "\n";
}
my $style = ( $options && ref $options ) ? ( $options->{style} // 'Table' ) : 'Table';
( run in 1.107 second using v1.01-cache-2.11-cpan-6aa56a78535 )