Class: Baps::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Command

Returns a new instance of Command.



7
8
9
10
# File 'lib/baps/command.rb', line 7

def initialize(command)
  @command = command
  @stdout, @stderr = String.new, String.new
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



5
6
7
# File 'lib/baps/command.rb', line 5

def command
  @command
end

#exit_statusObject

Returns the value of attribute exit_status.



5
6
7
# File 'lib/baps/command.rb', line 5

def exit_status
  @exit_status
end

#stderrObject (readonly)

Returns the value of attribute stderr.



5
6
7
# File 'lib/baps/command.rb', line 5

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/baps/command.rb', line 5

def stdout
  @stdout
end

Instance Method Details

#executeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/baps/command.rb', line 31

def execute
  puts "Executing: #{command}"
  Open3.popen3(command) do |_stdin, stdout, stderr, wait_thr|
    stdout_thread = Thread.new do
      while (line = stdout.gets) do
        on_stdout(line)
      end
    end

    stderr_thread = Thread.new do
      while (line = stderr.gets) do
        on_stderr(line)
      end
    end

    stdout_thread.join
    stderr_thread.join

    self.exit_status = wait_thr.value.to_i
  end
end

#on_stderr(data) ⇒ Object



16
17
18
# File 'lib/baps/command.rb', line 16

def on_stderr(data)
  @stderr += data
end

#on_stdout(data) ⇒ Object



12
13
14
# File 'lib/baps/command.rb', line 12

def on_stdout(data)
  @stdout += data
end