| 1 | package OrePAN2::S3::Indexer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Cwd qw(getcwd); | ||
| 7 | use Log::Log4perl; | ||
| 8 | use Data::Dumper; | ||
| 9 | use JSON qw(decode_json); | ||
| 10 | use English qw(-no_match_vars); | ||
| 11 | |||
| 12 | use Role::Tiny::With; | ||
| 13 | |||
| 14 | with qw( | ||
| 15 | OrePAN2::S3::Role::Inject | ||
| 16 | OrePAN2::S3::Role::UploadArtifacts | ||
| 17 | ); | ||
| 18 | |||
| 19 | ######################################################################## | ||
| 20 | sub new { | ||
| 21 | ######################################################################## | ||
| 22 | my ( $class, %args ) = @_; | ||
| 23 | |||
| 24 | my $self = bless \%args, $class; | ||
| 25 | |||
| 26 | my $home = $ENV{HOME} ? "$ENV{HOME}/" : getcwd . q{/}; | ||
| 27 | |||
| 28 | my $config_file = $ENV{CONFIG} // q{}; # specify the absolute path | ||
| 29 | |||
| 30 | my @candidates = ( "${home}.orepan2-s3.json", '/var/task/orepan2-s3.json', 'orepan2-s3.json', $config_file ); | ||
| 31 | |||
| 32 | $self->get_logger->debug( Dumper( [ candidates => \@candidates ] ) ); | ||
| 33 | |||
| 34 | my ($config_path) = grep { $_ && -e $_ } @candidates; | ||
| 35 | |||
| 36 | die "ERROR: no config file found.\n" | ||
| 37 | if !$config_path; | ||
| 38 | |||
| 39 | my $profile_name = $ENV{OREPAN2_PROFILE_NAME} // 'default'; | ||
| 40 | |||
| 41 | eval { | ||
| 42 | local $RS = undef; | ||
| 43 | open my $fh, '<', $config_file | ||
| 44 | or die "ERROR: could not open $config_file for reading: $OS_ERROR\n"; | ||
| 45 | |||
| 46 | my $config = decode_json(<$fh>); | ||
| 47 | |||
| 48 | $self->{config} = $config->{$profile_name}; | ||
| 49 | |||
| 50 | close $fh; | ||
| 51 | }; | ||
| 52 | |||
| 53 | die $EVAL_ERROR | ||
| 54 | if $EVAL_ERROR; | ||
| 55 | |||
| 56 | return $self; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub get_logger { return $_[0]->{logger} // Log::Log4perl->get_logger } | ||
| 60 | sub get_author_path { return $_[0]->{author_path} } | ||
| 61 | sub get_s3 { return $_[0]->{s3} } | ||
| 62 | sub get_bucket_name { return $_[0]->{bucket_name} } | ||
| 63 | sub get_config { return $_[0]->{config} } | ||
| 64 | |||
| 65 | 1; |