Class: FancyCommand::Command
- Inherits:
-
Object
- Object
- FancyCommand::Command
- Defined in:
- lib/fancy_command/command.rb
Defined Under Namespace
Classes: Failed
Instance Attribute Summary collapse
-
#accum ⇒ Object
readonly
Returns the value of attribute accum.
-
#err ⇒ Object
readonly
Returns the value of attribute err.
-
#in ⇒ Object
readonly
Returns the value of attribute in.
-
#out ⇒ Object
readonly
Returns the value of attribute out.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
- #append_in(string) ⇒ Object
- #call ⇒ Object
- #exitstatus ⇒ Object
- #if_success_then(&blk) ⇒ Object
-
#initialize(string, **opts) ⇒ Command
constructor
A new instance of Command.
- #must_succeed? ⇒ Boolean
- #pipe(command_or_string) ⇒ Object (also: #|)
- #success? ⇒ Boolean
- #then(&blk) ⇒ Object
- #unless_success_then(&blk) ⇒ Object
- #verbose? ⇒ Boolean
Constructor Details
#initialize(string, **opts) ⇒ Command
Returns a new instance of Command.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Attribute Details
#accum ⇒ Object (readonly)
Returns the value of attribute accum.
17 18 19 |
# File 'lib/fancy_command/command.rb', line 17 def accum @accum end |
#err ⇒ Object (readonly)
Returns the value of attribute err.
17 18 19 |
# File 'lib/fancy_command/command.rb', line 17 def err @err end |
#in ⇒ Object (readonly)
Returns the value of attribute in.
17 18 19 |
# File 'lib/fancy_command/command.rb', line 17 def in @in end |
#out ⇒ Object (readonly)
Returns the value of attribute out.
17 18 19 |
# File 'lib/fancy_command/command.rb', line 17 def out @out end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
17 18 19 |
# File 'lib/fancy_command/command.rb', line 17 def output @output end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
17 18 19 |
# File 'lib/fancy_command/command.rb', line 17 def pid @pid end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
17 18 19 |
# File 'lib/fancy_command/command.rb', line 17 def status @status end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
17 18 19 |
# File 'lib/fancy_command/command.rb', line 17 def string @string end |
Instance Method Details
#append_in(string) ⇒ Object
39 40 41 42 43 |
# File 'lib/fancy_command/command.rb', line 39 def append_in(string) @in ||= "" @in << string self end |
#call ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# 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 ⇒ Object
95 96 97 |
# File 'lib/fancy_command/command.rb', line 95 def exitstatus status.exitstatus end |
#if_success_then(&blk) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# 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? ⇒ Boolean
45 46 47 |
# File 'lib/fancy_command/command.rb', line 45 def must_succeed? !!@must_succeed end |
#pipe(command_or_string) ⇒ Object Also known as: |
133 134 135 136 137 138 139 140 141 |
# 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 |
#success? ⇒ Boolean
91 92 93 |
# File 'lib/fancy_command/command.rb', line 91 def success? status.success? end |
#then(&blk) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# 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? ⇒ Boolean
49 50 51 |
# File 'lib/fancy_command/command.rb', line 49 def verbose? !!@verbose || ENV["VERBOSE"] end |