Skip to content

Commit

Permalink
allow db to also use a JSON file or CPANSA::DB, although cpan-audit d…
Browse files Browse the repository at this point in the history
…oesn't expose this yet
  • Loading branch information
briandfoy committed Aug 26, 2024
1 parent 8c70c83 commit 8aeaba6
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion lib/CPAN/Audit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sub new {

$self->_handle_exclude_file if $self->{exclude_file};

$self->{db} //= CPAN::Audit::DB->db;
$self->{db} //= $self->_get_db(%args);

$self->{filter} = CPAN::Audit::Filter->new( exclude => $args{exclude} );
$self->{query} = CPAN::Audit::Query->new( db => $self->{db} );
Expand All @@ -35,6 +35,36 @@ sub new {
return $self;
}

sub _get_db {
my( $self, %params ) = @_;

if ( $params{'json_db'} ) {
my $data = do {
local $/;
open my($fh), '<:raw', $params{'json_db'}
or die "could not read file <$params{json_db}>\n";
<$fh>;
};
state $rc = require JSON;

my $decoded = eval { JSON::decode_json($data) };
die "could not decode JSON from <$params{json_db}>: @_\n" unless defined $decoded;
return $decoded;
}

my $rc = eval { require CPANSA::DB };
if ( $rc ) {
return CPANSA::DB->db;
}

$rc = eval { require CPAN::Audit::DB };
if ( $rc ) {
return CPAN::Audit::DB->db;
}

die "could not find a CPANSA database in CPANSA::DB or CPAN::Audit::DB\n";
}

sub _handle_exclude_file {
my( $self ) = @_;

Expand Down

0 comments on commit 8aeaba6

Please sign in to comment.