-
Notifications
You must be signed in to change notification settings - Fork 1
/
gistp
executable file
·132 lines (98 loc) · 2.6 KB
/
gistp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/perl
use strict;
use warnings;
use 5.008001;
use WWW::Mechanize;
use Getopt::Long;
use File::Slurp qw(slurp);
use Pod::Usage;
my %options;
GetOptions(\%options, "--name=s", "--private", "--help");
run(\%options, @ARGV);
sub run {
my($opts, @args) = @_;
if ($opts->{help}) {
pod2usage(0);
}
my @files = setup_files($opts, @args);
my %fields;
my $i = 1;
for my $file (@files) {
$fields{"file_name[gistfile$i]"} = $file->{name};
$fields{"file_contents[gistfile$i]"} = $file->{content};
$i++;
}
$fields{private} = 'on' if $opts->{private};
my %auth = get_auth() or die "No github.user and github.token found. See http://github.com/account\n";
my($id, $uri) = post_gist({ %fields, %auth });
git_clone($id, $uri);
}
sub setup_files {
my($opts, @args) = @_;
my @files;
if (@args == 0 or $args[0] eq '-') {
my $content = join '', <STDIN>;
@files = ({ name => $opts->{name} || '', content => $content });
} else {
for my $arg (@args) {
push @files, {
name => $arg,
content => scalar slurp($arg),
};
}
}
return @files;
}
sub post_gist {
my $fields = shift;
my $mech = WWW::Mechanize->new;
$mech->get('http://gist.github.com');
$mech->submit_form(
form_number => 2,
fields => $fields,
);
my $id = ($mech->uri->path =~ m!^/([0-9a-f]+)$!)[0]
or die "Creating a gist failed: " . $mech->uri;
return ($id, $mech->uri);
}
sub git_clone {
my($id, $uri) = @_;
my $dir = $ENV{GIST_DIR} || $ENV{GISTY_DIR} || "$ENV{HOME}/gists";
unless (-e $dir) {
mkdir $dir, 0777 or die "$dir: $!";
}
chdir $dir;
warn "Created a new gist at $uri\nNow cloning to $dir/$id\n";
system "git clone git\@gist.github.com:$id.git";
}
sub get_auth {
my ($self) = @_;
my($login, $token);
if (eval "require Git; 1") {
$login = Git::config('github.user');
$token = Git::config('github.token');
} else {
chomp($login = `git config --global github.user`);
chomp($token = `git config --global github.token`);
}
return unless $login and $token;
return (
login => $login,
token => $token,
);
}
__END__
=head1 NAME
gistp - Uploads and clone new paste to gist
=head1 SYNOPSIS
gistp code.pl
gistp --private foo.rb bar.txt
echo foo | gistp
ls -l | gistp --name ls-output.txt
=head1 DESCRIPTION
See L<README.mkdn> for details.
=head1 LICENSE
same as Perl.
=head1 AUTHOR
Tatsuhiko Miyagawa
=cut