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

- old
+ new

@@ -1,14 +1,17 @@ #!/usr/bin/env ruby - +$:.push File.expand_path("../../lib", __FILE__) +require 'version' require 'flattr' require 'launchy' +require 'commander/import' -options = { - :git_dir => "#{Dir.pwd}/.git", - :git_config => "#{Dir.pwd}/.git/config" -} +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 @@ -55,61 +58,106 @@ @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 -begin +module Auth + class <<self + def is_authed? + Git.config('flattr.token') != "" + end - unless File.exists?(options[:git_dir]) - error "Don't seem to be a git repository" - exit 1 - 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 - unless File.exists?(options[:git_config]) - error "Git .config file not found" - exit 1 + Git.set_config "flattr.token", token.chomp + end end +end - unless Git.github_repository? - error "Not a GitHub repository" - exit 1 - end +Flattr.configure do |config| + config.access_token = Git.config 'flattr.token' +end - if Git.config('flattr.token') == "" +command :repo do |c| + + + c.syntax = "git flattr repo" + c.description = "Flattr the repository" + c.action do |args, options| begin - Launchy.open("https://git-flattr.herokuapp.com/") - puts "Token: " - token = gets - 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" - print "Token: " - token = gets - end - if token.nil? - error "Invalid access token" + + Git.valid? + + if !Auth.is_authed? + Auth.do! + end + + flattr = Flattr.new + flattr.flattr Git.github_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 exit 1 + rescue Flattr::Error::Forbidden => e + error e.message + exit 1 end - Git.set_config "flattr.token", token.chomp end +end - Flattr.configure do |config| - config.access_token = Git.config 'flattr.token' - end +command :commit do |c| + c.syntax = "git flattr commit [commit]" + c.description = "Flattr a commit" + c.action do |args, opts| + begin + Git.valid? - flattr = Flattr.new - flattr.flattr Git.github_url - thing = flattr.thing_lookup Git.github_url - puts "Flattred #{Git.github_url} (#{thing.link})!" - exit 0 + if !Auth.is_authed? + Auth.do! + end -rescue Flattr::Error::Unauthorized => e - error e.message - exit 1 -rescue Flattr::Error::Forbidden => e - error e.message - exit 1 + ARGV.shift + end + end end