Class: FancyCommand::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/fancy_command/command.rb

Defined Under Namespace

Classes: Failed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, must_succeed: false, **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
# File 'lib/fancy_command/command.rb', line 19

def initialize(string, must_succeed: false, **opts)
  @string = string
  @verbose = opts.fetch(:verbose, false)
  @accum = opts.fetch(:accum) { [] }
  @in = opts[:in]
  @output = ""
  @out = nil
  @err = nil
  @status = nil
  @pid = nil
  @must_succeed = must_succeed
  @output_mutex = Mutex.new

  if block_given?
    append_in yield
  end
end

Instance Attribute Details

#accumObject (readonly)

Returns the value of attribute accum.



17
18
19
# File 'lib/fancy_command/command.rb', line 17

def accum
  @accum
end

#errObject (readonly)

Returns the value of attribute err.



17
18
19
# File 'lib/fancy_command/command.rb', line 17

def err
  @err
end

#inObject (readonly)

Returns the value of attribute in.



17
18
19
# File 'lib/fancy_command/command.rb', line 17

def in
  @in
end

#outObject (readonly)

Returns the value of attribute out.



17
18
19
# File 'lib/fancy_command/command.rb', line 17

def out
  @out
end

#outputObject (readonly)

Returns the value of attribute output.



17
18
19
# File 'lib/fancy_command/command.rb', line 17

def output
  @output
end

#pidObject (readonly)

Returns the value of attribute pid.



17
18
19
# File 'lib/fancy_command/command.rb', line 17

def pid
  @pid
end

#statusObject (readonly)

Returns the value of attribute status.



17
18
19
# File 'lib/fancy_command/command.rb', line 17

def status
  @status
end

#stringObject (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



37
38
39
40
41
# File 'lib/fancy_command/command.rb', line 37

def append_in(string)
  @in ||= ""
  @in << string
  self
end

#callObject



51
52
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
# File 'lib/fancy_command/command.rb', line 51

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

#exitstatusObject



93
94
95
# File 'lib/fancy_command/command.rb', line 93

def exitstatus
  status.exitstatus
end

#if_success_then(&blk) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fancy_command/command.rb', line 107

def if_success_then(&blk)
  if success?
    if blk.arity == 1
      blk.call(self)
    else
      blk.call
    end
  end

  self
end

#must_succeed?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/fancy_command/command.rb', line 43

def must_succeed?
  !!@must_succeed
end

#pipe(command_or_string) ⇒ Object Also known as: |



131
132
133
134
135
136
137
138
139
# File 'lib/fancy_command/command.rb', line 131

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/fancy_command/command.rb', line 89

def success?
  status.success?
end

#then(&blk) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/fancy_command/command.rb', line 97

def then(&blk)
  if blk.arity == 1
    blk.call(self)
  else
    blk.call
  end

  self
end

#unless_success_then(&blk) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fancy_command/command.rb', line 119

def unless_success_then(&blk)
  unless success?
    if blk.arity == 1
      blk.call(self)
    else
      blk.call
    end
  end

  self
end

#verbose?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/fancy_command/command.rb', line 47

def verbose?
  !!@verbose || ENV["VERBOSE"]
end