Skip to content

Commit

Permalink
CLI refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
scttnlsn committed Jun 21, 2011
1 parent 9d89e5b commit 4207706
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 210 deletions.
4 changes: 2 additions & 2 deletions bin/dandelion
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


$:.unshift(File.dirname(__FILE__) + '/../lib') unless $:.include?(File.dirname(__FILE__) + '/../lib') $:.unshift(File.dirname(__FILE__) + '/../lib') unless $:.include?(File.dirname(__FILE__) + '/../lib')


require 'dandelion/cli' require 'dandelion/application'


Dandelion::Cli::Main.execute(ARGV.dup) Dandelion::Application.execute(ARGV.dup)
73 changes: 73 additions & 0 deletions lib/dandelion/application.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'dandelion/command'

module Dandelion
class Application
class << self
def execute(args)
new(args).execute
end
end

def initialize(args)
@args = args
@options = {}
@parser = OptionParser.new
end

def execute
global = Command::Base.parser(@options)
parse(global)

begin
name = @args.shift
command = Command::Base.create(name)
parse(command.parser(@options))
rescue Command::InvalidCommandError
log.fatal("Invalid command: #{name}")
log.fatal(global.help)
log.fatal("Available commands:")
log.fatal(Command::Base.commands.map { |name| " #{name}" }.join("\n"))
exit 1
end

prepare
validate

command.new(@options) do |cmd|
cmd.setup(@args) if cmd.respond_to?(:setup)
cmd.execute
end
end

def log
Dandelion.logger
end

private

def parse(parser)
begin
parser.order!(@args)
rescue OptionParser::InvalidOption => e
log.fatal(e.to_s.capitalize)
log.fatal(parser.help)
exit 1
end
end

def prepare
@options[:config] ||= File.join(@options[:repo], 'dandelion.yml')
end

def validate
unless File.exists?(File.expand_path(File.join(@options[:repo], '.git')))
log.fatal("Not a git repository: #{@options[:repo]}")
exit 1
end
unless File.exists?(File.expand_path(@options[:config]))
log.fatal("Could not find file: #{@options[:config]}")
exit 1
end
end
end
end
2 changes: 1 addition & 1 deletion lib/dandelion/backend.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(gems)
end end
end end


class Backend class Base
class << self class << self
@@backends = {} @@backends = {}


Expand Down
2 changes: 1 addition & 1 deletion lib/dandelion/backend/ftp.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


module Dandelion module Dandelion
module Backend module Backend
class FTP < Backend class FTP < Backend::Base
scheme 'ftp' scheme 'ftp'


def initialize(config) def initialize(config)
Expand Down
2 changes: 1 addition & 1 deletion lib/dandelion/backend/s3.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


module Dandelion module Dandelion
module Backend module Backend
class S3 < Backend class S3 < Backend::Base
scheme 's3' scheme 's3'
gems 'aws-s3' gems 'aws-s3'


Expand Down
2 changes: 1 addition & 1 deletion lib/dandelion/backend/sftp.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


module Dandelion module Dandelion
module Backend module Backend
class SFTP < Backend class SFTP < Backend::Base
scheme 'sftp' scheme 'sftp'
gems 'net-sftp' gems 'net-sftp'


Expand Down
201 changes: 0 additions & 201 deletions lib/dandelion/cli.rb

This file was deleted.

Loading

0 comments on commit 4207706

Please sign in to comment.