Module: Beaker::Subcommands::SubcommandUtil

Defined in:
lib/beaker/subcommands/subcommand_util.rb

Overview

Methods used in execution of Subcommands

  • should we execute a subcommand?

  • sanitize options for saving as json

  • exit with a specific message

  • capture stdout and stderr

Constant Summary collapse

CONFIG_DIR =
".beaker"
SUBCOMMAND_OPTIONS =
Pathname("#{CONFIG_DIR}/subcommand_options.yaml")
SUBCOMMAND_STATE =
Pathname("#{CONFIG_DIR}/.subcommand_state.yaml")
PERSISTED_HOSTS =
Pathname("#{CONFIG_DIR}/.hosts.yaml")
PERSISTED_HYPERVISORS =
Pathname("#{CONFIG_DIR}/.hypervisors.yaml")
UNPERSISTED_OPTIONS =

These options should not be part of persisted subcommand state

%i[beaker_version command_line hosts_file logger password_prompt timestamp]

Class Method Summary collapse

Class Method Details

.error_with(msg, options = {}) ⇒ Object

Print a message to the console and exit with specified exit code, defaults to 1  @param [String] msg the message to output

Parameters:

  • options (Hash<Object>) (defaults to: {})

    to specify exit code or output stack trace



52
53
54
55
56
57
# 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) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# 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) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
45
46
47
# 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_outputObject

Execute a task but capture stdout and stderr to a buffer



60
61
62
63
64
65
66
67
68
69
70
71
# 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