Class: Bsdkrun::Result

Inherits:
Data
  • Object
show all
Defined in:
lib/bsdkrun/types.rb

Overview

The captured result of running a command in a guest via Sandbox#exec.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandString (readonly)

Returns a human label for the command.

Returns:

  • (String)

    a human label for the command.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bsdkrun/types.rb', line 111

Result = Data.define(:stdout, :stderr, :exit_code, :command) do
  # @return [Boolean] whether the command succeeded (exit 0).
  def ok?
    exit_code.zero?
  end

  # @return [String] stdout with trailing newlines trimmed — the common case.
  def text
    stdout.sub(/\n+\z/, "")
  end

  # @return [Object] stdout parsed as JSON.
  def json
    require "json"
    JSON.parse(stdout)
  end

  # @return [Array<String>] non-empty stdout lines.
  def lines
    stdout.split("\n").reject(&:empty?)
  end

  # @raise [CommandFailed] if the command exited non-zero.
  # @return [self]
  def throw_if_failed!
    unless ok?
      raise CommandFailed.new(
        exit_code: exit_code, stdout: stdout, stderr: stderr, command: command
      )
    end
    self
  end
end

#exit_codeInteger (readonly)

Returns:

  • (Integer)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bsdkrun/types.rb', line 111

Result = Data.define(:stdout, :stderr, :exit_code, :command) do
  # @return [Boolean] whether the command succeeded (exit 0).
  def ok?
    exit_code.zero?
  end

  # @return [String] stdout with trailing newlines trimmed — the common case.
  def text
    stdout.sub(/\n+\z/, "")
  end

  # @return [Object] stdout parsed as JSON.
  def json
    require "json"
    JSON.parse(stdout)
  end

  # @return [Array<String>] non-empty stdout lines.
  def lines
    stdout.split("\n").reject(&:empty?)
  end

  # @raise [CommandFailed] if the command exited non-zero.
  # @return [self]
  def throw_if_failed!
    unless ok?
      raise CommandFailed.new(
        exit_code: exit_code, stdout: stdout, stderr: stderr, command: command
      )
    end
    self
  end
end

#stderrString (readonly)

Returns:

  • (String)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bsdkrun/types.rb', line 111

Result = Data.define(:stdout, :stderr, :exit_code, :command) do
  # @return [Boolean] whether the command succeeded (exit 0).
  def ok?
    exit_code.zero?
  end

  # @return [String] stdout with trailing newlines trimmed — the common case.
  def text
    stdout.sub(/\n+\z/, "")
  end

  # @return [Object] stdout parsed as JSON.
  def json
    require "json"
    JSON.parse(stdout)
  end

  # @return [Array<String>] non-empty stdout lines.
  def lines
    stdout.split("\n").reject(&:empty?)
  end

  # @raise [CommandFailed] if the command exited non-zero.
  # @return [self]
  def throw_if_failed!
    unless ok?
      raise CommandFailed.new(
        exit_code: exit_code, stdout: stdout, stderr: stderr, command: command
      )
    end
    self
  end
end

#stdoutString (readonly)

Returns:

  • (String)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bsdkrun/types.rb', line 111

Result = Data.define(:stdout, :stderr, :exit_code, :command) do
  # @return [Boolean] whether the command succeeded (exit 0).
  def ok?
    exit_code.zero?
  end

  # @return [String] stdout with trailing newlines trimmed — the common case.
  def text
    stdout.sub(/\n+\z/, "")
  end

  # @return [Object] stdout parsed as JSON.
  def json
    require "json"
    JSON.parse(stdout)
  end

  # @return [Array<String>] non-empty stdout lines.
  def lines
    stdout.split("\n").reject(&:empty?)
  end

  # @raise [CommandFailed] if the command exited non-zero.
  # @return [self]
  def throw_if_failed!
    unless ok?
      raise CommandFailed.new(
        exit_code: exit_code, stdout: stdout, stderr: stderr, command: command
      )
    end
    self
  end
end

Instance Method Details

#jsonObject

Returns stdout parsed as JSON.

Returns:

  • (Object)

    stdout parsed as JSON.



123
124
125
126
# File 'lib/bsdkrun/types.rb', line 123

def json
  require "json"
  JSON.parse(stdout)
end

#linesArray<String>

Returns non-empty stdout lines.

Returns:

  • (Array<String>)

    non-empty stdout lines.



129
130
131
# File 'lib/bsdkrun/types.rb', line 129

def lines
  stdout.split("\n").reject(&:empty?)
end

#ok?Boolean

Returns whether the command succeeded (exit 0).

Returns:

  • (Boolean)

    whether the command succeeded (exit 0).



113
114
115
# File 'lib/bsdkrun/types.rb', line 113

def ok?
  exit_code.zero?
end

#textString

Returns stdout with trailing newlines trimmed — the common case.

Returns:

  • (String)

    stdout with trailing newlines trimmed — the common case.



118
119
120
# File 'lib/bsdkrun/types.rb', line 118

def text
  stdout.sub(/\n+\z/, "")
end

#throw_if_failed!self

Returns:

  • (self)

Raises:



135
136
137
138
139
140
141
142
# File 'lib/bsdkrun/types.rb', line 135

def throw_if_failed!
  unless ok?
    raise CommandFailed.new(
      exit_code: exit_code, stdout: stdout, stderr: stderr, command: command
    )
  end
  self
end