Class: Sevgi::Function::Shell::Result

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

Overview

Immutable result returned by shell commands.

Examples:

Inspect a command result

require "rbconfig"
result = Sevgi::F.sh(RbConfig.ruby, "-e", "puts 42")
result.ok?     # => true
result.outline # => "42"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args:, outs:, errs:, exit_code:, signal:) ⇒ void

Creates an owned result snapshot.

Parameters:

  • args (Array<Object>)

    command arguments

  • outs (Array<String>)

    captured stdout lines

  • errs (Array<String>)

    captured stderr lines

  • exit_code (Integer, nil)

    process exit code

  • signal (Integer, nil)

    terminating signal number



61
62
63
64
65
66
67
68
69
# File 'lib/sevgi/function/shell.rb', line 61

def initialize(args:, outs:, errs:, exit_code:, signal:)
  super(
    args: args.map { it.to_s.dup.freeze }.freeze,
    outs: outs.map { it.dup.freeze }.freeze,
    errs: errs.map { it.dup.freeze }.freeze,
    exit_code:,
    signal:
  )
end

Instance Attribute Details

#argsArray<String> (readonly)

Returns frozen command arguments.

Returns:

  • (Array<String>)

    frozen command arguments



42
43
44
# File 'lib/sevgi/function/shell.rb', line 42

def args
  @args
end

#errsArray<String> (readonly)

Returns frozen captured stderr lines.

Returns:

  • (Array<String>)

    frozen captured stderr lines



42
43
44
# File 'lib/sevgi/function/shell.rb', line 42

def errs
  @errs
end

#exit_codeInteger? (readonly)

Returns process exit code.

Returns:

  • (Integer, nil)

    process exit code



42
43
44
# File 'lib/sevgi/function/shell.rb', line 42

def exit_code
  @exit_code
end

#outsArray<String> (readonly)

Returns frozen captured stdout lines.

Returns:

  • (Array<String>)

    frozen captured stdout lines



42
43
44
# File 'lib/sevgi/function/shell.rb', line 42

def outs
  @outs
end

#signalInteger? (readonly)

Returns terminating signal number.

Returns:

  • (Integer, nil)

    terminating signal number



42
43
44
# File 'lib/sevgi/function/shell.rb', line 42

def signal
  @signal
end

Instance Method Details

#allString

Returns captured output with one blank line between non-empty streams. Captured lines are joined with one newline and are not otherwise trimmed.

Examples:

Combine standard output and standard error

result = Sevgi::Function::Shell::Result.new(
  args: ["tool"], outs: ["output"], errs: ["warning"], exit_code: 0, signal: nil
)
result.all # => "output\n\nwarning"

Returns:

  • (String)

    combined output, or an empty string when neither stream contains lines



81
82
83
84
85
86
# File 'lib/sevgi/function/shell.rb', line 81

def all
  return err if outs.empty?
  return out if errs.empty?

  "#{out}\n\n#{err}"
end

#cmdString

Returns the command as a shell-like display string.

Returns:



90
# File 'lib/sevgi/function/shell.rb', line 90

def cmd = ::Shellwords.join(args)

#errString

Returns captured stderr as a string.

Returns:



94
# File 'lib/sevgi/function/shell.rb', line 94

def err = errs.join("\n")

#notok?Boolean

Reports whether the command failed.

Returns:

  • (Boolean)


98
# File 'lib/sevgi/function/shell.rb', line 98

def notok? = !ok?

#ok?Boolean

Reports whether the command exited successfully.

Returns:

  • (Boolean)


102
# File 'lib/sevgi/function/shell.rb', line 102

def ok? = !exit_code.nil? && exit_code.zero?

#outString

Returns captured stdout as a string.

Returns:



106
# File 'lib/sevgi/function/shell.rb', line 106

def out = outs.join("\n")

#outlineString?

Returns the first stdout line.

Returns:



110
# File 'lib/sevgi/function/shell.rb', line 110

def outline = outs.first

#signaled?Boolean

Reports whether the command was terminated by a signal.

Returns:

  • (Boolean)


114
# File 'lib/sevgi/function/shell.rb', line 114

def signaled? = !signal.nil?