Class: Chef::Knife::SubcommandLoader::HashedCommandLoader

Inherits:
Chef::Knife::SubcommandLoader show all
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"

Instance Attribute Summary collapse

Attributes inherited from Chef::Knife::SubcommandLoader

#chef_config_dir, #env

Instance Method Summary collapse

Methods inherited from Chef::Knife::SubcommandLoader

autogenerated_manifest?, #command_class_from, custom_manifest?, #find_longest_key, #find_subcommands_via_dirglob, for_config, #force_load, #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

#manifestObject

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
# 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)
    { pref_category => manifest[KEY]["plugins_by_category"][pref_category] }
  else
    manifest[KEY]["plugins_by_category"]
  end
end

#load_command(args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 53

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
        Chef::Log.error "The file #{sc} is missing for subcommand '#{subcommand_for_args(args)}'. Please rehash to update the subcommands cache."
        return false
      end
    end
    true
  end
end

#subcommand_filesObject



49
50
51
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 49

def subcommand_files
  manifest[KEY]["plugins_paths"].values.flatten
end

#subcommand_for_args(args) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 70

def subcommand_for_args(args)
  if manifest[KEY]["plugins_paths"].key?(args)
    args
  else
    find_longest_key(manifest[KEY]["plugins_paths"], args, "_")
  end
end