Class: ShellResult

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

Overview

This class holds the results from an executed shell command.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out, err) ⇒ ShellResult

Constructor

Input

out : String

The output from stdout.

err : String

The output from stderr.

Output

ShellResult

A new ShellResult object.



43
44
45
46
# File 'lib/execute_shell/shell_result.rb', line 43

def initialize(out, err)
  @out = out
  @err = err
end

Instance Attribute Details

#errObject (readonly)

Returns the value of attribute err.



35
36
37
# File 'lib/execute_shell/shell_result.rb', line 35

def err
  @err
end

#outObject (readonly)

Returns the value of attribute out.



35
36
37
# File 'lib/execute_shell/shell_result.rb', line 35

def out
  @out
end

Class Method Details

.cleanup(command, path, out) ⇒ Object

Cleans up the output string, if necessary.

Input

command : String

The command that was called.

path : String

The current directory when the command was run.

out : String

The output (on stdout) from the command.

Output

String

A string that has been cleaned up.

Notes

This is primarily for Windows since cmd adds a bunch of garbage to the output.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/execute_shell/shell_result.rb', line 81

def self.cleanup(command, path, out)
  case Platform::IMPL
    when :mingw
      # Remove all the extra stuff that the cmd prompt adds.
      out.gsub!(/\n\n#{path.gsub(%r[/], '\\\\\\')}>\Z/, '')

      # replace contains the command line prompt
      # and the command up to the first space.
      replace = path.gsub(%r[/], '\\\\\\')
      replace += '>'
      replace += command[0..(command.index(/ /) || 0) - 1]

      # Remove the header portion of the text.
      # This includes the Microsoft 'banner' text
      # that consumes the first two lines.
      out = out.gsub(/\A.+#{replace}.*?$/m, '').strip
  end

  return out
end

Instance Method Details

#success?Boolean

Indicates whether the command was executed successfully.

The output is based on the contents of the err attribute.

Output

Boolean

Indicates whether the command was successful.

Examples

ShellResult.new('a', nil).success?     #=> true
ShellResult.new('a', '').success?      #=> true
ShellResult.new('a', ' ').success?     #=> false
ShellResult.new('a', 'uh-oh').success? #=> false

Returns:

  • (Boolean)


58
59
60
# File 'lib/execute_shell/shell_result.rb', line 58

def success?
  @err.nil? || @err.empty?
end

#to_sObject

Combines the output and error strings.

Output

String

Returns the output and error strings concatenated.

Examples

ShellResult.new('a', 'b').to_s #=> "a\nb"


67
68
69
# File 'lib/execute_shell/shell_result.rb', line 67

def to_s
  "#{@out}\n#{@err}".strip
end