Created
November 25, 2013 01:29
-
-
Save crowell/7634886 to your computer and use it in GitHub Desktop.
my gist at 2013-11-24 20:29:29 -0500
This file contains 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
require 'httparty' | |
require 'colorize' | |
require 'highline/import' | |
require 'json' | |
class GitHub | |
def getToken | |
puts "[!] GitHub API Token needed to use gists".red | |
_username = ask("USERNAME: ".green) {|q| q.echo = true} | |
_password = ask("PASSWORD: ".green) {|q| q.echo = false} | |
_auth = {:username=>_username, :password=>_password} | |
_options = { | |
:basic_auth => _auth, | |
:body => { | |
"scopes" => ["gist", "repo"], | |
"note" => "slap", | |
"note_url" => "http://raxcity.com/slap" | |
}.to_json, | |
:headers => { | |
"Content-Type" => "application/json" | |
} | |
} | |
_resp = HTTParty.post("https://api.github.com/authorizations", _options) | |
if _resp.code == 201 | |
puts "[+] got and saved token".green | |
else | |
puts "[!] unable to get token: #{_resp["message"]}".red | |
return nil | |
end | |
_token = _resp["token"] | |
return _token | |
end | |
def loadToken | |
_dotfile = ENV['HOME']+"/.slaprc" | |
if File.file?(_dotfile) | |
_token = File.open(_dotfile, "rb"){|io| io.read} | |
else | |
return nil | |
end | |
return _token | |
end | |
def saveToken(token) | |
_dotfile = ENV['HOME']+"/.slaprc" | |
File.open(_dotfile, "w"){|io| io.write(token)} | |
end | |
def getAuthed | |
_tok = loadToken | |
if not _tok.nil? | |
return _tok | |
end | |
_tok = getToken | |
if not _tok.nil? | |
saveToken(_tok) | |
return _tok | |
end | |
puts "[!] ERROR: could not get auth token!".red | |
return nil | |
end | |
def postGist(files, description, pub, anon) | |
_headers= Hash.new | |
_headers["Content-Type"] = "application/json" | |
if anon == false | |
tok = getAuthed | |
if tok.nil? | |
return nil | |
end | |
_headers = Hash.new | |
_headers["Authorization"] = "token #{tok}" | |
else | |
#do nothing | |
end | |
_options = { | |
:body => { | |
"description" => description, | |
"public" => pub, | |
"files" => files | |
}.to_json, | |
:headers => _headers | |
} | |
_resp = HTTParty.post("https://api.github.com/gists", _options) | |
return _resp | |
end | |
end |
This file contains 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
require 'colorize' | |
require 'trollop' | |
require 'json' | |
require 'clipboard' | |
require_relative 'github' | |
opts = Trollop::options do | |
version "slap 0.0.1 (c) 2013 Jeffrey Crowell" | |
banner <<-EOS | |
Slap is a client for pasting to github gists from the command line | |
Usage: | |
slap [options] <filenames>+ | |
where [options] are: | |
EOS | |
opt :description, "description of the gist", | |
:default => "my gist at #{Time.now}", | |
:type => String | |
opt :public, "make the gist public" | |
opt :anonymous, "make gist anonymous" | |
end | |
if ARGV.count == 0 | |
puts "[!] please specify at least one file".red | |
abort | |
end | |
exists = true | |
files = Hash.new | |
ARGV.each {|f| | |
if not File.file?(f) | |
exists = false | |
puts "[!] file #{f} does not exist".red | |
else | |
m = Hash.new | |
m["content"] = File.open(f, "rb"){|file| file.read} | |
if m["content"].length == 0 | |
puts "[!] ERROR #{f} has size of 0".red | |
abort | |
end | |
files[File.basename(f)] = m | |
end | |
} | |
g = GitHub.new | |
resp = g.postGist(files, opts[:description], opts[:public].to_s, opts[:anonymous]) | |
if resp.nil? | |
puts "[!] ERROR: failed to upload gist".red | |
puts resp | |
abort | |
end | |
if resp["html_url"].nil? | |
puts "[!] failed to upload gist".red | |
if not resp["message"].nil? | |
puts "[!] message - #{resp["message"]}".red | |
end | |
abort | |
end | |
url = resp["html_url"] | |
Clipboard.copy(url) | |
puts "[+] gist successfully uploaded to #{url}".green | |
puts "[!] copied to clipboard if xclip is installed".green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment