Created
February 16, 2009 22:26
-
-
Save copiousfreetime/65415 to your computer and use it in GitHub Desktop.
Altered gist.rb to take the filename on the command line
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 | |
| =begin | |
| INSTALL: | |
| curl http://github.com/defunkt/gist/raw/master/gist.rb > gist && | |
| chmod 755 gist && | |
| sudo mv gist /usr/local/bin/gist | |
| USE: | |
| gist < file.txt | |
| echo secret | gist -p # or --private | |
| gist 1234 > something.txt | |
| =end | |
| require 'open-uri' | |
| require 'net/http' | |
| module Gist | |
| extend self | |
| @@gist_url = 'http://gist.github.com/%s.txt' | |
| def read(gist_id) | |
| return help if gist_id == '-h' || gist_id == '--help' || gist_id.nil? || gist_id[/help/] | |
| if gist_id.respond_to?( :read ) then | |
| gist_id.read | |
| else | |
| open(@@gist_url % gist_id).read | |
| end | |
| end | |
| def write(content, fname, private_gist) | |
| name = nil | |
| ext = nil | |
| if fname then | |
| name = File.basename( fname ) | |
| ext = File.extname( fname ) | |
| end | |
| url = URI.parse('http://gist.github.com/gists') | |
| req = Net::HTTP.post_form(url, data(name, ext, content, private_gist)) | |
| created = copy( req['Location'] ) | |
| content | |
| end | |
| def help | |
| "USE:\n " + File.read(__FILE__).match(/USE:(.+?)=end/m)[1].strip | |
| end | |
| private | |
| def copy(content) | |
| case RUBY_PLATFORM | |
| when /darwin/ | |
| return content if `which pbcopy`.strip == '' | |
| IO.popen('pbcopy', 'r+') { |clip| clip.print content.strip } | |
| when /linux/ | |
| return content if `which xclip`.strip == '' | |
| IO.popen('xclip', 'r+') { |clip| clip.print content.strip } | |
| end | |
| content | |
| end | |
| def data(name, ext, content, private_gist) | |
| return { | |
| 'file_ext[gistfile1]' => ext, | |
| 'file_name[gistfile1]' => name, | |
| 'file_contents[gistfile1]' => content | |
| }.merge(private_gist ? { 'private' => 'on' } : {}).merge(auth) | |
| end | |
| def auth | |
| user = `git config --global github.user`.strip | |
| token = `git config --global github.token`.strip | |
| user.empty? ? {} : { :login => user, :token => token } | |
| end | |
| end | |
| if $stdin.tty? | |
| puts Gist.read( ARGV.shift ) | |
| else | |
| pflag = ARGV.shift | |
| fname = nil | |
| if %w[ -p --private].include?( pflag ) then | |
| pflag = true | |
| else | |
| fname = pflag | |
| pflag = false | |
| end | |
| fname = fname || ARGV.shift | |
| puts Gist.write($stdin.read, fname, pflag ) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment