class Chef::Knife

Constants

OFFICIAL_PLUGINS

Attributes

name_args[RW]
ui[RW]

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 @param new_category [String] value to set the category to (see examples)

@example 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 113
def self.category(new_category)
  @category = new_category
end
chef_config_dir() click to toggle source
# File lib/chef/knife.rb, line 193
def self.chef_config_dir
  @@chef_config_dir ||= config_loader.chef_config_dir
end
common_name() click to toggle source
# File lib/chef/knife.rb, line 125
def self.common_name
  snake_case_name.split("_").join(" ")
end
config_loader() click to toggle source
# File lib/chef/knife.rb, line 176
def self.config_loader
  @config_loader ||= WorkstationConfigLoader.new(nil, Chef::Log)
end
dependency_loaders() click to toggle source
# File lib/chef/knife.rb, line 223
def self.dependency_loaders
  @dependency_loaders ||= []
end
deps(&block) click to toggle source
# File lib/chef/knife.rb, line 227
def self.deps(&block)
  dependency_loaders << block
end
guess_category(args) click to toggle source
# File lib/chef/knife.rb, line 142
def self.guess_category(args)
  subcommand_loader.guess_category(args)
end
inherited(subclass) click to toggle source
# File lib/chef/knife.rb, line 89
def self.inherited(subclass)
  unless subclass.unnamed?
    subcommands[subclass.snake_case_name] = subclass
    subcommand_files[subclass.snake_case_name] +=
      if subclass.superclass.to_s == "Chef::ChefFS::Knife"
        # ChefFS-based commands have a superclass that defines an
        # inhereited method which calls super. This means that the
        # top of the call stack is not the class definition for
        # our subcommand.  Try the second entry in the call stack.
        [path_from_caller(caller[1])]
      else
        [path_from_caller(caller[0])]
      end
  end
end
list_commands(preferred_category = nil) click to toggle source
# File lib/chef/knife.rb, line 240
def list_commands(preferred_category = nil)
  category_desc = preferred_category ? preferred_category + " " : ""
  msg "Available #{category_desc}subcommands: (for details, knife SUB-COMMAND --help)\n\n"
  subcommand_loader.list_commands(preferred_category).sort.each do |category, commands|
    next if category =~ /deprecated/i
    msg "** #{category.upcase} COMMANDS **"
    commands.sort.each do |command|
      subcommand_loader.load_command(command)
      msg subcommands[command].banner if subcommands[command]
    end
    msg
  end
end
load_commands() click to toggle source
# File lib/chef/knife.rb, line 138
def self.load_commands
  @commands_loaded ||= subcommand_loader.load_commands
end
load_config(explicit_config_file, profile) click to toggle source
# File lib/chef/knife.rb, line 180
def self.load_config(explicit_config_file, profile)
  config_loader.explicit_config_file = explicit_config_file
  config_loader.profile = profile
  config_loader.load

  ui.warn("No knife configuration file found. See https://docs.chef.io/config_rb_knife.html for details.") if config_loader.no_config_found?

  config_loader
rescue Exceptions::ConfigurationError => e
  ui.error(ui.color("CONFIGURATION ERROR:", :red) + e.message)
  exit 1
end
load_deps() click to toggle source
# File lib/chef/knife.rb, line 231
def self.load_deps
  dependency_loaders.each do |dep_loader|
    dep_loader.call
  end
end
msg(msg = "") click to toggle source
# File lib/chef/knife.rb, line 75
def self.msg(msg = "")
  ui.msg(msg)
end
reset_config_loader!() click to toggle source
# File lib/chef/knife.rb, line 79
def self.reset_config_loader!
  @@chef_config_dir = nil
  @config_loader = nil
end
reset_subcommands!() click to toggle source
# File lib/chef/knife.rb, line 84
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.

@param args [Array] The arguments. Usually ARGV @param options [Mixlib::CLI option parser hash] These options are how

subcommands know about global knife CLI options
# File lib/chef/knife.rb, line 204
def self.run(args, options = {})
  # Fallback debug logging. Normally the logger isn't configured until we
  # read the config, but this means any logging that happens before the
  # config file is read may be lost. If the KNIFE_DEBUG variable is set, we
  # setup the logger for debug logging to stderr immediately to catch info
  # from early in the setup process.
  if ENV["KNIFE_DEBUG"]
    Chef::Log.init($stderr)
    Chef::Log.level(:debug)
  end

  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 121
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 117
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 146
def self.subcommand_class_from(args)
  if args.size == 1 && args[0].strip.casecmp("rehash") == 0
    # To prevent issues with the rehash file not pointing to the correct plugins,
    # we always use the glob loader when regenerating the rehash file
    @subcommand_loader = Chef::Knife::SubcommandLoader.gem_glob_loader(chef_config_dir)
  end
  subcommand_loader.command_class_from(args) || subcommand_not_found!(args)
end
subcommand_files() click to toggle source
# File lib/chef/knife.rb, line 159
def self.subcommand_files
  @@subcommand_files ||= Hash.new([])
end
subcommand_loader() click to toggle source
# File lib/chef/knife.rb, line 134
def self.subcommand_loader
  @subcommand_loader ||= Chef::Knife::SubcommandLoader.for_config(chef_config_dir)
end
subcommands() click to toggle source
# File lib/chef/knife.rb, line 155
def self.subcommands
  @@subcommands ||= {}
end
subcommands_by_category() click to toggle source
# File lib/chef/knife.rb, line 163
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 71
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 130
def self.unnamed?
  name.nil? || name.empty?
end
use_separate_defaults?() click to toggle source

Configure mixlib-cli to always separate defaults from user-supplied CLI options

# File lib/chef/knife.rb, line 67
def self.use_separate_defaults?
  true
end

Private Class Methods

path_from_caller(caller_line) click to toggle source

@api private

# File lib/chef/knife.rb, line 257
def path_from_caller(caller_line)
  caller_line.split(/:\d+/).first
end