Class: Litterbox::Command

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

Overview

CLI Command operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Command

Returns a new instance of Command



5
6
7
8
9
10
# File 'lib/litterbox/command.rb', line 5

def initialize(cmd)
  @cmd = cmd
  @cmd = "sudo #{@cmd}" if ENV['USE_SUDO']
  @stdout = ''
  @stderr = ''
end

Instance Attribute Details

#processObject

Returns the value of attribute process



4
5
6
# File 'lib/litterbox/command.rb', line 4

def process
  @process
end

#stderrObject

Returns the value of attribute stderr



4
5
6
# File 'lib/litterbox/command.rb', line 4

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout



4
5
6
# File 'lib/litterbox/command.rb', line 4

def stdout
  @stdout
end

Instance Method Details

#run_commandObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/litterbox/command.rb', line 12

def run_command
  Open3.popen3(@cmd) do |_, out, err, thr|
    while (line = err.gets)
      @stderr << line unless line.nil?
      puts(line)
    end
    while (lines = out.gets)
      @stdout << lines unless lines.nil?
      puts(lines)
    end
    @process = thr.value
    return @stdout, @stderr, @process
  end
end