module Shell::Extensions::ObjectCoreExtensions

Extensions to be included in every 'main' object in chef-shell. These objects are extended with this module.

Public Instance Methods

all_help_descriptions() click to toggle source
# File lib/chef/shell/ext.rb, line 117
def all_help_descriptions
  help_descriptions
end
desc(help_text) click to toggle source
# File lib/chef/shell/ext.rb, line 121
def desc(help_text)
  @desc = help_text
end
ensure_session_select_defined() click to toggle source
# File lib/chef/shell/ext.rb, line 39
def ensure_session_select_defined
  # irb breaks if you prematurely define IRB::JobMangager
  # so these methods need to be defined at the latest possible time.
  unless jobs.respond_to?(:select_session_by_context)
    def jobs.select_session_by_context(&block) # rubocop:disable Lint/NestedMethodDefinition
      @jobs.select { |job| block.call(job[1].context.main) }
    end
  end

  unless jobs.respond_to?(:session_select)
    def jobs.select_shell_session(target_context) # rubocop:disable Lint/NestedMethodDefinition
      session = if target_context.kind_of?(Class)
                  select_session_by_context { |main| main.kind_of?(target_context) }
                else
                  select_session_by_context { |main| main.equal?(target_context) }
                end
      Array(session.first)[1]
    end
  end
end
explain(explain_text) click to toggle source
# File lib/chef/shell/ext.rb, line 125
def explain(explain_text)
  @explain = explain_text
end
explain_command(method_name) click to toggle source
# File lib/chef/shell/ext.rb, line 87
def explain_command(method_name)
  help = all_help_descriptions.find { |h| h.cmd.to_s == method_name.to_s }
  if help
    puts ""
    puts "Command: #{method_name}"
    puts "".ljust(80, "=")
    puts help.explanation || help.desc
    puts "".ljust(80, "=")
    puts ""
  else
    puts ""
    puts "command #{method_name} not found or no help available"
    puts ""
  end
end
find_or_create_session_for(context_obj) click to toggle source
# File lib/chef/shell/ext.rb, line 60
def find_or_create_session_for(context_obj)
  ensure_session_select_defined
  if subsession = jobs.select_shell_session(context_obj)
    jobs.switch(subsession)
  else
    irb(context_obj)
  end
end
help_banner() click to toggle source
# File lib/chef/shell/ext.rb, line 69
def help_banner
  banner = []
  banner << ""
  banner << "#{Chef::Dist::SHELL} Help"
  banner << "".ljust(80, "=")
  banner << "| " + "Command".ljust(25) + "| " + "Description"
  banner << "".ljust(80, "=")

  all_help_descriptions.each do |help_text|
    banner << "| " + help_text.cmd.ljust(25) + "| " + help_text.desc
  end
  banner << "".ljust(80, "=")
  banner << "\n"
  banner << "Use help(:command) to get detailed help with individual commands"
  banner << "\n"
  banner.join("\n")
end
help_descriptions() click to toggle source
# File lib/chef/shell/ext.rb, line 113
def help_descriptions
  @help_descriptions ||= []
end
off() click to toggle source

returns :off so you can just do `tracing off'

# File lib/chef/shell/ext.rb, line 109
def off
  :off
end
on() click to toggle source

helpfully returns :on so we can have sugary syntax like `tracing on'

# File lib/chef/shell/ext.rb, line 104
def on
  :on
end
singleton_method_added(mname) click to toggle source
# File lib/chef/shell/ext.rb, line 133
def singleton_method_added(mname)
  if @desc
    help_descriptions << Help.new(mname.to_s, @desc.to_s, @explain)
    @desc, @explain = nil, nil
  end
  if @subcommand_help
    @subcommand_help.each do |subcommand, text|
      help_descriptions << Help.new("#{mname}.#{subcommand}", text.to_s, nil)
    end
  end
  @subcommand_help = {}
end
subcommands(subcommand_help = {}) click to toggle source
# File lib/chef/shell/ext.rb, line 129
def subcommands(subcommand_help = {})
  @subcommand_help = subcommand_help
end