Browse Source

add a standalone gist binary

master
Jonathan "Duke" Leto 14 years ago
parent
commit
e38429d98d
  1. 409
      bin/gist

409
bin/gist

@ -0,0 +1,409 @@
#!/usr/bin/env ruby
#
# This file, gist, is generated code.
# Please DO NOT EDIT or send patches for it.
#
# Please take a look at the source from
# http://github.com/defunkt/gist
# and submit patches against the individual files
# that build gist.
#
module Gist
module Manpage
extend self
def display(name)
puts manpage(name)
end
def manpage(name)
return "** Can't find groff(1)" unless groff?
require 'open3'
out = nil
Open3.popen3(groff_command) do |stdin, stdout, _|
stdin.puts raw_manpage(name)
stdin.close
out = stdout.read.strip
end
out
end
def raw_manpage(name)
if File.exists? file = File.dirname(__FILE__) + "/../../man/#{name}.1"
File.read(file)
else
DATA.read
end
end
def groff?
system("which groff > /dev/null")
end
def groff_command
"groff -Wall -mtty-char -mandoc -Tascii"
end
def puts(*args)
page_stdout
super
end
def page_stdout
return unless $stdout.tty?
read, write = IO.pipe
if Kernel.fork
$stdin.reopen(read)
read.close
write.close
ENV['LESS'] = 'FSRX'
Kernel.select [STDIN]
pager = ENV['PAGER'] || 'less -isr'
pager = 'cat' if pager.empty?
exec pager rescue exec "/bin/sh", "-c", pager
else
$stdout.reopen(write)
$stderr.reopen(write) if $stderr.tty?
read.close
write.close
end
end
end
end
module Gist
VERSION = Version = '1.2.1'
end
require 'open-uri'
require 'net/http'
require 'optparse'
require 'gist/manpage' unless defined?(Gist::Manpage)
require 'gist/version' unless defined?(Gist::Version)
module Gist
extend self
GIST_URL = 'http://gist.github.com/%s.txt'
CREATE_URL = 'http://gist.github.com/gists'
PROXY = ENV['HTTP_PROXY'] ? URI(ENV['HTTP_PROXY']) : nil
PROXY_HOST = PROXY ? PROXY.host : nil
PROXY_PORT = PROXY ? PROXY.port : nil
def execute(*args)
private_gist = defaults["private"]
gist_filename = nil
gist_extension = defaults["extension"]
browse_enabled = defaults["browse"]
opts = OptionParser.new do |opts|
opts.banner = "Usage: gist [options] [filename or stdin]"
opts.on('-p', '--[no-]private', 'Make the gist private') do |priv|
private_gist = priv
end
t_desc = 'Set syntax highlighting of the Gist by file extension'
opts.on('-t', '--type [EXTENSION]', t_desc) do |extension|
gist_extension = '.' + extension
end
opts.on('-o','--[no-]open', 'Open gist in browser') do |o|
browse_enabled = o
end
opts.on('-m', '--man', 'Print manual') do
Gist::Manpage.display("gist")
end
opts.on('-v', '--version', 'Print version') do
puts Gist::Version
exit
end
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit
end
end
opts.parse!(args)
begin
if $stdin.tty?
if args.empty?
puts opts
exit
end
if File.exists?(file = args[0])
input = File.read(file)
gist_filename = file
gist_extension = File.extname(file) if file.include?('.')
else
abort "Can't find #{file}"
end
else
input = $stdin.read
end
url = write(input, private_gist, gist_extension, gist_filename)
browse(url) if browse_enabled
puts copy(url)
rescue => e
warn e
puts opts
end
end
def write(content, private_gist = false, gist_extension = nil, gist_filename = nil)
url = URI.parse(CREATE_URL)
proxy = Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT)
req = proxy.post_form(url,
data(gist_filename, gist_extension, content, private_gist))
req['Location']
end
def read(gist_id)
open(GIST_URL % gist_id).read
end
def browse(url)
if RUBY_PLATFORM =~ /darwin/
`open #{url}`
elsif ENV['OS'] == 'Windows_NT' or
RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw|wince/i
`start "" "#{url}"`
end
end
def copy(content)
cmd = case true
when system("type pbcopy > /dev/null 2>&1")
:pbcopy
when system("type xclip > /dev/null 2>&1")
:xclip
when system("type putclip > /dev/null 2>&1")
:putclip
end
if cmd
IO.popen(cmd.to_s, 'r+') { |clip| clip.print content }
end
content
end
private
def data(name, ext, content, private_gist)
return {
'file_ext[gistfile1]' => ext ? ext : '.txt',
'file_name[gistfile1]' => name,
'file_contents[gistfile1]' => content
}.merge(private_gist ? { 'action_button' => 'private' } : {}).merge(auth)
end
def auth
user = config("github.user")
token = config("github.token")
user.to_s.empty? ? {} : { :login => user, :token => token }
end
def defaults
extension = config("gist.extension")
extension = nil if extension && extension.empty?
return {
"private" => config("gist.private"),
"browse" => config("gist.browse"),
"extension" => extension,
}
end
def config(key)
env_key = ENV[key.upcase.gsub(/\./, '_')]
return env_key if env_key and not env_key.empty?
str_to_bool `git config --global #{key}`.strip
end
def str_to_bool(str)
if str.size > 0 and str[0].chr == '!'
command = str[1, str.length]
value = `#{command}`
else
value = str
end
case value.downcase.strip
when "false", "0", "nil", "", "no", "off"
nil
when "true", "1", "yes", "on"
true
else
value
end
end
end
Gist.execute(*ARGV)
__END__
.\" generated with Ronn/v0.5
.\" http://github.com/rtomayko/ronn/
.
.TH "GIST" "1" "June 2010" "GITHUB" "Gist Manual"
.
.SH "NAME"
\fBgist\fR \- gist on the command line
.
.SH "SYNOPSIS"
\fBgist\fR [\fB\-p\fR] [\fB\-t extension\fR] \fIFILE\fR
.
.SH "DESCRIPTION"
\fBgist\fR can be used to create gists on gist.github.com from the command
line. There are two primary methods of creating gists.
.
.P
If standard input is supplied, it will be used as the content of the
new gist. If \fIFILE\fR is provided, the content of that file will be used
to create the gist.
.
.P
Once your gist is successfully created, the URL will be copied to your
clipboard. If you are on OS X, \fBgist\fR will open the gist in your
browser, too.
.
.SH "OPTIONS"
\fBgist\fR's default mode of operation is to read content from standard
input and create a public, text gist from it, tied to your GitHub
account if you user and token are provided (see \fBCONFIGURATION\fR).
.
.P
These options can be used to change this behavior:
.
.TP
\fB\-p\fR, \fB\-\-private\fR
Create a private gist instead of a public gist.
.
.TP
\fB\-t\fR, \fB\-\-type\fR
Set the file extension explicitly. Passing a type of \fBrb\fR ensure
the gist is created as a Ruby file.
.
.TP
\fB\-o\fR, \fB\-\-[no\-]open\fR
Open the gist in your browser after creation. Or don't. Defaults
to \-\-open
.
.P
You may additionally ask for help:
.
.TP
\fB\-h\fR, \fB\-\-help\fR
Print help.
.
.TP
\fB\-m\fR, \fB\-\-man\fR
Display this man page.
.
.SH "AUTHENTICATION"
There are two ways to set GitHub user and token info:
.
.IP "\(bu" 4
Using env vars GITHUB_USER and GITHUB_TOKEN
.
.IP
$ export GITHUB_USER=johndoe
$ export GITHUB_TOKEN=mysecretgithubtoken
$ gist ~/example
.
.IP "\(bu" 4
Using git\-config(1)
.
.IP "" 0
.
.P
Use git\-config(1) to display the currently configured GitHub username:
.
.IP "" 4
.
.nf
$ git config \-\-global github.user
.
.fi
.
.IP "" 0
.
.P
Or, set the GitHub username with:
.
.IP "" 4
.
.nf
$ git config \-\-global github.user <username>
.
.fi
.
.IP "" 0
.
.P
See \fIhttp://github.com/guides/local\-github\-config\fR for more
information.
.
.SH "CONFIGURATION"
You can set a few options in your git config (using git\-config(1)) to
control the default behavior of gist(1).
.
.IP "\(bu" 4
gist.private \- boolean (yes or no) \- Determines whether to make a gist
private by default
.
.IP "\(bu" 4
gist.extension \- string \- Default extension for gists you create.
.
.IP "\(bu" 4
gist.browse \- boolean (yes or no) \- Whether to open the gist in your
browser after creation. Default: yes
.
.IP "" 0
.
.SH "ENVIRONMENT"
The following environment variables affect the execution of \fBgist\fR:
.
.IP "\(bu" 4
HTTP_PROXY \- Proxy to use when Gisting. Should be "http://host:port/"
.
.IP "" 0
.
.SH "EXAMPLES"
.
.nf
$ gist < file.txt
$ echo secret | gist \-\-private
$ echo "puts :hi" | gist \-t rb
$ gist script.py
.
.fi
.
.SH "BUGS"
\fIhttp://github.com/defunkt/gist/issues\fR
.
.SH "AUTHOR"
Chris Wanstrath :: chris@ozmm.org
.
.SH "SEE ALSO"
hub(1), git(1), git\-clone(1), \fIhttp://github.com\fR, \fIhttp://github.com/defunkt/gist\fR
Loading…
Cancel
Save