| 1 | package OrePAN2::S3::Role::Upload; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use CLI::Simple::Utils qw(slurp); | ||
| 8 | use English qw(-no_match_vars); | ||
| 9 | use File::Basename qw(basename); | ||
| 10 | |||
| 11 | use Role::Tiny; | ||
| 12 | |||
| 13 | ######################################################################## | ||
| 14 | sub cmd_upload { # alias add | ||
| 15 | ######################################################################## | ||
| 16 | my ( $self, $distribution ) = @_; | ||
| 17 | |||
| 18 | my ($file) = $self->get_args; | ||
| 19 | $file //= $self->get_distribution; | ||
| 20 | |||
| 21 | die "ERROR: no file specified or not found\n" | ||
| 22 | if !$file || !-f $file; | ||
| 23 | |||
| 24 | die "ERROR: not a tar ball\n" | ||
| 25 | if $file !~ /[.]tar[.]gz$/xsm; | ||
| 26 | |||
| 27 | $self->set_distribution($file); | ||
| 28 | |||
| 29 | my $dirty = $self->_check_dirty($file); | ||
| 30 | |||
| 31 | if ($dirty) { | ||
| 32 | if ( !$self->get_force ) { | ||
| 33 | print {*STDOUT} "ERROR: You're trying to upload an uncommitted distribution. Use --force to force upload\n"; | ||
| 34 | return $FAILURE; | ||
| 35 | } | ||
| 36 | |||
| 37 | print {*STDOUT} "WARNING: You're uploading an uncommitted distribution - $dirty\n"; | ||
| 38 | } | ||
| 39 | |||
| 40 | $self->_upload($file); | ||
| 41 | |||
| 42 | return $SUCCESS; | ||
| 43 | } | ||
| 44 | |||
| 45 | ######################################################################## | ||
| 46 | sub _check_dirty { | ||
| 47 | ######################################################################## | ||
| 48 | my ( $self, $file ) = @_; | ||
| 49 | |||
| 50 | return | ||
| 51 | if !$self->get_dirty_check; | ||
| 52 | |||
| 53 | require Archive::Tar; | ||
| 54 | |||
| 55 | my $tar = Archive::Tar->new; | ||
| 56 | $tar->read($file); | ||
| 57 | |||
| 58 | my $name = $file; | ||
| 59 | $name =~ s/[-]\d.*$//xsm; | ||
| 60 | $name =~ s/[-]/\//xsmg; | ||
| 61 | |||
| 62 | my $content = eval { $tar->get_content( sprintf '%s/lib/%s.pm', basename( $file, '.tar.gz' ), $name ); }; | ||
| 63 | |||
| 64 | return | ||
| 65 | if !$content; | ||
| 66 | |||
| 67 | my ($dirty) = $content =~ /GIT_DIRTY\s*=\s['"](.*?)['"];\n/xsm; | ||
| 68 | |||
| 69 | return | ||
| 70 | if !$dirty || $dirty !~ /dirty/xsm; | ||
| 71 | |||
| 72 | return $dirty; | ||
| 73 | } | ||
| 74 | |||
| 75 | ######################################################################## | ||
| 76 | sub _upload { | ||
| 77 | ######################################################################## | ||
| 78 | my ( $self, $file ) = @_; | ||
| 79 | |||
| 80 | my $config = $self->get_config; | ||
| 81 | my $prefix = $config->{AWS}{prefix}; | ||
| 82 | my $base = basename($file); | ||
| 83 | my $tarball_key = sprintf '%s/authors/id/%s/%s', $prefix, $self->get_author_path, $base; | ||
| 84 | |||
| 85 | $self->get_s3->put_object( $self->get_bucket_name, $tarball_key, slurp($file) ); | ||
| 86 | $self->get_logger->info( sprintf 'uploaded %s => %s', $file, $tarball_key ); | ||
| 87 | |||
| 88 | return $TRUE; | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ |