Net::Amazon::Config


Net-Amazon-Config documentation Contained in the Net-Amazon-Config distribution.

Index


Code Index:


Net-Amazon-Config documentation Contained in the Net-Amazon-Config distribution.

# Copyright (c) 2010 by David Golden. All rights reserved.
# Licensed under Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License was distributed with this file or you may obtain a 
# copy of the License from http://www.apache.org/licenses/LICENSE-2.0

package Net::Amazon::Config;
use strict;
use warnings;

our $VERSION = '0.001';
$VERSION = eval $VERSION; ## no critic

use Carp ();
use Config::Tiny ();
use Net::Amazon::Config::Profile ();
use Params::Validate ();
use Path::Class ();
use Object::Tiny qw(
  config_dir
  config_file
  config_path
);

use constant IS_WIN32 => $^O eq 'MSWin32';

sub _default_dir {
  my $base = Path::Class::dir(IS_WIN32 ? $ENV{USERPROFILE} : $ENV{HOME});
  return $base->subdir('.amazon')->absolute->stringify;
}

sub new {
  my $class = shift;
  my %args = Params::Validate::validate( @_, {
    config_dir => {
      default => $ENV{NET_AMAZON_CONFIG_DIR} || _default_dir,
    },
    config_file => {
      default => $ENV{NET_AMAZON_CONFIG} || 'profiles.conf',
    }
  });

  if ( Path::Class::file($args{config_file})->is_absolute ) {
    $args{config_path} = $args{config_file};
  }
  else {
    $args{config_path} = 
      Path::Class::dir($args{config_dir})->file($args{config_file});
  }
  
  unless ( -r $args{config_path} ) {
    die "Could not find readable file $args{config_path}";
  }

  return bless \%args, $class;
}

sub get_profile {
  my ($self, $profile_name) = @_; 
  my $config = Config::Tiny->read( $self->config_path );
  
  $profile_name = $config->{_}{default} unless defined $profile_name;
  my $params = $config->{$profile_name}
    or return;

  $params->{profile_name} = $profile_name;
  my $profile = eval { Net::Amazon::Config::Profile->new( $params ) };
  if ($@) {
    Carp::croak "Invalid profile: $@";
  }
  return $profile;
}

1;

__END__