Class: ICSP::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/shell.rb

Overview

Shell

Instance Method Summary collapse

Constructor Details

#initialize(command, split: true, convert_to_utf8: true, fork: true) ⇒ Shell

Returns a new instance of Shell.



6
7
8
9
10
11
# File 'lib/shell.rb', line 6

def initialize(command, split: true, convert_to_utf8: true, fork: true)
  @command = command
  @split = split
  @convert_to_utf8 = convert_to_utf8
  @fork = fork
end

Instance Method Details

#executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shell.rb', line 13

def execute
  ::ICSP.logger.info("command: #{@command}")
  unless @fork
    exec(@command)
    return
  end

  raw_output = `#{@command}`
  exec_success = $CHILD_STATUS.success?
  exit_code = $CHILD_STATUS.exitstatus

  output = @convert_to_utf8 ? raw_output.force_encoding('cp1251').encode('utf-8', undef: :replace) : raw_output
  csp_error_code = ''
  output_lines = []

  if @split
    output_lines = output.split("\n")
    csp_error_code_line = output_lines.find { |l| l.include?('ErrorCode') }
    csp_error_code = /0x0*[1-9a-fA-F][0-9a-fA-F]*/.match(csp_error_code_line).to_s
  end

  shell_result = ::ICSP::ShellResult.new(
    output: output,
    output_lines: output_lines,
    csp_error_code: csp_error_code,
    ok: exec_success && csp_error_code == '',
    exit_code: exit_code
  )

  ::ICSP.logger.debug("ok: #{shell_result.ok}")
  shell_result
end