class ActionCommand::Result
The result of one or more commands being executed.
Attributes
@return [String] the last string error message
@return [Integer] the current result code
Public Class Methods
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
Return a value return by the command
# File lib/action_command/result.rb, line 126 def [](key) return current[key] end
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
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
returns the current hash of values we are operating on.
# File lib/action_command/result.rb, line 106 def current return @values.last end
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
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
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
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
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
determine if a key exists in the result.
# File lib/action_command/result.rb, line 121 def key?(key) return current.key?(key) end
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
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
@return true if logging is enabled.
# File lib/action_command/result.rb, line 25 def logging? return !@logger.nil? end
@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
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
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
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
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
# 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
# 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
# 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
# File lib/action_command/result.rb, line 154 def internal_key?(k) return k == :logger || k == :test || k == :parent end
# 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