diff options
author | Daniel DeLeo <dan@opscode.com> | 2011-03-27 09:32:12 -0700 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2011-03-27 09:32:12 -0700 |
commit | 9239ff893ea5b36db03ab53489376dcbd9536ecd (patch) | |
tree | cc04c4e4ea2502c8d11d9389a191499af015a08b /chef/lib/chef/knife.rb | |
parent | 588035fd3e2ed2668eb97b28c8f7ff8abbffbf92 (diff) | |
download | chef-9239ff893ea5b36db03ab53489376dcbd9536ecd.tar.gz |
[CHEF-2137] move subcommand loading into its own class
Diffstat (limited to 'chef/lib/chef/knife.rb')
-rw-r--r-- | chef/lib/chef/knife.rb | 61 |
1 files changed, 9 insertions, 52 deletions
diff --git a/chef/lib/chef/knife.rb b/chef/lib/chef/knife.rb index d1539e3b24..e7f2979bf6 100644 --- a/chef/lib/chef/knife.rb +++ b/chef/lib/chef/knife.rb @@ -20,6 +20,7 @@ require 'chef/version' require 'mixlib/cli' require 'chef/mixin/convert_to_class_name' +require 'chef/knife/subcommand_loader' require 'pp' @@ -28,9 +29,6 @@ class Chef include Mixlib::CLI extend Chef::Mixin::ConvertToClassName - CHEF_FILE_IN_GEM = /chef-[\d]+\.[\d]+\.[\d]+/ - CURRENT_CHEF_GEM = /chef-#{Regexp.escape(Chef::VERSION)}/ - attr_accessor :name_args def self.msg(msg="") @@ -74,6 +72,14 @@ class Chef name.nil? || name.empty? end + def self.subcommand_loader + @subcommand_loader ||= Knife::SubcommandLoader.new(chef_config_dir) + end + + def self.load_commands + subcommand_loader.load_commands + end + def self.subcommands @@subcommands ||= {} end @@ -88,12 +94,6 @@ class Chef @subcommands_by_category end - # Load all the sub-commands - def self.load_commands - find_subcommand_files.each { |subcommand| require subcommand } - subcommands - end - # Print the list of subcommands knife knows about. If +preferred_category+ # is given, only subcommands in that category are shown def self.list_commands(preferred_category=nil) @@ -205,49 +205,6 @@ class Chef @@chef_config_dir end - # Returns an Array of paths to knife commands located in chef_config_dir/plugins/knife/ - # and ~/.chef/plugins/knife/ - def self.site_subcommands - user_specific_files = [] - - if chef_config_dir - user_specific_files.concat Dir.glob(File.expand_path("plugins/knife/*.rb", chef_config_dir)) - end - - # finally search ~/.chef/plugins/knife/*.rb - user_specific_files.concat Dir.glob(File.join(ENV['HOME'], '.chef', 'plugins', 'knife', '*.rb')) - - user_specific_files.map! { |path| path[/(.+).rb/, 1] } - user_specific_files - end - - # Returns an Array of paths to knife commands built-in to chef, or installed via gem. - # If rubygems is not installed, falls back to globbing the knife directory. - def self.gem_and_builtin_subcommands - begin - # search all gems for chef/knife/*.rb - require 'rubygems' - files = Gem.find_files 'chef/knife/*.rb' - # wow, this is a sad hack :( - # Gem.find_files finds files in all versions of a gem, which - # means that if chef 0.10 and 0.9.x are installed, we'll try to - # require, e.g., chef/knife/ec2_server_create, which will cause - # a gem activation error. So remove files from older chef gems. - files.reject! {|f| f =~ CHEF_FILE_IN_GEM && f !~ CURRENT_CHEF_GEM } - files.map! do |file| - file[/(#{Regexp.escape File.join('chef', 'knife', '')}.*\.rb)/, 1] - end.uniq! - rescue LoadError - # The "require paths" of the core knife subcommands bundled with chef - files = Dir[File.expand_path(File.join(File.dirname(__FILE__), 'knife', '*.rb'))] - files.map! { |knife_file| knife_file[/#{CHEF_ROOT}#{Regexp.escape(File::SEPARATOR)}(.*)\.rb/,1] } - end - files - end - - def self.find_subcommand_files - @@subcommand_files ||= (gem_and_builtin_subcommands + site_subcommands).flatten.uniq - end public |