Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 4557677fd245c04d336d649e8dd4463601bd0660 @miyagawa committed Jan 26, 2009
Showing with 193 additions and 0 deletions.
  1. +23 −0 META.yml
  2. +13 −0 Makefile.PL
  3. +26 −0 README.mkdn
  4. +131 −0 gistp
@@ -0,0 +1,23 @@
+---
+abstract: 'Uploads and clone new paste to gist'
+author:
+ - 'Tatsuhiko Miyagawa'
+distribution_type: module
+generated_by: 'Module::Install version 0.77'
+license: perl
+meta-spec:
+ url: http://module-build.sourceforge.net/META-spec-v1.4.html
+ version: 1.4
+name: gistp
+no_index:
+ directory:
+ - inc
+requires:
+ File::Slurp: 0
+ Getopt::Long: 0
+ Pod::Usage: 0
+ WWW::Mechanize: 0
+ perl: 5.8.1
+resources:
+ license: http://dev.perl.org/licenses/
+version: undef
@@ -0,0 +1,13 @@
+use inc::Module::Install;
+name 'gistp';
+license 'perl';
+all_from 'gistp';
+
+requires 'WWW::Mechanize';
+requires 'Getopt::Long';
+requires 'File::Slurp';
+requires 'Pod::Usage';
+
+install_script 'gistp';
+
+WriteAll;
@@ -0,0 +1,26 @@
+= Description
+
+Yet another command line client for gist using Perl. Posts a new (public or private) gist, then auto git clone to ~/gists.
+
+= Configuration
+
+You might want to set environment variable GIST_DIR (or GISTY_DIR) where gistp automaticaly clones your new gists for you. By default it's `~/gists`.
+
+Set global git config as explained in [github account page](http://github.com/account)
+
+ git config --global github.user your_id
+ git config --global github.token your_token
+
+= Synopsis
+
+ echo "foo" | gistp post a new gist (no name set)
+ gistp file1 file2 ... post a new gist (with names, can be multiple)
+ gistp --private file1 post a new private gist
+
+= Difference with gist and gisty
+
+[gist](http://github.com/defunkt/gist/tree/master) gets your paste from STDIN, so you can't name your script. [gisty](http://github.com/swdyh/gisty/tree/master) allows you to post files. gistp does both. gisty has more convenient features like list, pull and sync all of your gists, but gistp doesn't do that. And it's written in Perl, unlike others are in Ruby :)
+
+
+
+
@@ -0,0 +1,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, "--private", "--help");
+
+run(\%options, @ARGV);
+
+sub run {
+ my($opts, @args) = @_;
+
+ if ($opts->{help}) {
+ pod2usage(0);
+ }
+
+ my @files = setup_files(@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 @args = @_;
+
+ my @files;
+ if (@args > 0) {
+ for my $arg (@args) {
+ push @files, {
+ name => $arg,
+ content => scalar slurp($arg),
+ };
+ }
+ } else {
+ my $content = join '', <STDIN>;
+ @files = ({ name => '', content => $content });
+ }
+
+ return @files;
+}
+
+sub post_gist {
+ my $fields = shift;
+
+ my $mech = WWW::Mechanize->new;
+ $mech->get('http://gist.github.com');
+ $mech->submit_form(
+ form_number => 1,
+ 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
+
+ echo foo | gistp
+ gistp code.pl
+ gistp --private foo.rb bar.txt
+
+=head1 DESCRIPTION
+
+See L<README.mkdn> for details.
+
+=head1 LICENSE
+
+same as Perl.
+
+=head1 AUTHOR
+
+Tatsuhiko Miyagawa
+
+=cut
+

0 comments on commit 4557677

Please sign in to comment.