module Beaker::Subcommands::SubcommandUtil

Methods used in execution of Subcommands

Constants

CONFIG_DIR
PERSISTED_HOSTS
PERSISTED_HYPERVISORS
SUBCOMMAND_OPTIONS
SUBCOMMAND_STATE
UNPERSISTED_OPTIONS

These options should not be part of persisted subcommand state

Public Class Methods

error_with(msg, options = {}) click to toggle source

Print a message to the console and exit with specified exit code, defaults to 1  @param [String] msg the message to output @param [Hash<Object>] options to specify exit code or output stack trace

# File lib/beaker/subcommands/subcommand_util.rb, line 52
def self.error_with(msg, options = {})
  puts msg
  puts options[:stack_trace] if options[:stack_trace]
  exit_code = options[:exit_code] ? options[:exit_code] : 1
  exit(exit_code)
end
execute_subcommand?(arg0) click to toggle source
# File lib/beaker/subcommands/subcommand_util.rb, line 22
def self.execute_subcommand?(arg0)
  return false if arg0.nil?

  (Beaker::Subcommand.instance_methods(false) << :help).include? arg0.to_sym
end
prune_unpersisted(options) click to toggle source
# File lib/beaker/subcommands/subcommand_util.rb, line 28
def self.prune_unpersisted(options)
  UNPERSISTED_OPTIONS.each do |unpersisted_key|
    options.each do |key, value|
      if key == unpersisted_key
        options.delete(key)
      elsif value.is_a?(Hash)
        options[key] = self.prune_unpersisted(value) unless value.empty?
      end
    end
  end
  options
end
sanitize_options_for_save(options) click to toggle source
# File lib/beaker/subcommands/subcommand_util.rb, line 41
def self.sanitize_options_for_save(options)
  # God help us, the YAML library won't stop adding tags to objects, so this
  # hack is a way to force the options into the basic object types so that
  # an eventual YAML.dump or .to_yaml call doesn't add tags.
  # Relevant stackoverflow: http://stackoverflow.com/questions/18178098/how-do-i-have-ruby-yaml-dump-a-hash-subclass-as-a-simple-hash
  JSON.parse(prune_unpersisted(options).to_json)
end
with_captured_output() { || ... } click to toggle source

Execute a task but capture stdout and stderr to a buffer

# File lib/beaker/subcommands/subcommand_util.rb, line 60
def self.with_captured_output
  begin
    old_stdout = $stdout.clone
    old_stderr = $stderr.clone
    $stdout = StringIO.new
    $stderr = StringIO.new
    yield
  ensure
    $stdout = old_stdout
    $stderr = old_stderr
  end
end