class NliPipeline::SystemWrapper::CallWrapper

wrapper for system calls handles output / debugging

Attributes

last_return_code[RW]

Public Class Methods

new(**kwargs) click to toggle source

@see NliPipeline::AbstractUtil#init_with_attrs handles everything

# File lib/nli_pipeline/system_wrapper/call_wrapper.rb, line 32
def initialize(**kwargs)
  init_with_attrs(**kwargs)
end
required_args() click to toggle source

no required args in this case, but method is required @see NliPipeline::AbstractUtil#init_with_attrs @see NliPipeline::AbstractUtil::ClassMethods#required_args? @return [Array]

# File lib/nli_pipeline/system_wrapper/call_wrapper.rb, line 27
def self.required_args
  []
end
supported_args() click to toggle source

static methods required by NliPipeline::AbstractUtil::init_attrs @see NliPipeline::AbstractUtil#init_with_attrs @see NliPipeline::AbstractUtil#get_allowed_args @return [Hash]

# File lib/nli_pipeline/system_wrapper/call_wrapper.rb, line 19
def self.supported_args
  { debug: false, fail_on_error: false }
end

Public Instance Methods

call_system(command, custom_error_message: false, return_output: false) click to toggle source

@param command [String] command to run @param custom_error_message [String] custom error to display on error @param return_output [Boolean] if false return code, if true return stdout @return [String | Boolean]

# File lib/nli_pipeline/system_wrapper/call_wrapper.rb, line 40
def call_system(command, custom_error_message: false, return_output: false)
  result = return_output ? `#{command}`.chomp : system(command)
  return_code = $CHILD_STATUS
  if @debug
    puts("\t#{command}")
    puts("\treturned: #{result} #{return_code}")
  end
  # can't return both output and code in return_output case
  # so assign instance var
  @last_return_code = return_code
  # only catches cases where command finished but with non-zero exit code
  # e.g. "echo 'hi" will throw an exception regardless of @fail_on_error
  # "echo 'hi' grep | 'y'" will throw a CallWrapperError only if @fail_on_error is true
  if @fail_on_error && !result
    puts(custom_error_message.red) if custom_error_message
    raise CallWrapperError.new(call: command, code: return_code)
  end
  result
end

Private Instance Methods

pretty_print(custom_message: false) { || ... } click to toggle source

pretty print intended for use with @see call_system @param custom_message [String]

# File lib/nli_pipeline/system_wrapper/call_wrapper.rb, line 88
def pretty_print(custom_message: false)
  # assign return value of block to ret
  ret = yield
  out_str = ret.to_s

  message_args = { custom_message: custom_message, out_str: out_str }
  out_str = format('%<custom_message>-20s: %<out_str>s', message_args) if custom_message
  puts(ret ? out_str.green : out_str.red)
  ret
end