AWS-CLI-Config
view release on metacpan or search on metacpan
specify the profile as the first argument.
## credentials (Str)
Fetches information from the credential file if it exists. You can optionally
specify the profile as the first argument.
## config (Str)
Fetches information from the config file if it exists. If you need to override
the default path of this file, use the `$ENV{AWS_CONFIG_FILE}` variable.
You can optionally specify the profile as the first argument.
## Automatic accessors
Accessors will also be automatically generated for all top-level keys in a given
profile the first time they are called. They will be cached, so that you only
pay this cost if you ask for it, and only do so once.
The accessors will have the same name as the keys they represent.
lib/AWS/CLI/Config.pm view on Meta::CPAN
use 5.008001;
use strict;
use warnings;
use Carp ();
use File::Spec;
use autodie;
our $VERSION = '0.05';
my $DEFAULT_PROFILE = 'default';
my $CREDENTIALS;
my %CREDENTIALS_PROFILE_OF;
my $CONFIG;
my %CONFIG_PROFILE_OF;
BEGIN: {
my %attributes = (
access_key_id => {
env => 'AWS_ACCESS_KEY_ID',
lib/AWS/CLI/Config.pm view on Meta::CPAN
my %opt = @_;
my $env_var = $opt{env};
my $profile_key = $opt{key} || $attr;
return sub {
if ($env_var && exists $ENV{$env_var} && $ENV{$env_var}) {
return $ENV{$env_var};
}
my $profile = shift || _default_profile();
my $credentials = credentials($profile);
if ($credentials && $credentials->$profile_key) {
return $credentials->$profile_key;
}
my $config = config($profile);
if ($config && $config->$profile_key) {
return $config->$profile_key;
}
return undef;
};
}
sub credentials {
my $profile = shift || _default_profile();
$CREDENTIALS ||= _parse(
(exists $ENV{AWS_CONFIG_FILE} and $ENV{AWS_CONFIG_FILE})
? $ENV{AWS_CONFIG_FILE}
: File::Spec->catfile(_default_dir(), 'credentials')
);
return unless (exists $CREDENTIALS->{$profile});
$CREDENTIALS_PROFILE_OF{$profile} ||=
AWS::CLI::Config::Profile->new($CREDENTIALS->{$profile});
return $CREDENTIALS_PROFILE_OF{$profile};
}
sub config {
my $profile = shift || _default_profile();
$CONFIG ||= _parse(
(exists $ENV{AWS_CONFIG_FILE} and $ENV{AWS_CONFIG_FILE})
? $ENV{AWS_CONFIG_FILE}
: File::Spec->catfile(_default_dir(), 'config')
);
return unless (exists $CONFIG->{$profile});
$CONFIG_PROFILE_OF{$profile} ||=
AWS::CLI::Config::Profile->new($CONFIG->{$profile});
return $CONFIG_PROFILE_OF{$profile};
}
sub _base_dir {
($^O eq 'MSWin32') ? $ENV{USERPROFILE} : $ENV{HOME};
}
sub _default_dir {
File::Spec->catdir(_base_dir(), '.aws');
}
sub _default_profile {
(exists $ENV{AWS_DEFAULT_PROFILE} && $ENV{AWS_DEFAULT_PROFILE})
? $ENV{AWS_DEFAULT_PROFILE}
: $DEFAULT_PROFILE;
}
# This only supports one level of nesting, but it seems AWS config files
# themselves only have but one level
sub _parse {
my $file = shift;
my $profile = shift || _default_profile();
my $hash = {};
my $nested = {};
return +{} unless -r $file;
my $contents;
{
local $/ = undef;
open my $fh, '<', $file;
lib/AWS/CLI/Config.pm view on Meta::CPAN
specify the profile as the first argument.
=head2 credentials (Str)
Fetches information from the credential file if it exists. You can optionally
specify the profile as the first argument.
=head2 config (Str)
Fetches information from the config file if it exists. If you need to override
the default path of this file, use the C<$ENV{AWS_CONFIG_FILE}> variable.
You can optionally specify the profile as the first argument.
=head2 Automatic accessors
Accessors will also be automatically generated for all top-level keys in a given
profile the first time they are called. They will be cached, so that you only
pay this cost if you ask for it, and only do so once.
The accessors will have the same name as the keys they represent.
t/04_config.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use File::Temp 'tempfile';
use AWS::CLI::Config;
my ($fh, $file) = tempfile(UNLINK => 1);
my $default_access_key_id = 'Me';
my $default_secret_access_key = '__secret__';
my $tester_access_key_id = "Tester$default_access_key_id";
my $tester_secret_access_key = "__tester$default_secret_access_key";
print $fh <<"EOS";
[default]
aws_access_key_id = $default_access_key_id
aws_secret_access_key = $default_secret_access_key
[profile tester]
aws_access_key_id = $tester_access_key_id
aws_secret_access_key = $tester_secret_access_key
s3 =
addressing_style = path
EOS
close $fh;
local $ENV{AWS_CONFIG_FILE} = $file;
subtest 'Default profile' => sub {
my $config = AWS::CLI::Config::config;
is($config->aws_access_key_id, $default_access_key_id, 'access_key_id');
is($config->aws_secret_access_key, $default_secret_access_key, 'secret_access_key');
};
subtest 'Specific profile' => sub {
my $config = AWS::CLI::Config::config('tester');
is($config->aws_access_key_id, $tester_access_key_id, 'access_key_id');
is($config->aws_secret_access_key, $tester_secret_access_key, 'secret_access_key');
is($config->s3->{addressing_style}, 'path', 'nested value');
};
( run in 0.431 second using v1.01-cache-2.11-cpan-0a6323c29d9 )