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".freeze

Instance Attribute Summary collapse

Attributes inherited from Chef::Knife::SubcommandLoader

#chef_config_dir

Instance Method Summary collapse

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, generate_hash, #load_commands, plugin_manifest, plugin_manifest?, plugin_manifest_path, #positional_arguments, #site_subcommands, write_hash

Constructor Details

#initialize(chef_config_dir, plugin_manifest) ⇒ HashedCommandLoader

Returns a new instance of HashedCommandLoader.



31
32
33
34
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 31

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



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

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



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
68
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 42

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 don't 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.exist?(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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/knife/core/hashed_command_loader.rb', line 74

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.exist?(sc)
        Kernel.load sc
      else
        return false
      end
    end
    true
  end
end

#subcommand_filesObject



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

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

#subcommand_for_args(args) ⇒ Object



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

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