Created
December 24, 2010 05:49
-
-
Save ahoward/753943 to your computer and use it in GitHub Desktop.
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 ruby | |
Main { | |
name 'gist' | |
description <<-__ | |
command line script for writing and reading to http://gist.github.com/ | |
__ | |
examples <<-__ | |
. gist a file, display the url on stdout. also copy url to clipboard. | |
~> gist a.rb | |
. gist a file. printing gisted content on stdout. note that this | |
behaviour makes gist work great as a file in vim/emacs. the location | |
of the gist is still copied to the clipboard. | |
~> gist a.rb | cat | |
. same, but privately | |
~> gist a.rb | cat | |
. same, but specify ext | |
~> gist a.txt --ext .rb | cat | |
. same, but give the file name (ext is derived) | |
~> gist javascript.txt --name awesome.js | |
__ | |
option('--private', '-p'){ | |
default false | |
} | |
option('--name=name', '-n'){ | |
default 'gist.rb' | |
} | |
option('--ext=ext', '-e'){ | |
default '.rb' | |
} | |
option('--debug', '-d'){ | |
} | |
def run | |
abort('no auth!') if auth.empty? | |
@private = !!param['private'].given? | |
@name = param['name'].value | |
@ext = param['ext'].value | |
content = process(argv) | |
@private = !!@private | |
@name = File.basename(@name) | |
@ext = @ext.sub(/^[.]*/, '.') | |
options = { | |
:private => @private, | |
:name => @name, | |
:ext => @ext | |
} | |
gisted = gist(content, options) | |
if STDIN.tty? | |
STDOUT.puts(gisted.location) | |
else | |
STDOUT.write(gisted) | |
end | |
end | |
def process(*paths) | |
paths.flatten! | |
paths.compact! | |
paths.uniq! | |
stdin = paths.delete('-') | |
stdin = true if paths.empty? | |
content = stdin ? STDIN.read : '' | |
exts = [] | |
names = [] | |
paths.each do |path| | |
content += IO.read(path) | |
basename = File.basename(path) | |
base, ext = basename.split(/[.]/, 2) | |
names.push(basename) | |
exts.push(ext) if ext | |
end | |
unless params['ext'].given? | |
ext = exts.last | |
@ext = ".#{ ext }" if ext | |
end | |
unless params['name'].given? | |
name = names.last | |
@name = name if name | |
end | |
content | |
end | |
def gist(content, options = {}) | |
url = URI.parse('https://gist.github.com/gists') | |
form = form_for(content, options) | |
req = post(url, form) | |
location = req['Location'] | |
class << content | |
attr_accessor :location | |
end | |
content.location = location | |
copy(location) | |
content | |
end | |
def post(url, params) | |
url = URI.parse(url.to_s) unless url.is_a?(URI) | |
req = Net::HTTP::Post.new(url.path) | |
req.form_data = params | |
http = Net::HTTP.new(url.host, url.port) | |
main = self | |
http.instance_eval do | |
@debug_output = $stderr if main.params['debug'].given? | |
http.use_ssl = true | |
@ssl_context = OpenSSL::SSL::SSLContext.new | |
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
http.start{|http| http.request(req)} | |
end | |
def form_for(content, options) | |
form = {} | |
form['file_contents[gistfile1]'] = content.to_s | |
form['file_ext[gistfile1]'] = options[:ext] | |
form['file_name[gistfile1]'] = options[:name] | |
form['private'] = 'on' if options[:private] | |
form.merge(auth) | |
end | |
def auth | |
return @auth if defined?(@auth) | |
user = `git config --global github.user`.strip | |
token = `git config --global github.token`.strip | |
@auth = user.empty? ? {} : {:login => user, :token => token} | |
end | |
def copy(content) | |
@copied = content.to_s.strip | |
program = | |
case RUBY_PLATFORM | |
when /darwin/ | |
pbcopy | |
when /linux/ | |
xclip | |
end | |
IO.popen(program, 'r+'){|pipe| pipe.write(@copied)} if program | |
@copied | |
end | |
def which(program) | |
stdout = `which #{ program }`.strip | |
path = stdout == '' ? nil : stdout | |
end | |
def pbcopy | |
return @pbcopy if defined?(@pbcopy) | |
@pbcopy = which(:pbcopy) | |
end | |
def xclip | |
return @xclip if defined?(@xclip) | |
@xclip = which(:xclip) | |
end | |
mode(:read) do | |
def run | |
ids = argv | |
ids.each do |id| | |
url = 'https://gist.github.com/%s.txt' % id | |
content = get(url).body | |
STDOUT.write(content) | |
end | |
end | |
def get(url) | |
url = URI.parse(url.to_s) unless url.is_a?(URI) | |
req = Net::HTTP::Get.new(url.path) | |
http = Net::HTTP.new(url.host, url.port) | |
main = self | |
http.instance_eval do | |
@debug_output = $stderr if main.params['debug'].given? | |
http.use_ssl = true | |
@ssl_context = OpenSSL::SSL::SSLContext.new | |
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
http.start{|http| http.request(req)} | |
end | |
end | |
} | |
BEGIN { | |
require 'open-uri' | |
require 'net/http' | |
require 'net/https' | |
require 'rubygems' | |
require 'main' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment