Class: NliPipeline::SystemWrapper::CallWrapper

Inherits:
Object
  • Object
show all
Includes:
AbstractUtil
Defined in:
lib/nli_pipeline/system_wrapper/call_wrapper.rb

Overview

wrapper for system calls handles output / debugging

Direct Known Subclasses

DockerManager, FileManager, GitManager

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AbstractUtil

#add_attrs, #drop_forbidden_args_message, included, #init_with_attrs, #raise_unless_all, #to_s

Constructor Details

#initialize(**kwargs) ⇒ CallWrapper

Returns a new instance of CallWrapper.

See Also:



32
33
34
# File 'lib/nli_pipeline/system_wrapper/call_wrapper.rb', line 32

def initialize(**kwargs)
  init_with_attrs(**kwargs)
end

Instance Attribute Details

#last_return_codeObject

Returns the value of attribute last_return_code.



13
14
15
# File 'lib/nli_pipeline/system_wrapper/call_wrapper.rb', line 13

def last_return_code
  @last_return_code
end

Class Method Details

.required_argsArray

no required args in this case, but method is required



27
28
29
# File 'lib/nli_pipeline/system_wrapper/call_wrapper.rb', line 27

def self.required_args
  []
end

.supported_argsHash

static methods required by NliPipeline::AbstractUtil::init_attrs

Returns:

  • (Hash)

See Also:



19
20
21
# File 'lib/nli_pipeline/system_wrapper/call_wrapper.rb', line 19

def self.supported_args
  { debug: false, fail_on_error: false }
end

Instance Method Details

#call_system(command, custom_error_message: false, return_output: false) ⇒ String | Boolean

Parameters:

  • command (String)

    command to run

  • custom_error_message (String) (defaults to: false)

    custom error to display on error

  • return_output (Boolean) (defaults to: false)

    if false return code, if true return stdout

Returns:

  • (String | Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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