App-GitHub
view release on metacpan or search on metacpan
lib/App/GitHub.pm view on Meta::CPAN
package App::GitHub;
use strict;
use warnings;
# ABSTRACT: GitHub Command Tools
use Carp;
use Moose;
use Net::GitHub;
use Term::ReadKey;
use Term::ReadLine;
use JSON::XS;
use IPC::Cmd qw/can_run/;
our $VERSION = '1.0.1';
has 'term' => (
is => 'rw',
required => 1,
default => sub { Term::ReadLine->new('Perl-App-GitHub') }
);
has 'prompt' => (
is => 'rw',
required => 1,
default => sub { 'github> ' }
);
has 'out_fh' => (
is => 'rw',
required => 1,
lazy => 1,
default => sub {
shift->term->OUT || \*STDOUT;
}
);
has 'repo_regexp' => (
is => 'ro',
required => 1,
isa => 'RegexpRef',
default => sub { qr/^([\-\w]+)[\/\\\s]([\-\w]+)$/ }
);
# For non-interactive mode
has 'silent' => (
is => 'rw',
required => 1,
default => 0,
);
sub print_err {
shift->print( @_, 1 );
}
sub print {
my ( $self, $message, $error ) = @_;
return 1 if $self->silent and not $error;
my $fh;
local $@;
my $rows = ( GetTerminalSize( $self->out_fh ) )[1];
my $message_rows = $message =~ tr/\n/\n/;
my $pager_use = 0;
# let less exit if one screen
no warnings 'uninitialized';
local $ENV{LESS} ||= "";
$ENV{LESS} .= " -F";
use warnings;
if ( $@ or $message_rows < $rows ) {
chomp $message;
lib/App/GitHub.pm view on Meta::CPAN
my ( $self, $ign ) = @_;
my $login = `git config --global github.user`;
my $pass = `git config --global github.pass`;
chomp($login);
chomp($pass);
unless ( ( $login and $pass ) or $ign ) {
$self->print("run git config --global github.user|pass fails");
return;
}
$self->_do_login( $login, $pass ) if $login and $pass;
}
sub _do_login {
my ( $self, $login, $pass ) = @_;
# save for set_repo
$self->{_data}->{login} = $login;
$self->{_data}->{pass} = $pass;
if ( $self->{_data}->{repo} ) {
$self->{github} = Net::GitHub->new(
version => 3,
owner => $self->{_data}->{owner},
repo => $self->{_data}->{repo},
login => $self->{_data}->{login},
pass => $self->{_data}->{pass}
);
}
else {
# Create a Net::GitHub object with the owner set to the logged in user
# Super convenient if you don't want to set a user first
$self->{github} = Net::GitHub->new(
version => 3,
login => $self->{_data}->{login},
pass => $self->{_data}->{pass},
owner => $self->{_data}->{login}
);
}
}
sub run_github {
my ( $self, $c1, $c2 ) = @_;
unless ( $self->github ) {
croak "not auth" if $self->silent;
$self->print(
q~not enough information. try calling login :user :pass or loadcfg~
);
return;
}
my @args = splice( @_, 3, scalar @_ - 3 );
eval {
my $result = $self->github->$c1->$c2(@args);
# o.blob return plain text
if ( ref $result ) {
$result = JSON::XS->new->utf8->pretty->encode($result);
}
$self->print($result);
};
if ($@) {
# custom error
if ( $@ =~ /login and pass are required/ ) {
croak "not auth" if $self->silent;
$self->print(
qq~authentication required.\ntry 'login :owner :pass' or 'loadcfg' first\n~
);
}
else {
croak $@ if $self->silent;
$self->print_err($@);
}
}
}
sub run_github_with_repo {
my ($self) = shift;
unless ( $self->{_data}->{repo} ) {
$self->print(q~no repo specified. try calling repo :owner :repo~);
return;
}
$self->run_github(
shift, shift,
$self->{_data}->{owner},
$self->{_data}->{repo}, @_
);
}
sub run_basic_repo_cmd {
my ( $self, $obj, $meth, $args ) = @_;
if ( $args and $args =~ $self->repo_regexp ) {
$self->run_github( $obj, $meth, $1, $2 );
}
else {
$self->run_github_with_repo( $obj, $meth );
}
}
################## Repos
sub repo_list {
my ( $self, $args ) = @_;
if ( $args and $args =~ /^[\w\-]+$/ ) {
$self->run_github( 'repos', 'list', $args );
}
else {
$self->run_github( 'repos', 'list' );
}
}
sub repo_create {
my ($self) = shift;
( run in 1.028 second using v1.01-cache-2.11-cpan-39bf76dae61 )