Config-AWS
view release on metacpan or search on metacpan
* Fixed a test that was broken because of improper use of chomp (JRaspass++)
* Reduced the number of methods in our namespace
0.09 2021-08-04 21:10:32+01:00 Europe/London
No changes from 0.080001.
0.080001 2021-07-16 19:24:33+01:00 Europe/London (TRIAL RELEASE)
Fixes:
* Fix tests in environemnts that have a credentials file
0.07 2019-09-14 13:34:43+01:00 Europe/London
Changes:
* Guard against undefined values in read_string
Fixes:
* Fix an issue with reporting File::Share as a dependency.
0.06 2019-09-13 22:24:53+01:00 Europe/London
use Config::AWS ':all';
# Read the data for a specific profile
$config = read( $source, $profile );
# Or read the default profile from the default file
$config = read();
# Which is the same as
$config = read(
-r credentials_file() ? credentials_file() : config_file(),
default_profile()
);
# Read all of the profiles from a file
$profiles = read_all( $source );
# Or if you have cycles to burn
$profiles = {
map { $_ => read( $source, $_ ) } list_profiles( $source )
};
Parse AWS config data. All these functions take the data source to use as
their first argument. The source can be any of the following:
- A **string** with the path to the file
- A **Path::Tiny object** for the config file
- An **array reference** of lines to parse
- A **scalar reference** with the entire slurped contents of the file
- An **undefined** value
If the source is undefined, a default file name will be used. This will be
the result of calling **credentials\_file** (if it is a readable file) or the
result of calling **config\_file** otherwise.
**read\_all** will return the results of parsing all of the content in the
source, for all profiles that may be defined in it.
**read** will instead return the data _for a single profile only_. This
profile can be specified as the second argument. If no profile is provided,
**read** will use the result of calling **default\_profile** as the default.
**list\_profiles** will return only the names of the profiles specified in the
- **default\_profile**
Returns the contents of the `AWS_DEFAULT_PROFILE` environment variable, or
`default` if undefined.
- **config\_file**
Returns the contents of the `AWS_CONFIG_FILE` environment variable, or
`~/.aws/config` if undefined.
- **credentials\_file**
Returns the contents of the `AWS_SHARED_CREDENTIALS_FILE` environment
variable, or `~/.aws/credentials` if undefined.
## Compatibility with Config::INI
This module includes routines that allow it to be used as a drop-in
replacement of [Config::INI](https://metacpan.org/pod/Config%3A%3AINI). The **read\_file**, **read\_string**, and
**read\_handle** functions behave like those described in the documentation
for that distribution. They can be imported with the `:ini` tag.
Unlike the functions described above, they do not use the default values
for AWS config files or profiles, and require the source to be explicitly
lib/Config/AWS.pm view on Meta::CPAN
use Ref::Util;
use Scalar::Util;
use Exporter::Shiny qw(
read
read_all
list_profiles
read_file
read_string
read_handle
config_file
credentials_file
default_profile
);
our $VERSION = '0.12';
our %EXPORT_TAGS = (
ini => [qw( read_file read_string read_handle )],
aws => [qw( config_file default_profile credentials_file )],
read => [qw( read read_all list_profiles )],
all => [qw( :ini :aws :read )],
);
# Internal methods for parsing and validation
my $prepare = sub {
# Input is given argument or credentials file (if exists) or config file.
my $input = shift // do {
my $cred_file = credentials_file();
-r $cred_file ? $cred_file : config_file();
};
unless ( Ref::Util::is_ref $input ) {
require Path::Tiny;
my @lines = eval { Path::Tiny::path( $input )->lines };
if ($@) {
Carp::croak "Cannot read from $input: $@->{err}"
if ref $@ && $@->isa('Path::Tiny::Error');
Carp::croak $@;
lib/Config/AWS.pm view on Meta::CPAN
sub list_profiles {
map /^\[(?:profile )?([\w\/.@%:_-]+)\]/, @{ &$prepare };
}
# AWS information methods
sub default_profile {
$ENV{AWS_DEFAULT_PROFILE} // 'default';
}
sub credentials_file {
$ENV{AWS_SHARED_CREDENTIALS_FILE} // ( glob '~/.aws/credentials' )[0];
}
sub config_file {
$ENV{AWS_CONFIG_FILE} // ( glob '~/.aws/config' )[0];
}
# Methods for compatibility with Config::INI interface
sub read_file {
Carp::croak 'Filename is missing' unless @_;
lib/Config/AWS.pm view on Meta::CPAN
use Config::AWS ':all';
# Read the data for a specific profile
$config = read( $source, $profile );
# Or read the default profile from the default file
$config = read();
# Which is the same as
$config = read(
-r credentials_file() ? credentials_file() : config_file(),
default_profile()
);
# Read all of the profiles from a file
$profiles = read_all( $source );
# Or if you have cycles to burn
$profiles = {
map { $_ => read( $source, $_ ) } list_profiles( $source )
};
lib/Config/AWS.pm view on Meta::CPAN
=item * An B<array reference> of lines to parse
=item * A B<scalar reference> with the entire slurped contents of the file
=item * An B<undefined> value
=back
If the source is undefined, a default file name will be used. This will be
the result of calling B<credentials_file> (if it is a readable file) or the
result of calling B<config_file> otherwise.
B<read_all> will return the results of parsing all of the content in the
source, for all profiles that may be defined in it.
B<read> will instead return the data I<for a single profile only>. This
profile can be specified as the second argument. If no profile is provided,
B<read> will use the result of calling B<default_profile> as the default.
B<list_profiles> will return only the names of the profiles specified in the
lib/Config/AWS.pm view on Meta::CPAN
=item B<default_profile>
Returns the contents of the C<AWS_DEFAULT_PROFILE> environment variable, or
C<default> if undefined.
=item B<config_file>
Returns the contents of the C<AWS_CONFIG_FILE> environment variable, or
C<~/.aws/config> if undefined.
=item B<credentials_file>
Returns the contents of the C<AWS_SHARED_CREDENTIALS_FILE> environment
variable, or C<~/.aws/credentials> if undefined.
=back
=head2 Compatibility with Config::INI
=for Pod::Coverage read_file read_string read_handle
This module includes routines that allow it to be used as a drop-in
replacement of L<Config::INI>. The B<read_file>, B<read_string>, and
B<read_handle> functions behave like those described in the documentation
t/environment.t view on Meta::CPAN
local $ENV{HOMEDIR} = 'tester';
tests 'default_profile' => sub {
local $ENV{AWS_DEFAULT_PROFILE} = 'some-profile';
is Config::AWS::default_profile(), 'some-profile';
delete $ENV{AWS_DEFAULT_PROFILE};
is Config::AWS::default_profile(), 'default';
};
tests 'credentials_file' => sub {
local $ENV{AWS_SHARED_CREDENTIALS_FILE} = 'some-credentials';
is Config::AWS::credentials_file(), 'some-credentials';
delete $ENV{AWS_SHARED_CREDENTIALS_FILE};
is path( Config::AWS::credentials_file )
->relative( path('~/.aws/credentials') )->stringify, '.';
unlike Config::AWS::credentials_file, qr/^~/, 'tilde is expanded';
};
tests 'config_file' => sub {
local $ENV{AWS_CONFIG_FILE} = 'some-config';
is Config::AWS::config_file(), 'some-config';
delete $ENV{AWS_CONFIG_FILE};
is path( Config::AWS::config_file )
->relative( path('~/.aws/config') )->stringify, '.';
use Test2::V0;
use Test2::Tools::Spec;
describe 'Import tags' => sub {
my (%tags, $tag);
our %functions;
before_all 'Prepare tags' => sub {
%tags = (
ini => [qw( read_file read_string read_handle )],
aws => [qw( config_file default_profile credentials_file )],
read => [qw( read read_all list_profiles )],
);
$tags{all} = [ map { @{$_} } values %tags ];
%functions = map { $_ => 1 } @{$tags{all}};
};
case 'None' => sub { $tag = '' };
case ':ini' => sub { $tag = 'ini' };
case ':aws' => sub { $tag = 'aws' };
case ':read' => sub { $tag = 'read' };
t/list_profiles.t view on Meta::CPAN
use Test2::V0;
use Test2::Tools::Spec;
use Config::AWS;
use Path::Tiny qw( path tempdir );
my $DIR = tempdir 'ConfigAWS-XXXXXXXXX';
$DIR->child('config/.aws')->mkpath;
$DIR->child('credentials/.aws')->mkpath;
$DIR->child('config/.aws/config')->touch->spew(<<'CONFIG');
[alternate]
key = value
[default]
key = value
[bad]
CONFIG
$DIR->child('credentials/.aws/credentials')->touch->spew(<<'CREDENTIALS');
[profile default]
key = value
[profile with-hyphens]
key = value
CREDENTIALS
describe 'Config::AWS list_profiles tests' => sub {
it 'Dies with unparseable arguments' => sub {
like dies { Config::AWS::list_profiles( {} ) },
qr/could not use .* as source/i,
t/list_profiles.t view on Meta::CPAN
qr/cannot read from objects of type/i,
'Object which is not a Path::Tiny';
};
describe 'List config profiles' => sub {
my ($input, $result);
after_case 'Clear input' => sub { undef $input };
case 'Credentials from ENV' => sub {
$ENV{AWS_SHARED_CREDENTIALS_FILE} = $DIR->child('credentials/.aws/credentials');
$ENV{AWS_CONFIG_FILE} = {}; # Not a file that exists
undef $input;
$result = [qw( default with-hyphens )];
};
case 'Config from ENV' => sub {
$ENV{AWS_SHARED_CREDENTIALS_FILE} = {}; # Not a file that exists
$ENV{AWS_CONFIG_FILE} = $DIR->child('config/.aws/config');
undef $input;
$result = [qw( alternate bad default )];
};
case 'Credentials as argument' => sub {
$input = $DIR->child('credentials/.aws/credentials');
$result = [qw( default with-hyphens )];
};
case 'Config as argument' => sub {
$input = $DIR->child('config/.aws/config')->stringify;
$result = [qw( alternate bad default )];
};
case 'Slurped contents' => sub {
my $string = $DIR->child('config/.aws/config')->slurp;
t/namespace.t view on Meta::CPAN
BEGIN
EXPORT
EXPORT_OK
EXPORT_TAGS
ISA
VERSION
__ANON__
_exporter_permitted_regexp
_exporter_validate_opts
config_file
credentials_file
default_profile
import
list_profiles
read
read_all
read_file
read_handle
read_string
)] => 'No unexpected methods in namespace';
use Test2::V0;
use Test2::Tools::Spec;
use Config::AWS;
use Path::Tiny qw( path tempdir );
use Ref::Util qw( is_globref );
my $DIR = tempdir 'ConfigAWS-XXXXXXXXX';
$DIR->child('config/.aws')->mkpath;
$DIR->child('credentials/.aws')->mkpath;
$DIR->child('config/.aws/config')->touch->spew(<<'CONFIG');
[alternate]
root_key = alternate root value
parent =
child = alternate value for child
another_root_key = alternate another root value
another_parent =
another_child = alternate value for another child
[default]
[bad]
= ignored
empty_value =
parent =
child = bad child
false_parent = 0
child = ignored
trailing_empty =
CONFIG
$DIR->child('credentials/.aws/credentials')->touch->spew(<<'CREDENTIALS');
[profile default]
root_key = credentials root value
another_root_key = another credentials root value
parent =
child = value for credentials child
[profile with-hyphens]
key = key-with-hyphens
CREDENTIALS
my %PROFILES = (
default => {
root_key => 'root value',
another_root_key => 'another root value',
parent => {
child => 'value for child',
};
};
it 'Reads file from ENV' => sub {
local $ENV{AWS_CONFIG_FILE} = $DIR->child('config/.aws/config')->stringify;
like Config::AWS::read( undef, 'default' ),
{ parent => { child => 'value for child' } },
'Read default config file from ENV';
local $ENV{AWS_SHARED_CREDENTIALS_FILE}
= $DIR->child('credentials/.aws/credentials')->stringify;
like Config::AWS::read( undef, 'default' ),
{ parent => { child => 'value for credentials child' } },
'Read default credentials file from ENV';
};
it 'Reads profile with hyphens' => sub {
my $file = $DIR->child('credentials/.aws/credentials');
like Config::AWS::read( $file, 'with-hyphens' ),
{ key => 'key-with-hyphens' },
'Read default credentials file from ENV';
};
it 'Does not die on bad config data' => sub {
is Config::AWS::read( $DIR->child('config/.aws/config'), 'bad' ),
$PROFILES{bad},
'Reads a bad profile';
};
it 'Reads all profiles' => sub {
is Config::AWS::read_all( $DIR->child('config/.aws/config') ),
( run in 0.288 second using v1.01-cache-2.11-cpan-4d50c553e7e )