| 1 | package App::SFDC::Role::Credentials; | ||
| 2 | # ABSTRACT: Handle credential command-line input | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use 5.10.0; | ||
| 7 | |||
| 8 | our $VERSION = '0.14'; # TRIAL VERSION | ||
| 9 | |||
| 10 | use Config::Properties; | ||
| 11 | use Data::Dumper; | ||
| 12 | use File::HomeDir; | ||
| 13 | use Log::Log4perl ':easy'; | ||
| 14 | use WWW::SFDC '0.33'; | ||
| 15 | |||
| 16 | use Moo::Role; | ||
| 17 | use MooX::Options; | ||
| 18 | |||
| 19 | |||
| 20 | option 'username', | ||
| 21 | is => 'rw', | ||
| 22 | short => 'u', | ||
| 23 | format => 's'; | ||
| 24 | |||
| 25 | |||
| 26 | option 'password', | ||
| 27 | is => 'rw', | ||
| 28 | short => 'p', | ||
| 29 | format => 's'; | ||
| 30 | |||
| 31 | |||
| 32 | option 'url', | ||
| 33 | is => 'rw', | ||
| 34 | format => 's', | ||
| 35 | default => 'https://login.salesforce.com'; | ||
| 36 | |||
| 37 | |||
| 38 | option 'apiversion', | ||
| 39 | is => 'rw', | ||
| 40 | format => 'i', | ||
| 41 | default => 34; | ||
| 42 | |||
| 43 | |||
| 44 | option 'credfile', | ||
| 45 | doc => 'The file from which to read credentials.', | ||
| 46 | is => 'ro', | ||
| 47 | format => 's', | ||
| 48 | lazy => 1, | ||
| 49 | default => File::HomeDir->my_home."/.salesforce.properties", | ||
| 50 | isa => sub { | ||
| 51 | LOGDIE "The credentials file ".$_[0]." doesn't exist!" | ||
| 52 | unless -e $_[0]; | ||
| 53 | }; | ||
| 54 | |||
| 55 | |||
| 56 | option 'environment', | ||
| 57 | is => 'ro', | ||
| 58 | short => 'e', | ||
| 59 | format => 's'; | ||
| 60 | |||
| 61 | has '_session', | ||
| 62 | is => 'ro', | ||
| 63 | lazy => 1, | ||
| 64 | default => sub { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | $self->_readOptionsFromFile if $self->environment | ||
| 68 | and (!$self->username or !$self->password); | ||
| 69 | |||
| 70 | WWW::SFDC->new( | ||
| 71 | username => $self->username, | ||
| 72 | password => $self->password, | ||
| 73 | url => $self->url, | ||
| 74 | apiVersion => $self->apiversion, | ||
| 75 | ) | ||
| 76 | }; | ||
| 77 | |||
| 78 | sub _readOptionsFromFile { | ||
| 79 | my ($self) = @_; | ||
| 80 | |||
| 81 | my $environment = $self->environment; | ||
| 82 | |||
| 83 | INFO "Reading options for $environment from " | ||
| 84 | . $self->credfile; | ||
| 85 | |||
| 86 | my %environments = %{ | ||
| 87 | Config::Properties | ||
| 88 | ->new(file => $self->credfile) | ||
| 89 | ->splitToTree() | ||
| 90 | }; | ||
| 91 | |||
| 92 | LOGDIE "Couldn't find credentials for $environment in " | ||
| 93 | .$self->credfile | ||
| 94 | unless $environments{$environment}; | ||
| 95 | |||
| 96 | for (qw'username password url apiversion'){ | ||
| 97 | if (exists($environments{$environment}->{$_})){ | ||
| 98 | DEBUG "Setting $_ with ".$environments{$environment}->{$_}."."; | ||
| 99 | $self->$_($environments{$environment}->{$_}); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||
| 106 | |||
| 107 | __END__ |