In Files

Parent

Namespace

Included Modules

Class/Module Index [+]

Quicksearch

Chef::Knife

Public Class Methods

category(new_category) click to toggle source

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

Arguments

new_category:

A String to set the category to (see examples)

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 88
def self.category(new_category)
  @category = new_category
end
common_name() click to toggle source
# File lib/chef/knife.rb, line 100
def self.common_name
  snake_case_name.split('_').join(' ')
end
deps(&block) click to toggle source
# File lib/chef/knife.rb, line 199
def self.deps(&block)
  @dependency_loader = block
end
guess_category(args) click to toggle source
# File lib/chef/knife.rb, line 171
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
inherited(subclass) click to toggle source
# File lib/chef/knife.rb, line 73
def self.inherited(subclass)
  unless subclass.unnamed?
    subcommands[subclass.snake_case_name] = subclass
  end
end
list_commands(preferred_category=nil) click to toggle source

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 133
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
load_commands() click to toggle source
# File lib/chef/knife.rb, line 113
def self.load_commands
  @commands_loaded ||= subcommand_loader.load_commands
end
load_deps() click to toggle source
# File lib/chef/knife.rb, line 203
def self.load_deps
  @dependency_loader && @dependency_loader.call
end
msg(msg="") click to toggle source
# File lib/chef/knife.rb, line 64
def self.msg(msg="")
  ui.msg(msg)
end
reset_subcommands!() click to toggle source
# File lib/chef/knife.rb, line 68
def self.reset_subcommands!
  @@subcommands = {}
  @subcommands_by_category = nil
end
run(args, options={}) click to toggle source

Run knife for the given args (ARGV), adding options to the list of CLI options that the subcommand knows how to handle.

Arguments

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 161
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
snake_case_name() click to toggle source
# File lib/chef/knife.rb, line 96
def self.snake_case_name
  convert_to_snake_case(name.split('::').last) unless unnamed?
end
subcommand_category() click to toggle source
# File lib/chef/knife.rb, line 92
def self.subcommand_category
  @category || snake_case_name.split('_').first unless unnamed?
end
subcommand_class_from(args) click to toggle source
# File lib/chef/knife.rb, line 183
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
subcommand_loader() click to toggle source
# File lib/chef/knife.rb, line 109
def self.subcommand_loader
  @subcommand_loader ||= Knife::SubcommandLoader.new(chef_config_dir)
end
subcommands() click to toggle source
# File lib/chef/knife.rb, line 117
def self.subcommands
  @@subcommands ||= {}
end
subcommands_by_category() click to toggle source
# File lib/chef/knife.rb, line 121
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
ui() click to toggle source
# File lib/chef/knife.rb, line 60
def self.ui
  @ui ||= Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
end
unnamed?() click to toggle source

Does this class have a name? (Classes created via Class.new don't)

# File lib/chef/knife.rb, line 105
def self.unnamed?
  name.nil? || name.empty?
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.