App-lcpan

 view release on metacpan or  search on metacpan

lib/App/lcpan/Cmd/copy_script.pm  view on Meta::CPAN

use strict;
use warnings;

require App::lcpan;

use Perinci::Object;

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2023-09-26'; # DATE
our $DIST = 'App-lcpan'; # DIST
our $VERSION = '1.074'; # VERSION

our %SPEC;

$SPEC{'handle_cmd'} = {
    v => 1.1,
    summary => "Copy a script's latest release file to current directory",
    args => {
        %App::lcpan::common_args,
        %App::lcpan::script_args,
        %App::lcpan::overwrite_args,
        %App::lcpan::all_args,
    },
    tags => ['write-to-fs'],
};
sub handle_cmd {
    require File::Copy;

    my %args = @_;

    my $state = App::lcpan::_init(\%args, 'ro');
    my $dbh = $state->{dbh};

    my $script = $args{script};

    my $sth = $dbh->prepare("SELECT
  script.name script,
  file.cpanid author,
  file.name release
FROM script
LEFT JOIN file ON script.file_id=file.id
LEFT JOIN module ON file.id=module.file_id
WHERE script.name=?
GROUP BY file.id
ORDER BY module.version_numified DESC");

    $sth->execute($script);

    my @srcpaths;
    my @targetpaths;
    my %mem;
    while (my $row = $sth->fetchrow_hashref) {
        unless ($args{all}) {
            next if $mem{$row->{script}}++;
        }
        push @srcpaths, App::lcpan::_fullpath(
            $row->{release}, $state->{cpan}, $row->{author});
        push @targetpaths, $row->{release};
    }

    return [404, "No release for script '$script'"] unless @srcpaths;

    my $envres = envresmulti();
    for my $i (0..$#srcpaths) {
        my $srcpath = $srcpaths[$i];
        my $targetpath = $targetpaths[$i];
        (-f $srcpath) or do {
            $envres->add_result(
                404, "File not found: $srcpath",
                {item_id => $srcpath},
            );
            next;
        };
        if ((-f $targetpath) && !$args{overwrite}) {
            $envres->add_result(
                412, "Refusing to overwrite existing file '$targetpath'",
                {item_id => $srcpath},
            );
            next;
        }
        File::Copy::syscopy($srcpath, $targetpath) or do {
            $envres->add_result(
                500, "Can't copy '$srcpath' to '$targetpath': $!",
                {item_id => $srcpath},
            );
            next;
        };
        $envres->add_result(
            200, "OK",
            {item_id => $srcpath},
        );
    }
    $envres->as_struct;
}

1;
# ABSTRACT: Copy a script's latest release file to current directory

__END__

=pod

=encoding UTF-8

=head1 NAME

App::lcpan::Cmd::copy_script - Copy a script's latest release file to current directory

=head1 VERSION

This document describes version 1.074 of App::lcpan::Cmd::copy_script (from Perl distribution App-lcpan), released on 2023-09-26.

=head1 FUNCTIONS


=head2 handle_cmd

Usage:

 handle_cmd(%args) -> [$status_code, $reason, $payload, \%result_meta]

Copy a script's latest release file to current directory.

This function is not exported.

Arguments ('*' denotes required arguments):

=over 4



( run in 0.624 second using v1.01-cache-2.11-cpan-39bf76dae61 )