Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better error handling; better credential checks; allow unsaved docs t…

…o be uploaded;
  • Loading branch information...
commit 12e4a386e757ec4edf215e6b0dc83d1a51122f95 1 parent 59c164b
@dandean dandean authored
Showing with 29 additions and 9 deletions.
  1. +29 −9 Commands/Public Gist.tmCommand
View
38 Commands/Public Gist.tmCommand
@@ -10,18 +10,38 @@
require 'net/http'
require 'uri'
-contents = `/usr/bin/env cat #{ENV['TM_FILEPATH']}` unless ENV['TM_SELECTED_TEXT']
+contents = ENV['TM_SELECTED_TEXT'] || `/usr/bin/env cat #{ENV['TM_FILEPATH']}`
+name = ENV['TM_FILENAME'] || 'Uploaded from TextMate'
-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 contents.nil? || contents.strip == ''
+ puts "Error: no content to upload"
-response = Net::HTTP.post_form(URI.parse('http://gist.github.com/api/v1/xml/new'), { "files[#{ENV['TM_FILENAME']}]" => ENV['TM_SELECTED_TEXT'] || contents, "login" => login, "token" => token })
-
-if response.body =~ /<repo>(\w+)<\/repo>/xi
- `echo -n "http://gist.github.com/#{$1}" | pbcopy`
- puts "Public Gist URL copied to clipboard"
else
- puts "Error"
+ 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"
+
+ else
+ response = Net::HTTP.post_form(URI.parse('http://gist.github.com/api/v1/xml/new'), {
+ "files[#{name}]" => contents,
+ "login" => login,
+ "token" => token
+ })
+
+ if response.body =~ /<repo>(\w+)<\/repo>/xi
+ `echo -n "http://gist.github.com/#{$1}" | pbcopy`
+ puts "Public Gist URL copied to clipboard"
+
+ else
+ puts "Error"
+ end
+ end
+
end</string>
<key>input</key>
<string>selection</string>
Please sign in to comment.
Something went wrong with that request. Please try again.