App-gist
view release on metacpan or search on metacpan
lib/App/gist.pm view on Meta::CPAN
$ gist script.pl
=cut
sub opt_spec {
return (
['description|d=s', 'set the description for the gist' ],
['name|n=s', 'specify the name of the file' ],
['update|u=s', 'update the given gist with the given file'],
['private|p', 'create a private gist' ],
['web|w', 'only output the web url' ]
);
}
sub validate_args {
my ($self, $opt, $args) = @_;
$self -> usage_error("Too few arguments.")
unless %$opt || @$args || ! -t STDIN;
}
sub execute {
my ($self, $opt, $args) = @_;
my $id = $opt -> {'update'};
my $file = $args -> [0];
my $description = $opt -> {'description'};
my $public = $opt -> {'private'} ? 0 : 1;
my $web = $opt -> {'web'} ? 1 : 0;
my ($name, $data);
if ($file) {
open my $fh, '<', $file or die "Err: Enter a valid file name.\n";
$data = join('', <$fh>);
close $fh;
$name = basename($file);
} else {
$name = $opt -> {'name'} || 'gistfile.txt';
$data = join('', <STDIN>);
}
my $gist = Pithub::Gists -> new;
my $info = $id ?
_edit_gist($gist, $id, $name, $data) :
_create_gist($gist, $name, $data, $description, $public);
die "Err: " . $info -> content -> {'message'} . ".\n"
unless $info -> success;
my $gist_id = $info -> content -> {'id'};
my $html_url = $info -> content -> {'html_url'};
my $pull_url = $info -> content -> {'git_pull_url'};
my $push_url = $info -> content -> {'git_push_url'};
if ($web) {
print "$html_url\n";
} else {
print "Gist '$gist_id' successfully created/updated.\n";
print "Web URL: $html_url\n";
print "Public Clone URL: $pull_url\n" if $public;
print "Private Clone URL: $push_url\n";
}
}
sub _create_gist {
my ($gist, $name, $data, $description, $public) = @_;
return $gist -> create(data => {
description => $description,
public => $public,
files => {
$name => { content => $data }
}
});
}
sub _edit_gist {
my ($gist, $id, $name, $data) = @_;
my $info = $gist -> get(gist_id => $id);
die "Err: " . $info -> content -> {'message'} . ".\n"
unless $info -> success;
return $gist -> update(
gist_id => $id,
data => {
description => $info -> content -> {'description'},
files => {
$name => { content => $data }
}
}
);
}
sub _get_credentials {
my ($login, $pass, $token);
my %identity = Config::Identity::GitHub -> load
if try_load_class('Config::Identity::GitHub');
if (%identity) {
$login = $identity{'login'};
} else {
$login = `git config github.user`; chomp $login;
}
if (!$login) {
my $error = %identity ?
"Err: missing value 'user' in ~/.github" :
"Err: Missing value 'github.user' in git config";
die "$error.\n";
}
if (%identity) {
$token = $identity{'token'};
$pass = $identity{'password'};
( run in 0.967 second using v1.01-cache-2.11-cpan-bbe5e583499 )