Skip to content

Commit

Permalink
gli removed
Browse files Browse the repository at this point in the history
  • Loading branch information
martinciu committed Apr 11, 2012
1 parent 041db89 commit 51ba6cb
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 94 deletions.
98 changes: 5 additions & 93 deletions bin/issues
Original file line number Diff line number Diff line change
@@ -1,97 +1,9 @@
#!/usr/bin/env ruby
# 1.9 adds realpath to resolve symlinks; 1.8 doesn't
# have this method, so we add it so we get resolved symlinks
# and compatibility
unless File.respond_to? :realpath
class File #:nodoc:
def self.realpath path
return realpath(File.readlink(path)) if symlink?(path)
path
end
end
end
$: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
require 'rubygems'
require 'gli'
require 'issues'

include GLI

program_desc 'GitHub Issues Command Line Interface'

version Issues::VERSION

config_file '.issuesrc'

desc 'username'
arg_name 'username'
flag [:u, :username]

desc 'password'
arg_name 'password'
flag [:p, :password]

desc 'repository'
arg_name 'owner/repo'
flag [:r, :repo]

desc 'List of Issues'
command :list do |c|
c.desc 'milestone'
c.arg_name 'milestone_number'
c.flag [:m, :milestone]

c.desc 'assignee'
c.arg_name 'username'
c.flag [:a, :assignee]
lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)

c.desc 'list of comma separated Label names. Example: bug,ui,@high'
c.arg_name 'label1,label2'
c.flag [:l, :labels]

c.desc 'state'
c.arg_name 'state'
c.flag [:s, :state]

c.action do |global_options,options,args|
Issues::Issue.all.each do |issue|
p issue
end
end
end

desc 'List of Milestones'
command :milestones do |c|
c.desc 'state'
c.arg_name 'state'
c.default_value 'open'
c.flag [:s, :state]

c.action do |global_options,options,args|
Issues::Milestone.all.each do |milestone|
p milestone
end
end
end


pre do |global,command,options,args|
Issues::Github.username = global[:username]
Issues::Github.password = global[:password]
Issues.repo = Issues::Repo.find(global[:repo])
true
end

post do |global,command,options,args|
# Post logic here
# Use skips_post before a command to skip this
# block on that command only
end

on_error do |exception|
# Error logic here
# return false to skip default error handling
true
end
require 'rubygems'
require 'issues'

exit GLI.run(ARGV)
exit Issues::Command.run(ARGV)
1 change: 0 additions & 1 deletion issues.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ spec = Gem::Specification.new do |s|
s.extra_rdoc_files = ['README.rdoc','issues.rdoc']
s.rdoc_options << '--title' << 'issues' << '--main' << 'README.rdoc' << '-ri'
s.bindir = 'bin'
s.add_dependency 'gli', ['>=1.5.1']
s.add_dependency 'virtus', ['>=0.4.0']
s.add_dependency 'faraday', ['>=0.7.6']
s.add_dependency 'json', ['>=1.6.5']
Expand Down
1 change: 1 addition & 0 deletions lib/issues.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'json'
require 'singleton'

require 'issues/command'
require 'issues/version'
require 'issues/github'
require 'issues/user'
Expand Down
22 changes: 22 additions & 0 deletions lib/issues/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'optparse'

require 'issues/command/options'
require 'issues/command/resolver'

require 'issues/command/base'
require 'issues/command/authenticated'
require 'issues/command/with_repo'

require 'issues/command/main'
require 'issues/command/help'
require 'issues/command/milestones'

module Issues
module Command

def self.run(args)
Main.new(args).run
end

end
end
16 changes: 16 additions & 0 deletions lib/issues/command/authenticated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Issues
module Command
module Authenticated

def self.included(base)
base.after_initialize :authenticate
end

private
def authenticate
Issues::Github.username = options.fetch(:username).value
Issues::Github.password = options.fetch(:password).value
end
end
end
end
51 changes: 51 additions & 0 deletions lib/issues/command/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Issues
module Command
module Base

def initialize(args)
self.options = Options::Parser.new(self.class, args).parse
run_hooks
end

def run
success
end

def self.included(base)
base.send(:include, Options)
base.extend ClassMethods
Resolver.register base
end

module ClassMethods

def after_initialize(message)
hooks << message
end

def hooks
@hooks ||= []
end

end

private
attr_accessor :options

def hooks
self.class.hooks
end

def run_hooks
hooks.each do |hook|
send(hook)
end
end

def success
0
end

end
end
end
13 changes: 13 additions & 0 deletions lib/issues/command/help.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Issues
module Command
class Help
include Issues::Command::Base

def run
puts "help"

success
end
end
end
end
23 changes: 23 additions & 0 deletions lib/issues/command/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Issues
module Command

class Main
include Options

option :repo, '--repo owner/repo', '-r'
option :username, '--username login', '-u'
option :password, '--password password', '-p'

def initialize(args)
@args = args
end

def run
Resolver.new(@args).command.run
end

end

end
end

19 changes: 19 additions & 0 deletions lib/issues/command/milestones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Issues
module Command

class Milestones
include Issues::Command::Base
include Issues::Command::Authenticated
include Issues::Command::WithRepo

def run
Issues::Milestone.all.each do |milestone|
p milestone
end

success
end
end
end

end
31 changes: 31 additions & 0 deletions lib/issues/command/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'issues/command/options/option'
require 'issues/command/options/group'
require 'issues/command/options/parser'

module Issues
module Command
module Options

def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def options
@options ||= Group.new(global_options)
end

def option(name, *args)
options << Option.new(name, args)
end

private
def global_options
Main.options unless self == Main
end

end

end
end
end
28 changes: 28 additions & 0 deletions lib/issues/command/options/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Issues
module Command
module Options

class Group
extend Forwardable
def_delegators :options, :each, :find, :<<

def initialize(options = [])
options ||= []
options.each do |option|
self << option
end
end

def fetch(name)
find { |option| option.name == name.to_s }
end

private
def options
@options ||= []
end
end

end
end
end
18 changes: 18 additions & 0 deletions lib/issues/command/options/option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Issues
module Command
module Options

class Option
attr_accessor :name, :args, :value
def initialize(name, args)
self.name, self.args = name, args
end

def name=(name)
@name = name.to_s
end
end

end
end
end
36 changes: 36 additions & 0 deletions lib/issues/command/options/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Issues
module Command
module Options

class Parser
def initialize(klass, args)
self.klass, self.args = klass, args
end

def parse
parser.parse!(args)

options
end

private
attr_accessor :klass, :args
def options
klass.options
end

def parser
OptionParser.new do |parser|
options.each do |option|
parser.on(*option.args) do |value|
option.value = value
end
end
end
end

end

end
end
end
Loading

0 comments on commit 51ba6cb

Please sign in to comment.