bin/git-flattr in git-flattr-0.0.3 vs bin/git-flattr in git-flattr-0.0.9

- old
+ new

@@ -1,126 +1,22 @@ #!/usr/bin/env ruby $:.push File.expand_path("../../lib", __FILE__) -require 'version' -require 'flattr' -require 'launchy' + +require 'git-flattr' require 'commander/import' program :name, "git flattr" program :description, "Easily flattr GitHub repositories from the CLI" program :version, VERSION program :help, 'Author', 'Simon Gate <simon@smgt.me>' default_command :help -def error message - puts "Error: #{message}" -end - -class Git - class <<self - def command cmd, opts = [] - opts = [opts].flatten.map {|s| escape(s) }.join(' ') - git_cmd = "git #{cmd} #{opts} 2>&1" - `#{git_cmd}`.chomp - end - - def escape(s) - escaped = s.to_s.gsub('\'', '\'\\\'\'') - %Q{"#{escaped}"} - end - - def config name - command('config', ['--get', name]) - end - - def set_config name, value - command('config', ['--global', '--add', name, value]) - end - - def github_repository? - origin = config 'remote.origin.url' - !origin.match(%r{^(https://[a-z0-9\-_]+@github\.com|git@github\.com:|https://github\.com/)}).nil? - end - - def github_url - origin = config 'remote.origin.url' - if @github_url.nil? - if origin.match %r{^https://[a-zA-Z0-9\-_]+@github\.com} - match = origin.match(%r{^https://[a-zA-Z0-9\-_]+@github\.com/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)\.git$}) - elsif origin.match %r{^git@github.com:} - match = origin.match(%r{^git@github\.com:([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)\.git$}) - elsif origin.match %r{^https://github\.com/} - match = origin.match(%r{^https://github\.com/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)\.git$}) - end - if match.nil? - error "Parsing GitHub origin." - exit 1 - else - @github_url = "https://github.com/#{match[1]}/#{match[2]}" - end - end - return @github_url - end - - def valid? - - git_opts = { - :dir => "#{Dir.pwd}/.git", - :config => "#{Dir.pwd}/.git/config" - } - - unless File.exists?(git_opts[:dir]) - error "Don't seem to be a git repository" - exit 1 - end - - unless File.exists?(git_opts[:config]) - error "Git .config file not found" - exit 1 - end - - unless Git.github_repository? - error "Not a GitHub repository" - exit 1 - end - end - end -end - -module Auth - class <<self - def is_authed? - Git.config('flattr.token') != "" - end - - def do! - begin - Launchy.open("https://git-flattr.herokuapp.com/") - token = ask("Token: ") - rescue Exception => e - puts e.message - puts "Seems like you are missing a Flattr access token." - puts "Browse to http://git-flattr.herokuapp.com and follow the instructions" - token = ask("Token: ") - end - if token.nil? - error "Invalid access token" - exit 1 - end - - Git.set_config "flattr.token", token.chomp - end - end -end - Flattr.configure do |config| config.access_token = Git.config 'flattr.token' end command :repo do |c| - - c.syntax = "git flattr repo" c.description = "Flattr the repository" c.action do |args, options| begin @@ -136,14 +32,12 @@ puts "Flattred #{Git.github_url} (#{thing.link})!" exit 0 rescue Flattr::Error::Unauthorized => e error e.message - exit 1 rescue Flattr::Error::Forbidden => e error e.message - exit 1 end end end command :commit do |c| @@ -156,8 +50,46 @@ if !Auth.is_authed? Auth.do! end ARGV.shift + + commit = ARGV.shift + if commit.nil? + error "No commit supplied" + end + + commit = Git.commit commit + + if !commit + error "Commit does not exist in this repository" + end + + commit_url = "#{Git.github_url}/commit/#{commit}" + flattr = Flattr.new + puts commit_url + flattr.flattr commit_url + thing = flattr.thing_lookup Git.github_url + puts "Flattred #{Git.github_url} (#{thing.link})!" + exit 0 + rescue Flattr::Error::Unauthorized => e + error e.message + rescue Flattr::Error::Forbidden => e + error e.message + end + end +end + +command :info do |c| + c.syntax = "git flattr info" + c.description = "Information about the current repository" + c.action do |args, opts| + Git.valid? + flattr = Flattr.new + thing = flattr.thing_lookup Git.github_url + if thing + puts "[#{thing.flattrs}] flattrs, #{thing.title} ( #{thing.url} )" + else + puts "This GitHub repo is not on Flattr." end end end