Class: Command::Pipe
- Inherits:
-
Object
- Object
- Command::Pipe
- Defined in:
- lib/local/command.rb
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Remaining output not consumed by #get/#read or through #reader or #error.
Instance Method Summary collapse
- #error ⇒ Object
-
#initialize(cmd, stderr: nil, fail: true) ⇒ Pipe
constructor
A new instance of Pipe.
- #reader ⇒ Object
- #wait ⇒ Object
- #writer ⇒ Object
Constructor Details
#initialize(cmd, stderr: nil, fail: true) ⇒ Pipe
Returns a new instance of Pipe.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/local/command.rb', line 40 def initialize(cmd, stderr: nil, fail: true) @cmd = "set -o errexit\nset -o pipefail\n#{cmd}" @stderr = stderr @fail = fail @pw = IO::pipe # pipe[0] for read, pipe[1] for write @pr = IO::pipe @pe = IO::pipe STDOUT.flush @pid = fork { @pw[1].close @pr[0].close @pe[0].close STDIN.reopen(@pw[0]) @pw[0].close STDOUT.reopen(@pr[1]) @pr[1].close STDERR.reopen(@pe[1]) @pe[1].close exec(cmd) } @pw[0].close @pr[1].close @pe[1].close end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
26 27 28 |
# File 'lib/local/command.rb', line 26 def cmd @cmd end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
27 28 29 |
# File 'lib/local/command.rb', line 27 def status @status end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
38 39 40 |
# File 'lib/local/command.rb', line 38 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Remaining output not consumed by #get/#read or through #reader or #error
37 38 39 |
# File 'lib/local/command.rb', line 37 def stdout @stdout end |
Instance Method Details
#error ⇒ Object
31 |
# File 'lib/local/command.rb', line 31 def error() @pe[0] end |
#reader ⇒ Object
30 |
# File 'lib/local/command.rb', line 30 def reader() @pr[0] end |
#wait ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/local/command.rb', line 73 def wait @pw[1].close @status = Process.waitpid2(@pid)[1].exitstatus out = @pr[0].readlines.map(&:chomp) err = @pe[0].readlines.map(&:chomp) @pr[0].close @pe[0].close raise Command::Error.new(@cmd, @status, nil, out, err) if @status != 0 && @fail == true case @stderr when true; [out, err] when false; out when nil $stderr.puts err out end end |
#writer ⇒ Object
29 |
# File 'lib/local/command.rb', line 29 def writer() @pw[1] end |