App-lcpan-CmdBundle-gh

 view release on metacpan or  search on metacpan

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

package App::lcpan::Cmd::gh_clone;

use 5.010001;
use strict;
use warnings;
use Log::ger;

require App::lcpan;
require App::lcpan::Cmd::dist_meta;

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2022-03-27'; # DATE
our $DIST = 'App-lcpan-CmdBundle-gh'; # DIST
our $VERSION = '0.005'; # VERSION

our %SPEC;

$SPEC{handle_cmd} = {
    v => 1.1,
    summary => 'Clone github repo of a module/dist',
    args => {
        %App::lcpan::common_args,
        %App::lcpan::dist_args,
        as => {
            schema => 'dirname*',
            pos => 1,
        },
    },
    deps => {
        prog => 'git',
    },
};
sub handle_cmd {
    my %args = @_;

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

    my ($dist, $file_id);
    {
        # first find dist
        if (($file_id) = $dbh->selectrow_array(
            "SELECT id FROM file WHERE dist_name=? AND is_latest_dist", {}, $args{module_or_dist})) {
            $dist = $args{module_or_dist};
            last;
        }
        # try mod
        if (($file_id, $dist) = $dbh->selectrow_array("SELECT m.file_id, f.dist_name FROM module m JOIN file f ON m.file_id=f.id WHERE m.name=?", {}, $args{module_or_dist})) {
            last;
        }
    }
    $file_id or return [404, "No such module/dist '$args{module_or_dist}'"];

    my $res = App::lcpan::Cmd::dist_meta::handle_cmd(%args, dist=>$dist);
    return [412, $res->[1]] unless $res->[0] == 200;
    my $meta = $res->[2];

    unless ($meta->{resources} && $meta->{resources}{repository} && $meta->{resources}{repository}{type} eq 'git') {
        return [412, "No git repository specified in the distmeta's resources"];
    }
    my $url = $meta->{resources}{repository}{url};
    unless ($url =~ m!^https?://github\.com!i) {
        return [412, "Git repository is not on github ($url)"];
    }

    require IPC::System::Options;
    IPC::System::Options::system({log=>1, die=>1}, "git", "clone", $url,
                                 ( defined $args{as} ? ($args{as}) : ()));
    [200];
}

1;
# ABSTRACT: Clone github repo of a module/dist

__END__

=pod

=encoding UTF-8

=head1 NAME

App::lcpan::Cmd::gh_clone - Clone github repo of a module/dist

=head1 VERSION

This document describes version 0.005 of App::lcpan::Cmd::gh_clone (from Perl distribution App-lcpan-CmdBundle-gh), released on 2022-03-27.

=head1 DESCRIPTION

This module handles the L<lcpan> subcommand C<gh-clone>.

=head1 FUNCTIONS


=head2 handle_cmd

Usage:

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

Clone github repo of a moduleE<sol>dist.

This function is not exported.

Arguments ('*' denotes required arguments):

=over 4

=item * B<as> => I<dirname>

=item * B<cpan> => I<dirname>



( run in 0.971 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )