Skip to content

Commit

Permalink
* abstract common code into gist.rb
Browse files Browse the repository at this point in the history
* use github username and token from shell environment if it exists there. If not, check git config
  • Loading branch information
jjb committed Dec 6, 2010
1 parent 4ebc27c commit 2891da4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 71 deletions.
47 changes: 14 additions & 33 deletions Commands/Private Gist.tmCommand
Expand Up @@ -7,43 +7,24 @@
<key>command</key>
<string>#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require ENV['TM_BUNDLE_SUPPORT'] + '/gist.rb'
contents = ENV['TM_SELECTED_TEXT'] || `/usr/bin/env cat "#{ENV['TM_FILEPATH']}"`
name = ENV['TM_FILENAME'] || 'Uploaded from TextMate'
if contents.nil? || contents.strip == ''
puts "Error: no content to upload"
else
login = `#{ENV['TM_GIT'] || "/usr/bin/env git"} config --global --get github.user`.chomp
token = `#{ENV['TM_GIT'] || "/usr/bin/env git"} config --global --get github.token`.chomp
if login.nil? || login == ''
puts "Error: your github login is not set"
elsif token.nil? || token == ''
puts "Error: your github token is not set"
if @continue
response = Net::HTTP.post_form(URI.parse('http://gist.github.com/api/v1/xml/new'), {
"files[#{@name}]" =&gt; @contents,
"private" =&gt; true,
"login" =&gt; @login,
"token" =&gt; @token
})
if response.body =~ /&lt;repo&gt;(\w+)&lt;\/repo&gt;/xi
`echo -n "https://gist.github.com/#{$1}" | pbcopy`
puts "Private Gist URL copied to clipboard"
else
response = Net::HTTP.post_form(URI.parse('http://gist.github.com/api/v1/xml/new'), {
"files[#{name}]" =&gt; contents,
"private" =&gt; true,
"login" =&gt; login,
"token" =&gt; token
})
if response.body =~ /&lt;repo&gt;(\w+)&lt;\/repo&gt;/xi
`echo -n "https://gist.github.com/#{$1}" | pbcopy`
puts "Private Gist URL copied to clipboard"
else
puts "Error uploading private gist"
end
puts "Error uploading private gist"
end
end</string>
end
</string>
<key>input</key>
<string>selection</string>
<key>keyEquivalent</key>
Expand Down
45 changes: 13 additions & 32 deletions Commands/Public Gist.tmCommand
Expand Up @@ -7,42 +7,23 @@
<key>command</key>
<string>#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require ENV['TM_BUNDLE_SUPPORT'] + '/gist.rb'
contents = ENV['TM_SELECTED_TEXT'] || `/usr/bin/env cat "#{ENV['TM_FILEPATH']}"`
name = ENV['TM_FILENAME'] || 'Uploaded from TextMate'
if contents.nil? || contents.strip == ''
puts "Error: no content to upload"
else
login = `#{ENV['TM_GIT'] || "/usr/bin/env git"} config --global --get github.user`.chomp
token = `#{ENV['TM_GIT'] || "/usr/bin/env git"} config --global --get github.token`.chomp
if login.nil? || login == ''
puts "Error: your github login is not set"
elsif token.nil? || token == ''
puts "Error: your github token is not set"
if @continue
response = Net::HTTP.post_form(URI.parse('http://gist.github.com/api/v1/xml/new'), {
"files[#{@name}]" =&gt; @contents,
"login" =&gt; @login,
"token" =&gt; @token
})
if response.body =~ /&lt;repo&gt;(\w+)&lt;\/repo&gt;/xi
`echo -n "http://gist.github.com/#{$1}" | pbcopy`
puts "Public Gist URL copied to clipboard"
else
response = Net::HTTP.post_form(URI.parse('http://gist.github.com/api/v1/xml/new'), {
"files[#{name}]" =&gt; contents,
"login" =&gt; login,
"token" =&gt; token
})
if response.body =~ /&lt;repo&gt;(\w+)&lt;\/repo&gt;/xi
`echo -n "http://gist.github.com/#{$1}" | pbcopy`
puts "Public Gist URL copied to clipboard"
else
puts "Error uploading public gist"
end
puts "Error uploading public gist"
end
end</string>
end
</string>
<key>input</key>
<string>selection</string>
<key>keyEquivalent</key>
Expand Down
21 changes: 15 additions & 6 deletions README.textile
Expand Up @@ -6,22 +6,31 @@ h2. Pre-requisites

* Install ruby
* Configure your "GitHub account":https://github.com/account
* If for some reason your git binary is in a place that /usr/bin/env cannot locate, then you can set it to a TextMate Shell variable (TM_GIT)

* If for some reason your git binary is in a place that /usr/bin/env cannot locate, then you can set it to a TextMate Shell variable (`TM_GIT`), or add the path its in to `PATH`. For example, if you installed git from MacPorts, you would add `/opt/local/bin:` to the front of `PATH`,
* add your github username and token to your git config:
<pre>
<code>
$ git config --global github.user [your username]
$ git config --global github.token [your token]
</code>
</pre>
**or** to your shell environment:
<pre>
$ git config --global github.user [your username]
$ git config --global github.token [your token]
<code>
export GITHUB_USER="[your username]"
export GITHUB_TOKEN="[your token]"
</code>
</pre>

h2. Install

<pre>
<code>
mkdir -p ~/Library/Application\ Support/TextMate/Bundles/
cd ~/Library/Application\ Support/TextMate/Bundles
git clone git://github.com/ivanvc/gists-tmbundle.git Gists.tmbundle
osascript -e 'tell app "TextMate" to reload bundles'
</code>
</pre>

h2. That's it

* Use ⌘⌥⇧I to create a private gist
Expand Down
27 changes: 27 additions & 0 deletions Support/gist.rb
@@ -0,0 +1,27 @@
require 'net/http'
require 'uri'

git_binary = ENV['TM_GIT'] || "/usr/bin/env git"
github_config_get = "#{git_binary} config --global --get github"

@login = ENV['GITHUB_USER'] || `#{github_config_get}.user`.chomp
@token = ENV['GITHUB_TOKEN'] || `#{github_config_get}.token`.chomp

@continue = true

if @login.nil? || @login == ''
print "\nError: your github login is not set. See https://github.com/ivanvc/gists-tmbundle for configuration instructions.\n"
@continue = false
end
if @token.nil? || @token == ''
print "\nError: your github token is not set. See https://github.com/ivanvc/gists-tmbundle for configuration instructions.\n"
@continue = false
end

@contents = ENV['TM_SELECTED_TEXT'] || `/usr/bin/env cat "#{ENV['TM_FILEPATH']}"`
@name = ENV['TM_FILENAME'] || 'Uploaded from TextMate'

if @contents.nil? || @contents.strip == ''
puts "Error: no content to upload"
@continue = false
end

0 comments on commit 2891da4

Please sign in to comment.