Class: Chef::Knife::SubcommandLoader
- Inherits:
-
Object
- Object
- Chef::Knife::SubcommandLoader
- Defined in:
- lib/chef/knife/core/subcommand_loader.rb,
lib/chef/knife/core/gem_glob_loader.rb,
lib/chef/knife/core/hashed_command_loader.rb,
lib/chef/knife/core/custom_manifest_loader.rb
Overview
Public Methods of a Subcommand Loader
load_commands - loads all available subcommands load_command(args) - loads subcommands for the given args list_commands(args) - lists all available subcommands,
optionally filtering by category
subcommand_files - returns an array of all subcommand files
that could be loaded
commnad_class_from(args) - returns the subcommand class for the
user-requested command
Direct Known Subclasses
Defined Under Namespace
Classes: CustomManifestLoader, GemGlobLoader, HashedCommandLoader
Instance Attribute Summary collapse
-
#chef_config_dir ⇒ Object
readonly
Returns the value of attribute chef_config_dir.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Class Method Summary collapse
- .autogenerated_manifest? ⇒ Boolean
- .custom_manifest? ⇒ Boolean
-
.for_config(chef_config_dir) ⇒ Object
A small factory method.
-
.gem_glob_loader(chef_config_dir) ⇒ Object
There are certain situations where we want to shortcut the loader selection in self.for_config and force using the GemGlobLoader.
- .plugin_manifest ⇒ Object
- .plugin_manifest? ⇒ Boolean
- .plugin_manifest_path ⇒ Object
Instance Method Summary collapse
- #command_class_from(args) ⇒ Object
-
#find_longest_key(hash, words, sep = "_") ⇒ Object
Utility function for finding an element in a hash given an array of words and a separator.
-
#find_subcommands_via_dirglob ⇒ Object
This is shared between the custom_manifest_loader and the gem_glob_loader.
- #force_load ⇒ Object
- #guess_category(args) ⇒ Object
-
#initialize(chef_config_dir, env = nil) ⇒ SubcommandLoader
constructor
A new instance of SubcommandLoader.
- #list_commands(pref_cat = nil) ⇒ Object
- #load_command(_command_args) ⇒ Object
-
#load_commands ⇒ Object
Load all the sub-commands.
-
#positional_arguments(args) ⇒ Array<String>
The positional arguments from the argument list provided by the users.
-
#site_subcommands ⇒ Object
Returns an Array of paths to knife commands located in chef_config_dir/plugins/knife/ and ~/.chef/plugins/knife/.
-
#subcommand_files ⇒ Object
Subclassses should define this themselves.
Constructor Details
#initialize(chef_config_dir, env = nil) ⇒ SubcommandLoader
Returns a new instance of SubcommandLoader.
87 88 89 90 91 92 93 94 95 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 87 def initialize(chef_config_dir, env = nil) @chef_config_dir = chef_config_dir # Deprecated and un-used instance variable. @env = env unless env.nil? Chef.log_deprecation("The env argument to Chef::Knife::SubcommandLoader is deprecated. If you are using env to inject/mock HOME, consider mocking Chef::Util::PathHelper.home instead.") end end |
Instance Attribute Details
#chef_config_dir ⇒ Object (readonly)
Returns the value of attribute chef_config_dir.
40 41 42 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 40 def chef_config_dir @chef_config_dir end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
41 42 43 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 41 def env @env end |
Class Method Details
.autogenerated_manifest? ⇒ Boolean
71 72 73 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 71 def self.autogenerated_manifest? plugin_manifest? && plugin_manifest.key?(HashedCommandLoader::KEY) end |
.custom_manifest? ⇒ Boolean
75 76 77 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 75 def self.custom_manifest? plugin_manifest? && plugin_manifest.key?("plugins") end |
.for_config(chef_config_dir) ⇒ Object
A small factory method. Eventually, this is the only place where SubcommandLoader should know about its subclasses, but to maintain backwards compatibility many of the instance methods in this base class contain default implementations of the functions sub classes should otherwise provide or directly instantiate the appropriate subclass
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 49 def self.for_config(chef_config_dir) if autogenerated_manifest? Chef::Log.debug("Using autogenerated hashed command manifest #{plugin_manifest_path}") Knife::SubcommandLoader::HashedCommandLoader.new(chef_config_dir, plugin_manifest) elsif custom_manifest? Chef.log_deprecation("Using custom manifest #{plugin_manifest_path} is deprecated. Please use a `knife rehash` autogenerated manifest instead.") Knife::SubcommandLoader::CustomManifestLoader.new(chef_config_dir, plugin_manifest) else Knife::SubcommandLoader::GemGlobLoader.new(chef_config_dir) end end |
.gem_glob_loader(chef_config_dir) ⇒ Object
There are certain situations where we want to shortcut the loader selection in self.for_config and force using the GemGlobLoader
63 64 65 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 63 def self.gem_glob_loader(chef_config_dir) Knife::SubcommandLoader::GemGlobLoader.new(chef_config_dir) end |
.plugin_manifest ⇒ Object
79 80 81 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 79 def self.plugin_manifest Chef::JSONCompat.from_json(File.read(plugin_manifest_path)) end |
.plugin_manifest? ⇒ Boolean
67 68 69 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 67 def self.plugin_manifest? plugin_manifest_path && File.exist?(plugin_manifest_path) end |
.plugin_manifest_path ⇒ Object
83 84 85 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 83 def self.plugin_manifest_path Chef::Util::PathHelper.home(".chef", "plugin_manifest.json") end |
Instance Method Details
#command_class_from(args) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 122 def command_class_from(args) cmd_words = positional_arguments(args) load_command(cmd_words) result = Chef::Knife.subcommands[find_longest_key(Chef::Knife.subcommands, cmd_words, "_")] result || Chef::Knife.subcommands[args.first.tr("-", "_")] end |
#find_longest_key(hash, words, sep = "_") ⇒ Object
Utility function for finding an element in a hash given an array of words and a separator. We find the the longest key in the hash composed of the given words joined by the separator.
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 171 def find_longest_key(hash, words, sep = "_") match = nil until match || words.empty? candidate = words.join(sep) if hash.key?(candidate) match = candidate else words.pop end end match end |
#find_subcommands_via_dirglob ⇒ Object
This is shared between the custom_manifest_loader and the gem_glob_loader
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 140 def find_subcommands_via_dirglob # The "require paths" of the core knife subcommands bundled with chef files = Dir[File.join(Chef::Util::PathHelper.escape_glob_dir(File.("../../../knife", __FILE__)), "*.rb")] 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 |
#force_load ⇒ Object
104 105 106 107 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 104 def force_load @loaded = false load_commands end |
#guess_category(args) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 130 def guess_category(args) category_words = positional_arguments(args) category_words.map! { |w| w.split("-") }.flatten! find_longest_key(Chef::Knife.subcommands_by_category, category_words, " ") end |
#list_commands(pref_cat = nil) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 113 def list_commands(pref_cat = nil) load_commands if pref_cat && Chef::Knife.subcommands_by_category.key?(pref_cat) { pref_cat => Chef::Knife.subcommands_by_category[pref_cat] } else Chef::Knife.subcommands_by_category end end |
#load_command(_command_args) ⇒ Object
109 110 111 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 109 def load_command(_command_args) load_commands end |
#load_commands ⇒ Object
Load all the sub-commands
98 99 100 101 102 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 98 def load_commands return true if @loaded subcommand_files.each { |subcommand| Kernel.load subcommand } @loaded = true end |
#positional_arguments(args) ⇒ Array<String>
The positional arguments from the argument list provided by the users. Used to search for subcommands and categories.
190 191 192 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 190 def positional_arguments(args) args.select { |arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ } end |
#site_subcommands ⇒ Object
Returns an Array of paths to knife commands located in chef_config_dir/plugins/knife/ and ~/.chef/plugins/knife/
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 196 def site_subcommands user_specific_files = [] if chef_config_dir user_specific_files.concat Dir.glob(File.("plugins/knife/*.rb", Chef::Util::PathHelper.escape_glob_dir(chef_config_dir))) end # finally search ~/.chef/plugins/knife/*.rb Chef::Util::PathHelper.home(".chef", "plugins", "knife") do |p| user_specific_files.concat Dir.glob(File.join(Chef::Util::PathHelper.escape_glob_dir(p), "*.rb")) end user_specific_files end |
#subcommand_files ⇒ Object
Subclassses should define this themselves. Eventually, this will raise a NotImplemented error, but for now, we mimic the behavior the user was likely to get in the past.
156 157 158 159 160 161 162 163 164 |
# File 'lib/chef/knife/core/subcommand_loader.rb', line 156 def subcommand_files Chef.log_deprecation "Using Chef::Knife::SubcommandLoader directly is deprecated. Please use Chef::Knife::SubcommandLoader.for_config(chef_config_dir, env)" @subcommand_files ||= if Chef::Knife::SubcommandLoader.plugin_manifest? Chef::Knife::SubcommandLoader::CustomManifestLoader.new(chef_config_dir, env).subcommand_files else Chef::Knife::SubcommandLoader::GemGlobLoader.new(chef_config_dir, env).subcommand_files end end |