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, 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

#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
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 files specified in the manifest that are missing. Please 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_filesObject



69
70
71
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 69

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

#subcommand_for_args(args) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 89

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