-
-
Save hirose31/85928 to your computer and use it in GitHub Desktop.
gistit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
sub usage { | |
my $mesg = shift; | |
(my $prog = $0) =~ s!.+/!!; | |
print "[Error] $mesg\n" if $mesg; | |
print <<EOUSAGE; | |
[usage] | |
$prog [-p|-s] file1 file2 ... : submit specified files. | |
$prog [-p|-s] : submit all files and directories in cwd. | |
-p: create public gist (default) | |
-s: create private gist | |
EOUSAGE | |
exit; | |
} | |
use strict; | |
use warnings; | |
use utf8; | |
use Carp; | |
use Cwd; | |
use File::chdir; | |
use Path::Class qw/dir file/; | |
use WWW::Mechanize; | |
use Encode; | |
use Data::Dumper; | |
sub p(@) { | |
my $d = Dumper(\@_); | |
$d =~ s/x{([0-9a-z]+)}/chr(hex($1))/ge; | |
print $d; | |
} | |
my %submit_button = (number => 2); | |
if ($ARGV[0] && $ARGV[0] =~ /^-/) { | |
my $op = shift @ARGV; | |
if ($op eq "-p") { | |
; # public (default) | |
} elsif ($op eq "-s") { | |
# private | |
%submit_button = (number => 1); | |
} else { | |
usage; | |
} | |
} | |
unless (@ARGV) { | |
print "gist all files and directories in cwd? "; | |
chomp(my $yn = <STDIN>); | |
if ($yn !~ /^[yY]$/) { | |
print "abort...\n"; | |
exit; | |
} | |
} | |
my $cwd = dir(getcwd); | |
my $github = do { | |
local $CWD = $cwd; | |
{ | |
login => qx{ git config github.user }, | |
token => qx{ git config github.token }, | |
}; | |
}; | |
die "required git config github.user & github.token" | |
unless $github->{login} && $github->{token}; | |
chomp($github->{login}, $github->{token}); | |
my $mech = WWW::Mechanize->new( stack_depth => 1 ); | |
$mech->get('http://gist.github.com'); | |
die $mech->status unless $mech->success; | |
my $i = 0; | |
my $data = {}; | |
qx{ git init } unless -d $cwd->subdir(".git"); | |
if (@ARGV) { | |
for my $fn (@ARGV) { | |
unless (-f $fn) { | |
carp "[SKIP] no a file: $fn"; | |
next; | |
} | |
my $f = $cwd->file($fn); | |
add_file($f); | |
} | |
} else { | |
$cwd->recurse( callback => sub { | |
my $f = shift; | |
return unless -f $f; | |
add_file($f); | |
}); | |
} | |
$mech->form_number(2); | |
$mech->set_fields(%$github, %$data); | |
$mech->click_button(%submit_button); | |
#p $mech->status; | |
die $mech->status unless $mech->success; | |
my ($repo) = $mech->content =~ m!(?:git\@|git://)(gist\.github\.com[:/].+?\.git)!; | |
#p $mech->content; | |
die "can't find repository path" unless $repo; | |
$repo = 'git@'.$repo; | |
$repo =~ s{gist.github.com/}{gist.github.com:}; | |
print $repo, "\n"; | |
{ | |
local $CWD = $cwd; | |
qx{ git remote add origin $repo }; | |
qx{ git pull origin master }; | |
} | |
exit; | |
sub add_file { | |
my $f = shift; | |
(my $name = $f) =~ s!^$cwd/!!; | |
return if $name =~ m!^\.git($|/)!; | |
my $index = 'gistfile' . ++$i; | |
$data->{"file_name[${index}]"} = $name; | |
$data->{"file_contents[${index}]"} = Encode::decode("utf-8", $f->slurp); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment