class ActionCommand::Result

The result of one or more commands being executed.

Attributes

last_error[R]

@return [String] the last string error message

result_code[R]

@return [Integer] the current result code

Public Class Methods

new() click to toggle source

By default, a command is ok?

# File lib/action_command/result.rb, line 8
def initialize
  @result_code = RESULT_CODE_OK
  @last_error  = nil
  @values = [{}]
  @logger = nil
end

Public Instance Methods

[](key) click to toggle source

Return a value return by the command

# File lib/action_command/result.rb, line 126
def [](key)
  return current[key]
end
[]=(key, val) click to toggle source

Assign some kind of a return value for use by the caller.

# File lib/action_command/result.rb, line 111
def []=(key, val)
  current[key] = val
end
configure_logger(logger, format) click to toggle source

set the logger for this result

# File lib/action_command/result.rb, line 16
def configure_logger(logger, format)
  return unless logger
  @sequence = SecureRandom.hex 
  @stack = [] 
  @logger = logger
  @log_format = format
end
current() click to toggle source

returns the current hash of values we are operating on.

# File lib/action_command/result.rb, line 106
def current
  return @values.last
end
debug(msg = nil) { || ... } click to toggle source

display an debugging message to the logger, if there is one. @yield return a message or hash

# File lib/action_command/result.rb, line 31
def debug(msg = nil)
  if @logger
    msg = build_log(msg || yield, ActionCommand::LOG_KIND_DEBUG)
    @logger.info(format_log(msg))
  end
end
error(msg) click to toggle source

display an error message to the logger, if there is one.

# File lib/action_command/result.rb, line 48
def error(msg)
  if @logger
    msg = build_log(msg, ActionCommand::LOG_KIND_ERROR)
    @logger.error(format_log(msg))
  end
end
failed(msg) click to toggle source

Call this if your command implementation fails. Sets ok? to false on the result. @param msg [String] message describing the failure.

# File lib/action_command/result.rb, line 58
def failed(msg)
  @result_code = RESULT_CODE_FAILED
  @last_error  = msg
  error(msg)
end
failed_with_code(msg, result_code) click to toggle source

Call this if your command implementation fails. Sets ok? to false on the result. @param msg [String] message describing the failure. @param result_code [Integer]

# File lib/action_command/result.rb, line 68
def failed_with_code(msg, result_code)
  @result_code = result_code
  @last_error = msg
  error(msg)
end
info(msg = nil) { || ... } click to toggle source

display an informational message to the logger, if there is one. @yield return a message or hash

# File lib/action_command/result.rb, line 40
def info(msg = nil)
  if @logger
    msg = build_log(msg || yield, ActionCommand::LOG_KIND_INFO)
    @logger.info(format_log(msg))
  end
end
key?(key) click to toggle source

determine if a key exists in the result.

# File lib/action_command/result.rb, line 121
def key?(key)
  return current.key?(key)
end
log_input(params) click to toggle source

Used internally to log the input parameters to a command

# File lib/action_command/result.rb, line 131
def log_input(params)
  return unless @logger
  output = params.reject { |k, _v| internal_key?(k) }
  log_info_hash(output, ActionCommand::LOG_KIND_COMMAND_INPUT)
end
log_output() click to toggle source

Used internally to log the output parameters for a command.

# File lib/action_command/result.rb, line 138
def log_output
  return unless @logger
  # only log the first level parameters, subcommands will log
  # their own output.
  output = current.reject { |k, v| v.is_a?(Hash) || internal_key?(k) }
  log_info_hash(output, ActionCommand::LOG_KIND_COMMAND_OUTPUT) 
end
logging?() click to toggle source

@return true if logging is enabled.

# File lib/action_command/result.rb, line 25
def logging?
  return !@logger.nil?
end
ok?() click to toggle source

@return [Boolean] true, up until failed has been called at least once.

# File lib/action_command/result.rb, line 75
def ok?
  return @result_code == RESULT_CODE_OK
end
pop(key) click to toggle source

removes the current set of results from the stack.

# File lib/action_command/result.rb, line 99
def pop(key)
  return unless key
  @values.pop
  @stack.pop if @logger
end
push(key, cmd) click to toggle source

adds results under the subkey until pop is called

# File lib/action_command/result.rb, line 86
def push(key, cmd)
  return unless key
  old_cur = current
  if old_cur.key?(key)
    @values << old_cur[key]
  else
    @values << {}
    old_cur[key] = @values.last
  end
  @stack << { key: key, cmd: cmd } if @logger
end
root_command(cls) click to toggle source

Used internally to establish the class of the root command

# File lib/action_command/result.rb, line 147
def root_command(cls)
  @stack << { key: nil, cmd: cls }  if @logger
end
sequence() click to toggle source

return the unique sequence id for the commands under this result

# File lib/action_command/result.rb, line 116
def sequence
  return @sequence
end

Private Instance Methods

build_log(msg, kind) click to toggle source
# File lib/action_command/result.rb, line 164
def build_log(msg, kind)
  cur = @stack.last
  out = { 
    sequence: @sequence,
    cmd: cur[:cmd].name,
    depth: @stack.length - 1
  }
  out[:key] = cur[:key] if cur[:key]
  out[:kind] = kind
  out[:msg] = msg if msg
  return out
end
format_human_log(depth, cmd, kind, msg) click to toggle source
# File lib/action_command/result.rb, line 187
def format_human_log(depth, cmd, kind, msg)
  base_depth = 2 * depth
  extra_depth = base_depth + 2
  if kind == ActionCommand::LOG_KIND_COMMAND_INPUT
    cmd = "#{cmd}: #{msg}"
    return cmd.rjust(cmd.length + base_depth)
  elsif kind == ActionCommand::LOG_KIND_COMMAND_OUTPUT
    out = "output: #{msg}"
    return out.rjust(out.length + extra_depth)
  elsif msg
    return msg.rjust(msg.to_s.length + extra_depth)
  end
end
format_log(msg) click to toggle source
# File lib/action_command/result.rb, line 177
def format_log(msg)
  return JSON.generate(msg) unless @log_format == :human

  depth = msg[:depth]
  kind = msg[:kind]
  message = msg[:msg]
  cmd = msg[:cmd]
  return format_human_log(depth, cmd, kind, message)
end
internal_key?(k) click to toggle source
# File lib/action_command/result.rb, line 154
def internal_key?(k)
  return k == :logger || k == :test || k == :parent
end
log_info_hash(params, kind) click to toggle source
# File lib/action_command/result.rb, line 158
def log_info_hash(params, kind)
  return unless @logger
  msg = build_log(params, kind)
  @logger.info(format_log(msg))
end