Class: Chef::Knife::SubcommandLoader

Inherits:
Object
  • Object
show all
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

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

command_class_from(args) - returns the subcommand class for the

user-requested command

Direct Known Subclasses

GemGlobLoader, HashedCommandLoader

Defined Under Namespace

Classes: GemGlobLoader, HashedCommandLoader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chef_config_dir) ⇒ SubcommandLoader

Returns a new instance of SubcommandLoader.



98
99
100
# File 'lib/chef/knife/core/subcommand_loader.rb', line 98

def initialize(chef_config_dir)
  @chef_config_dir = chef_config_dir
end

Instance Attribute Details

#chef_config_dirObject (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

Class Method Details

.autogenerated_manifest?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/chef/knife/core/subcommand_loader.rb', line 67

def self.autogenerated_manifest?
  plugin_manifest? && plugin_manifest.key?(HashedCommandLoader::KEY)
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



48
49
50
51
52
53
54
55
# File 'lib/chef/knife/core/subcommand_loader.rb', line 48

def self.for_config(chef_config_dir)
  if autogenerated_manifest?
    Chef::Log.trace("Using autogenerated hashed command manifest #{plugin_manifest_path}")
    Knife::SubcommandLoader::HashedCommandLoader.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



59
60
61
# File 'lib/chef/knife/core/subcommand_loader.rb', line 59

def self.gem_glob_loader(chef_config_dir)
  Knife::SubcommandLoader::GemGlobLoader.new(chef_config_dir)
end

.generate_hashObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/knife/core/subcommand_loader.rb', line 79

def self.generate_hash
  output = if plugin_manifest?
             plugin_manifest
           else
             { Chef::Knife::SubcommandLoader::HashedCommandLoader::KEY => {} }
           end
  output[Chef::Knife::SubcommandLoader::HashedCommandLoader::KEY]["plugins_paths"] = Chef::Knife.subcommand_files
  output[Chef::Knife::SubcommandLoader::HashedCommandLoader::KEY]["plugins_by_category"] = Chef::Knife.subcommands_by_category
  output
end

.plugin_manifestObject



71
72
73
# File 'lib/chef/knife/core/subcommand_loader.rb', line 71

def self.plugin_manifest
  Chef::JSONCompat.from_json(File.read(plugin_manifest_path))
end

.plugin_manifest?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/chef/knife/core/subcommand_loader.rb', line 63

def self.plugin_manifest?
  plugin_manifest_path && File.exist?(plugin_manifest_path)
end

.plugin_manifest_pathObject



75
76
77
# File 'lib/chef/knife/core/subcommand_loader.rb', line 75

def self.plugin_manifest_path
  ChefConfig::PathHelper.home(".chef", "plugin_manifest.json")
end

.write_hash(data) ⇒ Object



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

def self.write_hash(data)
  plugin_manifest_dir = File.expand_path("..", plugin_manifest_path)
  FileUtils.mkdir_p(plugin_manifest_dir) unless File.directory?(plugin_manifest_dir)
  File.open(plugin_manifest_path, "w") do |f|
    f.write(Chef::JSONCompat.to_json_pretty(data))
  end
end

Instance Method Details

#command_class_from(args) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/chef/knife/core/subcommand_loader.rb', line 128

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 longest key in the hash composed of the given words joined by the separator.



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/chef/knife/core/subcommand_loader.rb', line 166

def find_longest_key(hash, words, sep = "_")
  words = words.dup
  match = nil
  until match || words.empty?
    candidate = words.join(sep).tr("-", "_")
    if hash.key?(candidate)
      match = candidate
    else
      words.pop
    end
  end
  match
end

#find_subcommands_via_dirglobObject

This is shared between the custom_manifest_loader and the gem_glob_loader



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chef/knife/core/subcommand_loader.rb', line 145

def find_subcommands_via_dirglob
  # The "require paths" of the core knife subcommands bundled with chef
  files = Dir[File.join(ChefConfig::PathHelper.escape_glob_dir(File.expand_path("../../knife", __dir__)), "*.rb")]
  version_file_match = /#{Regexp.escape(File.join('chef', 'knife', 'version.rb'))}/
  subcommand_files = {}
  files.each do |knife_file|
    rel_path = knife_file[/#{KNIFE_ROOT}#{Regexp.escape(File::SEPARATOR)}(.*)\.rb/, 1]
    # Exclude version.rb file for the gem. It's not a knife command, and  force-loading it later
    # because loaded via in subcommand files generates CLI warnings about its consts already having been defined
    next if knife_file&.match?(version_file_match)

    subcommand_files[rel_path] = knife_file
  end
  subcommand_files
end

#force_loadObject



110
111
112
113
# File 'lib/chef/knife/core/subcommand_loader.rb', line 110

def force_load
  @loaded = false
  load_commands
end

#guess_category(args) ⇒ Object



136
137
138
139
140
141
# File 'lib/chef/knife/core/subcommand_loader.rb', line 136

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



119
120
121
122
123
124
125
126
# File 'lib/chef/knife/core/subcommand_loader.rb', line 119

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



115
116
117
# File 'lib/chef/knife/core/subcommand_loader.rb', line 115

def load_command(_command_args)
  load_commands
end

#load_commandsObject

Load all the sub-commands



103
104
105
106
107
108
# File 'lib/chef/knife/core/subcommand_loader.rb', line 103

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.

Returns:

  • (Array<String>)


186
187
188
# File 'lib/chef/knife/core/subcommand_loader.rb', line 186

def positional_arguments(args)
  args.grep(/^(([[:alnum:]])[[:alnum:]\_\-]+)$/)
end

#site_subcommandsObject

Returns an Array of paths to knife commands located in chef_config_dir/plugins/knife/ and ~/.chef/plugins/knife/



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/chef/knife/core/subcommand_loader.rb', line 192

def site_subcommands
  user_specific_files = []

  if chef_config_dir
    user_specific_files.concat Dir.glob(File.expand_path("plugins/knife/*.rb", ChefConfig::PathHelper.escape_glob_dir(chef_config_dir)))
  end

  # finally search ~/.chef/plugins/knife/*.rb
  ChefConfig::PathHelper.home(".chef", "plugins", "knife") do |p|
    user_specific_files.concat Dir.glob(File.join(ChefConfig::PathHelper.escape_glob_dir(p), "*.rb"))
  end

  user_specific_files
end