Skip to content

Commit

Permalink
Accept an optional 'STUB' for GitHub identity files
Browse files Browse the repository at this point in the history
  • Loading branch information
timlegge committed Aug 25, 2023
1 parent c870c76 commit f6f9260
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/Config/Identity.pm
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ GitHub API:
my %identity = Config::Identity::GitHub->load_check;
print "login: $identity{login} token: $identity{token}\n";
or
# you can also pass a "stub" to the load_check to look for
# the identity information in ~/.project-identity or ~/.project
my %identity = Config::Identity::GitHub->load_check("project");
print "login: $identity{login} token: $identity{token}\n";
=head1 DESCRIPTION
Config::Identity is a tool for loading (and optionally decrypting via GnuPG) user/pass identity information
Expand Down
48 changes: 46 additions & 2 deletions lib/Config/Identity/GitHub.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ sub STUB { defined $_ and return $_ for $ENV{CI_GITHUB_STUB}, $STUB }

sub load {
my $self = shift;
return Config::Identity->try_best( $self->STUB );
my $stub = shift || $self->STUB;
return Config::Identity->try_best( $stub );
}

sub check {
Expand All @@ -27,10 +28,53 @@ sub check {

sub load_check {
my $self = shift;
my %identity = $self->load;
my $stub = shift;
my %identity = $self->load($stub);
$self->check( %identity );
return %identity;
}

=pod
=head1 SYNOPSIS
GitHub API:
use Config::Identity::GitHub;
# 1. Find either $HOME/.github-identity or $HOME/.github
# 2. Decrypt the found file (if necessary) read, and parse it
# 3. Throw an exception unless %identity has 'login' and 'token' defined
my %identity = Config::Identity::GitHub->load_check;
print "login: $identity{login} token: $identity{token}\n";
or
# you can also pass a "stub" to the load_check to look for
# the identity information in ~/.project-identity or ~/.project
my %identity = Config::Identity::GitHub->load_check("project");
print "login: $identity{login} token: $identity{token}\n";
=head2 METHODS
=over
=item load_check
Accepts an optional "STUB" to allow you to find a separate identity file.
The filename becomes:
~/.STUB-identity or ~/.STUB
If the option setting is not provided it defaults to github
~/.github-identity or ~/.github
=back
=cut

1;

8 changes: 7 additions & 1 deletion t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ _END_
{
local $Config::Identity::home = File::Spec->catfile(qw/ t assets github /);
my %identity = Config::Identity::GitHub->load_check;
cmp_deeply( \%identity, {qw/ login alice token hunter2 /} );
cmp_deeply( \%identity, {qw/ login alice token hunter2 /}, "github load default identity" );
}
{
local $Config::Identity::home = File::Spec->catfile(qw/ t assets github /);
my %identity = Config::Identity::GitHub->load_check("org");;
cmp_deeply( \%identity, {qw/ login notalice token bughunter /}, "github load specific identity" );
}


use Config::Identity::PAUSE;
{
Expand Down
Binary file added t/assets/github/.org-identity
Binary file not shown.

0 comments on commit f6f9260

Please sign in to comment.