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 93 def self.category(new_category) @category = new_category end
# File lib/chef/knife.rb, line 105 def self.common_name snake_case_name.split('_').join(' ') end
# File lib/chef/knife.rb, line 204 def self.deps(&block) @dependency_loader = block end
# File lib/chef/knife.rb, line 176 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 78 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 138 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.each do |command| msg subcommands[command].banner if subcommands[command] end msg end end
# File lib/chef/knife.rb, line 118 def self.load_commands @commands_loaded ||= subcommand_loader.load_commands end
# File lib/chef/knife.rb, line 208 def self.load_deps @dependency_loader && @dependency_loader.call end
# File lib/chef/knife.rb, line 69 def self.msg(msg="") ui.msg(msg) end
# File lib/chef/knife.rb, line 73 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 166 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 101 def self.snake_case_name convert_to_snake_case(name.split('::').last) unless unnamed? end
# File lib/chef/knife.rb, line 97 def self.subcommand_category @category || snake_case_name.split('_').first unless unnamed? end
# File lib/chef/knife.rb, line 188 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 114 def self.subcommand_loader @subcommand_loader ||= Knife::SubcommandLoader.new(chef_config_dir) end
# File lib/chef/knife.rb, line 122 def self.subcommands @@subcommands ||= {} end
# File lib/chef/knife.rb, line 126 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 65 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 110 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 61 def self.use_separate_defaults? true end
Generated with the Darkfish Rdoc Generator 2.