Explicitly set the category for the current command to new_category The category is normally determined from the first word of the command name, but some commands make more sense using two or more words
new_category: |
A String to set the category to (see examples) |
Data bag commands would be in the ‘data’ category by default. To put them in the ‘data bag’ category:
category('data bag')
# File lib/chef/knife.rb, line 94 def self.category(new_category) @category = new_category end
# File lib/chef/knife.rb, line 106 def self.common_name snake_case_name.split('_').join(' ') end
# File lib/chef/knife.rb, line 205 def self.dependency_loaders @dependency_loaders ||= [] end
# File lib/chef/knife.rb, line 209 def self.deps(&block) dependency_loaders << block end
# File lib/chef/knife.rb, line 177 def self.guess_category(args) category_words = args.select {|arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ } category_words.map! {|w| w.split('-')}.flatten! matching_category = nil while (!matching_category) && (!category_words.empty?) candidate_category = category_words.join(' ') matching_category = candidate_category if subcommands_by_category.key?(candidate_category) matching_category || category_words.pop end matching_category end
# File lib/chef/knife.rb, line 79 def self.inherited(subclass) unless subclass.unnamed? subcommands[subclass.snake_case_name] = subclass end end
Print the list of subcommands knife knows about. If preferred_category is given, only subcommands in that category are shown
# File lib/chef/knife.rb, line 139 def self.list_commands(preferred_category=nil) load_commands category_desc = preferred_category ? preferred_category + " " : '' msg "Available #{category_desc}subcommands: (for details, knife SUB-COMMAND --help)\n\n" if preferred_category && subcommands_by_category.key?(preferred_category) commands_to_show = {preferred_category => subcommands_by_category[preferred_category]} else commands_to_show = subcommands_by_category end commands_to_show.sort.each do |category, commands| next if category =~ /deprecated/ msg "** #{category.upcase} COMMANDS **" commands.sort.each do |command| msg subcommands[command].banner if subcommands[command] end msg end end
# File lib/chef/knife.rb, line 119 def self.load_commands @commands_loaded ||= subcommand_loader.load_commands end
# File lib/chef/knife.rb, line 213 def self.load_deps dependency_loaders.each do |dep_loader| dep_loader.call end end
# File lib/chef/knife.rb, line 70 def self.msg(msg="") ui.msg(msg) end
# File lib/chef/knife.rb, line 74 def self.reset_subcommands! @@subcommands = {} @subcommands_by_category = nil end
Run knife for the given args (ARGV), adding options to the list of CLI options that the subcommand knows how to handle.
args: |
usually ARGV |
options: |
A Mixlib::CLI option parser hash. These options are how |
subcommands know about global knife CLI options
# File lib/chef/knife.rb, line 167 def self.run(args, options={}) load_commands subcommand_class = subcommand_class_from(args) subcommand_class.options = options.merge!(subcommand_class.options) subcommand_class.load_deps instance = subcommand_class.new(args) instance.configure_chef instance.run_with_pretty_exceptions end
# File lib/chef/knife.rb, line 102 def self.snake_case_name convert_to_snake_case(name.split('::').last) unless unnamed? end
# File lib/chef/knife.rb, line 98 def self.subcommand_category @category || snake_case_name.split('_').first unless unnamed? end
# File lib/chef/knife.rb, line 189 def self.subcommand_class_from(args) command_words = args.select {|arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ } subcommand_class = nil while ( !subcommand_class ) && ( !command_words.empty? ) snake_case_class_name = command_words.join("_") unless subcommand_class = subcommands[snake_case_class_name] command_words.pop end end # see if we got the command as e.g., knife node-list subcommand_class ||= subcommands[args.first.gsub('-', '_')] subcommand_class || subcommand_not_found!(args) end
# File lib/chef/knife.rb, line 115 def self.subcommand_loader @subcommand_loader ||= Knife::SubcommandLoader.new(chef_config_dir) end
# File lib/chef/knife.rb, line 123 def self.subcommands @@subcommands ||= {} end
# File lib/chef/knife.rb, line 127 def self.subcommands_by_category unless @subcommands_by_category @subcommands_by_category = Hash.new { |hash, key| hash[key] = [] } subcommands.each do |snake_cased, klass| @subcommands_by_category[klass.subcommand_category] << snake_cased end end @subcommands_by_category end
# File lib/chef/knife.rb, line 66 def self.ui @ui ||= Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {}) end
Does this class have a name? (Classes created via Class.new don’t)
# File lib/chef/knife.rb, line 111 def self.unnamed? name.nil? || name.empty? end
Configure mixlib-cli to always separate defaults from user-supplied CLI options
# File lib/chef/knife.rb, line 62 def self.use_separate_defaults? true end
Generated with the Darkfish Rdoc Generator 2.