Class: Chef::Knife::SubcommandLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/knife/core/subcommand_loader.rb

Constant Summary collapse

CHEF_FILE_IN_GEM =
/chef-[\d]+\.[\d]+\.[\d]+/
CURRENT_CHEF_GEM =
/chef-#{Regexp.escape(Chef::VERSION)}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chef_config_dir, env = ENV) ⇒ SubcommandLoader

Returns a new instance of SubcommandLoader.



30
31
32
33
# File 'lib/chef/knife/core/subcommand_loader.rb', line 30

def initialize(chef_config_dir, env=ENV)
  @chef_config_dir, @env = chef_config_dir, env
  @forced_activate = {}
end

Instance Attribute Details

#chef_config_dirObject (readonly)

Returns the value of attribute chef_config_dir.



27
28
29
# File 'lib/chef/knife/core/subcommand_loader.rb', line 27

def chef_config_dir
  @chef_config_dir
end

#envObject (readonly)

Returns the value of attribute env.



28
29
30
# File 'lib/chef/knife/core/subcommand_loader.rb', line 28

def env
  @env
end

Instance Method Details

#find_subcommands_via_dirglobObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/chef/knife/core/subcommand_loader.rb', line 77

def find_subcommands_via_dirglob
  # The "require paths" of the core knife subcommands bundled with chef
  files = Dir[File.expand_path('../../../knife/*.rb', __FILE__)]
  subcommand_files = {}
  files.each do |knife_file|
    rel_path = knife_file[/#{CHEF_ROOT}#{Regexp.escape(File::SEPARATOR)}(.*)\.rb/,1]
    subcommand_files[rel_path] = knife_file
  end
  subcommand_files
end

#find_subcommands_via_rubygemsObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef/knife/core/subcommand_loader.rb', line 88

def find_subcommands_via_rubygems
  files = Gem.find_files 'chef/knife/*.rb'
  files.reject! {|f| from_old_gem?(f) }
  subcommand_files = {}
  files.each do |file|
    rel_path = file[/(#{Regexp.escape File.join('chef', 'knife', '')}.*)\.rb/, 1]
    subcommand_files[rel_path] = file
  end

  subcommand_files.merge(find_subcommands_via_dirglob)
end

#gem_and_builtin_subcommandsObject

Returns a Hash 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. The Hash is of the form => “/absolute/path” – Note: the “right” way to load the plugins is to require the relative path, i.e.,

require 'chef/knife/command'

but we’re getting frustrated by bugs at every turn, and it’s slow besides. So subcommand loader has been modified to load the plugins by using Kernel.load with the absolute path.



65
66
67
68
69
70
71
# File 'lib/chef/knife/core/subcommand_loader.rb', line 65

def gem_and_builtin_subcommands
  # search all gems for chef/knife/*.rb
  require 'rubygems'
  find_subcommands_via_rubygems
rescue LoadError
  find_subcommands_via_dirglob
end

#load_commandsObject

Load all the sub-commands



36
37
38
39
# File 'lib/chef/knife/core/subcommand_loader.rb', line 36

def load_commands
  subcommand_files.each { |subcommand| Kernel.load subcommand }
  true
end

#site_subcommandsObject

Returns an Array of paths to knife commands located in chef_config_dir/plugins/knife/ and ~/.chef/plugins/knife/



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/knife/core/subcommand_loader.rb', line 43

def 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
end

#subcommand_filesObject



73
74
75
# File 'lib/chef/knife/core/subcommand_loader.rb', line 73

def subcommand_files
  @subcommand_files ||= (gem_and_builtin_subcommands.values + site_subcommands).flatten.uniq
end