Class: Chef::Knife::SubcommandLoader::HashedCommandLoader
- Inherits:
-
Chef::Knife::SubcommandLoader
- Object
- Chef::Knife::SubcommandLoader
- Chef::Knife::SubcommandLoader::HashedCommandLoader
- Defined in:
- lib/chef/knife/core/hashed_command_loader.rb
Overview
Load a subcommand from a pre-computed path for the given command.
Constant Summary collapse
- KEY =
"_autogenerated_command_paths".freeze
Instance Attribute Summary collapse
-
#manifest ⇒ Object
Returns the value of attribute manifest.
Attributes inherited from Chef::Knife::SubcommandLoader
Instance Method Summary collapse
- #guess_category(args) ⇒ Object
-
#initialize(chef_config_dir, plugin_manifest) ⇒ HashedCommandLoader
constructor
A new instance of HashedCommandLoader.
- #list_commands(pref_category = nil) ⇒ Object
- #load_command(args) ⇒ Object
- #subcommand_files ⇒ Object
- #subcommand_for_args(args) ⇒ Object
Methods inherited from Chef::Knife::SubcommandLoader
autogenerated_manifest?, #command_class_from, #find_longest_key, #find_subcommands_via_dirglob, for_config, #force_load, gem_glob_loader, #load_commands, plugin_manifest, plugin_manifest?, plugin_manifest_path, #positional_arguments, #site_subcommands
Constructor Details
#initialize(chef_config_dir, plugin_manifest) ⇒ HashedCommandLoader
Returns a new instance of HashedCommandLoader.
30 31 32 33 |
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 30 def initialize(chef_config_dir, plugin_manifest) super(chef_config_dir) @manifest = plugin_manifest end |
Instance Attribute Details
#manifest ⇒ Object
Returns the value of attribute manifest.
29 30 31 |
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 29 def manifest @manifest end |
Instance Method Details
#guess_category(args) ⇒ Object
35 36 37 38 39 |
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 35 def guess_category(args) category_words = positional_arguments(args) category_words.map! { |w| w.split("-") }.flatten! find_longest_key(manifest[KEY]["plugins_by_category"], category_words, " ") end |
#list_commands(pref_category = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 41 def list_commands(pref_category = nil) if pref_category || manifest[KEY]["plugins_by_category"].key?(pref_category) commands = { pref_category => manifest[KEY]["plugins_by_category"][pref_category] } else commands = manifest[KEY]["plugins_by_category"] end # If any of the specified plugins in the manifest dont have a valid path we will # eventually get an error and the user will need to rehash - instead, lets just # print out 1 error here telling them to rehash errors = {} commands.collect { |k, v| v }.flatten.each do |command| paths = manifest[KEY]["plugins_paths"][command] if paths && paths.is_a?(Array) # It is only an error if all the paths don't exist if paths.all? { |sc| !File.exists?(sc) } errors[command] = paths end end end if errors.empty? commands else Chef::Log.error "There are plugin files specified in the knife cache that cannot be found. Please run knife rehash to update the subcommands cache. If you see this error after rehashing delete the cache at #{Chef::Knife::SubcommandLoader.plugin_manifest_path}" Chef::Log.error "Missing files:\n\t#{errors.values.flatten.join("\n\t")}" {} end end |
#load_command(args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 73 def load_command(args) paths = manifest[KEY]["plugins_paths"][subcommand_for_args(args)] if paths.nil? || paths.empty? || (! paths.is_a? Array) false else paths.each do |sc| if File.exists?(sc) Kernel.load sc else return false end end true end end |
#subcommand_files ⇒ Object
69 70 71 |
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 69 def subcommand_files manifest[KEY]["plugins_paths"].values.flatten end |