Permalink
Browse files

replace thor with clamp for command line processing

  • Loading branch information...
1 parent ab8b805 commit 03050044ab1fd7fe8f0db87e7692c300345b0def Matt Conway committed Jan 11, 2012
View
@@ -9,3 +9,4 @@ pkg
.DS_Store
.gitignore
atlassian-ide-plugin.xml
+log
View
@@ -14,5 +14,4 @@ require 'rubber'
Rubber::initialize(root, env)
require 'rubber/cli'
-$thor_runner = true
-Rubber::CLI.start
+Rubber::CLI.run
View
@@ -1,100 +1,30 @@
-require "thor"
-require "thor/group"
-require "thor/runner"
+require "clamp"
-# monkey patch thor arg parsing to allow "--" to short circuit
-# parsing - standard in most cli parsing systems
-class Thor::Arguments
- private
- def peek
- p = @pile.first
- p == "--" ? nil : p
- end
+# require all the rubber commands
+files = Dir[File.expand_path(File.join(File.dirname(__FILE__), 'commands/*.rb'))]
+files.each do |f|
+ require f
end
module Rubber
- class CLI < Thor
+ class CLI < Clamp::Command
- # Override Thor#help so it can give information about any class and any method.
- #
- def help(meth = nil)
- initialize_thorfiles
- if meth && !self.respond_to?(meth)
- klass, task = find_class_and_task_by_namespace(meth)
- klass.start(["-h", task].compact, :shell => self.shell)
- else
- display_klasses
- end
- end
-
- # If a task is not found on Thor::Runner, method missing is invoked and
- # Thor::Runner is then responsable for finding the task in all classes.
- #
- def method_missing(meth, *args)
- initialize_thorfiles
-
- klass, task = find_class_and_task_by_namespace(meth)
-
- args.unshift(task) if task
- klass.start(args, :shell => self.shell)
- end
-
- private
-
- def find_class_and_task_by_namespace(meth)
- meth = meth.to_s
-
- pieces = meth.split(":")
- task = pieces.pop
- namespace = pieces.join(":")
- namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
-
- klass = Thor::Base.subclasses.find { |k| k.namespace == namespace && k.tasks[task] }
- return klass, task
- end
-
- def self.exit_on_failure?
- true
- end
-
- def initialize_thorfiles
- files = Dir[File.expand_path(File.join(File.dirname(__FILE__), 'commands/*.rb'))]
- files.each do |f|
- require f
+ # setup clamp subcommands for each rubber command
+ command_classes = []
+ Rubber::Commands.constants.each do |c|
+ clazz = Rubber::Commands.const_get(c)
+ if clazz.class == Class && clazz.ancestors.include?(Clamp::Command) &&
+ clazz.respond_to?(:subcommand_name) && clazz.respond_to?(:subcommand_description)
+ subcommand clazz.subcommand_name, clazz.subcommand_description, clazz
end
end
-
- def display_klasses(show_internal=false, klasses=Thor::Base.subclasses)
- klasses -= [Thor, Thor::Runner, Thor::Group] unless show_internal
-
- raise Error, "No Thor tasks available" if klasses.empty?
-
- list = Hash.new { |h,k| h[k] = [] }
- groups = klasses.select { |k| k.ancestors.include?(Thor::Group) }
-
- # Get classes which inherit from Thor
- (klasses - groups).each { |k| list[k.namespace.split(":").first] += k.printable_tasks(false) }
-
- # Get classes which inherit from Thor::Base
- groups.map! { |k| k.printable_tasks(false).first }
- list["root"] = groups
-
- # Order namespaces with default coming first
- list = list.sort{ |a,b| a[0].sub(/^default/, '') <=> b[0].sub(/^default/, '') }
- list.each { |n, tasks| display_tasks(n, tasks) unless tasks.empty? }
- end
-
- def display_tasks(namespace, list) #:nodoc:
- list.sort!{ |a,b| a[0] <=> b[0] }
-
- say shell.set_color(namespace, :blue, true)
- say "-" * namespace.size
-
- print_table(list, :truncate => true)
- say
+
+ option ["-v", "--version"], :flag, "print version" do
+ puts Rubber.version
+ exit 0
end
-
+
end
end
@@ -2,42 +2,43 @@
module Rubber
module Commands
- class Config < Thor
+ class Config < Clamp::Command
- namespace :default
-
- method_option :host, :type => :string, :aliases => "-h",
- :desc => "Override the instance's host for generation"
- method_option :roles, :type => :string, :aliases => "-r",
- :desc => "Override the instance's roles for generation"
- method_option :file, :type => :string, :aliases => "-p",
- :desc => "Only generate files that match the given pattern"
- method_option :no_post, :type => :boolean, :aliases => "-n",
- :desc => "Skip running post commands for files that get generated"
- method_option :force, :type => :boolean, :aliases => "-f",
- :desc => "Overwrite files that already exist"
-
- desc "config", Rubber::Util.clean_indent(<<-EOS
- Generate system config files by transforming the files in the config/rubber tree
- EOS
- )
+ def self.subcommand_name
+ "config"
+ end
- def config
+ def self.subcommand_description
+ "Transform templates in the config/rubber tree"
+ end
+
+ def self.description
+ "Generate system files by transforming the files in the config/rubber tree"
+ end
+
+ option ["--host", "-h"], "HOST", "Override the instance's host for generation"
+ option ["--roles", "-r"], "ROLES", "Override the instance's roles for generation" do |str|
+ str.split(",")
+ end
+ option ["--file", "-p"], "FILE", "Only generate files matching the given pattern"
+ option ["--no_post", "-n"], :flag, "Skip running post commands"
+ option ["--force", "-f"], :flag, "Overwrite files that already exist"
+
+ def execute
cfg = Rubber::Configuration.get_configuration(Rubber.env)
instance_alias = cfg.environment.current_host
instance = cfg.instance[instance_alias]
if instance
- roles = instance.role_names
- env = cfg.environment.bind(roles, instance_alias)
- gen = Rubber::Configuration::Generator.new("#{Rubber.root}/config/rubber", roles, instance_alias)
+ role_names = instance.role_names
+ env = cfg.environment.bind(role_names, instance_alias)
+ gen = Rubber::Configuration::Generator.new("#{Rubber.root}/config/rubber", role_names, instance_alias)
elsif ['development', 'test'].include?(Rubber.env)
- instance_alias = options[:host] || instance_alias
- roles = options[:roles].split(',') if options[:roles]
- roles ||= cfg.environment.known_roles
- role_items = roles.collect do |r|
+ instance_alias = host || instance_alias
+ role_names = roles || cfg.environment.known_roles
+ role_items = role_names.collect do |r|
Rubber::Configuration::RoleItem.new(r, r == "db" ? {'primary' => true} : {})
end
- env = cfg.environment.bind(roles, instance_alias)
+ env = cfg.environment.bind(role_names, instance_alias)
domain = env.domain
instance = Rubber::Configuration::InstanceItem.new(instance_alias, domain, role_items,
'dummyid', 'm1.small', 'ami-7000f019', ['dummygroup'])
@@ -46,18 +47,18 @@ def config
instance.internal_host = instance.full_name
instance.internal_ip = "127.0.0.1"
cfg.instance.add(instance)
- gen = Rubber::Configuration::Generator.new("#{Rubber.root}/config/rubber", roles, instance_alias)
+ gen = Rubber::Configuration::Generator.new("#{Rubber.root}/config/rubber", role_names, instance_alias)
gen.fake_root ="#{Rubber.root}/tmp/rubber"
else
puts "Instance not found for host: #{instance_alias}"
exit 1
end
- if options[:file]
- gen.file_pattern = options[:file]
+ if file
+ gen.file_pattern = file
end
- gen.no_post = options[:no_post]
- gen.force = options[:force]
+ gen.no_post = no_post?
+ gen.force = force?
gen.stop_on_error_cmd = env.stop_on_error_cmd
gen.run
Oops, something went wrong.

0 comments on commit 0305004

Please sign in to comment.