Skip to content

Instantly share code, notes, and snippets.

@schacon
Forked from anonymous/gistie
Created July 22, 2008 14:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schacon/1008 to your computer and use it in GitHub Desktop.
Save schacon/1008 to your computer and use it in GitHub Desktop.
create a gist on the command line
#!/usr/bin/env ruby
require "tempfile"
GIST_URL = 'http://gist.github.com/gists'
if ARGV.include? "-p"
text = `pbpaste`
elsif !ARGV.empty?
@filename = ARGV.shift
text = File.read(File.expand_path(@filename))
elsif ENV["FILE"]
@filename = ENV["FILE"]
text = File.read(File.expand_path(ENV["FILE"]))
else
text = STDIN.read
end
@filename ||= "UnnamedFile"
@extension = File.extname(@filename)
@extension = "txt" if @extension == ""
text_file = Tempfile.open('something')
text_file << text
text_file.flush
cmd = <<-EOS
curl #{GIST_URL} \
-s -L -o /dev/null -w "%{url_effective}" \
-F "file_ext[gistfile1]=#{@extension}" \
-F "file_name[gistfile1]=#{@filename}" \
-F "file_contents[gistfile1]=<#{text_file.path}" \
-F "x=27" \
-F "y=27"
EOS
url = %x{#{cmd}}
text_file.close(true)
`echo #{url} | pbcopy`
puts "Copied to pasteboard: #{url}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment