Class: Ukiryu::Execution::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/ukiryu/execution/output.rb

Overview

Captured output from command execution

Provides typed access to stdout and stderr with parsing utilities

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stderr:, exit_status:) ⇒ Output

Returns a new instance of Output.



11
12
13
14
15
# File 'lib/ukiryu/execution/output.rb', line 11

def initialize(stdout:, stderr:, exit_status:)
  @raw_stdout = stdout
  @raw_stderr = stderr
  @exit_status = exit_status
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



9
10
11
# File 'lib/ukiryu/execution/output.rb', line 9

def exit_status
  @exit_status
end

#raw_stderrObject (readonly)

Returns the value of attribute raw_stderr.



9
10
11
# File 'lib/ukiryu/execution/output.rb', line 9

def raw_stderr
  @raw_stderr
end

#raw_stdoutObject (readonly)

Returns the value of attribute raw_stdout.



9
10
11
# File 'lib/ukiryu/execution/output.rb', line 9

def raw_stdout
  @raw_stdout
end

Instance Method Details

#failure?Boolean

Check if command failed

Returns:

  • (Boolean)

    true if exit status is non-zero



107
108
109
# File 'lib/ukiryu/execution/output.rb', line 107

def failure?
  @exit_status != 0
end

#inspectString

Inspect

Returns:

  • (String)

    inspection string



139
140
141
# File 'lib/ukiryu/execution/output.rb', line 139

def inspect
  "#<Ukiryu::Execution::Output exit=#{@exit_status} success=#{success?}>"
end

#stderrString

Get stderr as a string (stripped)

Returns:

  • (String)

    stripped stderr



27
28
29
# File 'lib/ukiryu/execution/output.rb', line 27

def stderr
  @raw_stderr.strip
end

#stderr_contains?(pattern) ⇒ Boolean

Check if stderr contains a pattern

Parameters:

  • pattern (String, Regexp)

    pattern to search for

Returns:

  • (Boolean)

    true if pattern is found



61
62
63
64
65
66
67
# File 'lib/ukiryu/execution/output.rb', line 61

def stderr_contains?(pattern)
  if pattern.is_a?(Regexp)
    @raw_stderr.match?(pattern)
  else
    @raw_stderr.include?(pattern.to_s)
  end
end

#stderr_empty?Boolean

Check if stderr is empty

Returns:

  • (Boolean)

    true if stderr is empty



79
80
81
# File 'lib/ukiryu/execution/output.rb', line 79

def stderr_empty?
  @raw_stderr.strip.empty?
end

#stderr_lengthInteger

Get stderr length

Returns:

  • (Integer)

    byte length of stderr



93
94
95
# File 'lib/ukiryu/execution/output.rb', line 93

def stderr_length
  @raw_stderr.length
end

#stderr_linesArray<String>

Get stderr lines as an array

Returns:

  • (Array<String>)

    stderr split by lines



41
42
43
# File 'lib/ukiryu/execution/output.rb', line 41

def stderr_lines
  @raw_stderr.split(/\r?\n/)
end

#stdoutString

Get stdout as a string (stripped)

Returns:

  • (String)

    stripped stdout



20
21
22
# File 'lib/ukiryu/execution/output.rb', line 20

def stdout
  @raw_stdout.strip
end

#stdout_contains?(pattern) ⇒ Boolean

Check if stdout contains a pattern

Parameters:

  • pattern (String, Regexp)

    pattern to search for

Returns:

  • (Boolean)

    true if pattern is found



49
50
51
52
53
54
55
# File 'lib/ukiryu/execution/output.rb', line 49

def stdout_contains?(pattern)
  if pattern.is_a?(Regexp)
    @raw_stdout.match?(pattern)
  else
    @raw_stdout.include?(pattern.to_s)
  end
end

#stdout_empty?Boolean

Check if stdout is empty

Returns:

  • (Boolean)

    true if stdout is empty



72
73
74
# File 'lib/ukiryu/execution/output.rb', line 72

def stdout_empty?
  @raw_stdout.strip.empty?
end

#stdout_lengthInteger

Get stdout length

Returns:

  • (Integer)

    byte length of stdout



86
87
88
# File 'lib/ukiryu/execution/output.rb', line 86

def stdout_length
  @raw_stdout.length
end

#stdout_linesArray<String>

Get stdout lines as an array

Returns:

  • (Array<String>)

    stdout split by lines



34
35
36
# File 'lib/ukiryu/execution/output.rb', line 34

def stdout_lines
  @raw_stdout.split(/\r?\n/)
end

#success?Boolean

Check if command succeeded

Returns:

  • (Boolean)

    true if exit status is 0



100
101
102
# File 'lib/ukiryu/execution/output.rb', line 100

def success?
  @exit_status.zero?
end

#to_hHash

Convert to hash

Returns:

  • (Hash)

    output as hash



114
115
116
117
118
119
120
121
122
123
# File 'lib/ukiryu/execution/output.rb', line 114

def to_h
  {
    stdout: @raw_stdout,
    stderr: @raw_stderr,
    exit_status: @exit_status,
    success: success?,
    stdout_lines: stdout_lines,
    stderr_lines: stderr_lines
  }
end

#to_sString

String representation

Returns:

  • (String)

    summary string



128
129
130
131
132
133
134
# File 'lib/ukiryu/execution/output.rb', line 128

def to_s
  if success?
    "Success (exit: #{@exit_status}, stdout: #{stdout_length} bytes, stderr: #{stderr_length} bytes)"
  else
    "Failed (exit: #{@exit_status}, stdout: #{stdout_length} bytes, stderr: #{stderr_length} bytes)"
  end
end