class FancyCommand::Command
Attributes
accum[R]
err[R]
in[R]
out[R]
output[R]
pid[R]
status[R]
string[R]
Public Class Methods
new(string, **opts) { || ... }
click to toggle source
# File lib/fancy_command/command.rb, line 19 def initialize(string, **opts) opts = FancyCommand.defaults.merge(opts) @string = string @verbose = opts.fetch(:verbose, false) @must_succeed = opts.fetch(:must_succeed, false) @accum = opts.fetch(:accum) { [] } @in = opts[:in] @output = "" @output_mutex = Mutex.new @out = nil @err = nil @status = nil @pid = nil if block_given? append_in yield end end
Public Instance Methods
append_in(string)
click to toggle source
# File lib/fancy_command/command.rb, line 39 def append_in(string) @in ||= "" @in << string self end
call()
click to toggle source
# File lib/fancy_command/command.rb, line 53 def call puts "$ #{string}" if verbose? @in.freeze Open3.popen3(string) do |i, o, e, t| unless @in.nil? i.print @in i.close end @out, @err = [o, e].map do |stream| Thread.new do lines = [] until (line = stream.gets).nil? do lines << line @output_mutex.synchronize do @accum << line @output << line end end lines.join end end.map(&:value) @pid = t.pid @status = t.value end [@out, @err, @output].each(&:freeze) if must_succeed? && !success? raise Failed, command: string, status: status.exitstatus, output: output end self end
exitstatus()
click to toggle source
# File lib/fancy_command/command.rb, line 95 def exitstatus status.exitstatus end
if_success_then(&blk)
click to toggle source
# File lib/fancy_command/command.rb, line 109 def if_success_then(&blk) if success? if blk.arity == 1 blk.call(self) else blk.call end end self end
must_succeed?()
click to toggle source
# File lib/fancy_command/command.rb, line 45 def must_succeed? !!@must_succeed end
pipe(command_or_string)
click to toggle source
# File lib/fancy_command/command.rb, line 133 def pipe(command_or_string) command = if String === command_or_string self.class.new(command_or_string, accum: accum, verbose: verbose?) else command_or_string end command.append_in(output).() end
Also aliased as: |
success?()
click to toggle source
# File lib/fancy_command/command.rb, line 91 def success? status.success? end
then(&blk)
click to toggle source
# File lib/fancy_command/command.rb, line 99 def then(&blk) if blk.arity == 1 blk.call(self) else blk.call end self end
unless_success_then(&blk)
click to toggle source
# File lib/fancy_command/command.rb, line 121 def unless_success_then(&blk) unless success? if blk.arity == 1 blk.call(self) else blk.call end end self end
verbose?()
click to toggle source
# File lib/fancy_command/command.rb, line 49 def verbose? !!@verbose || ENV["VERBOSE"] end