Class: Lastpass::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/lastpass-api/utils.rb

Class Method Summary collapse

Class Method Details

.cmd(command, output: false) ⇒ Object

Run a command



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lastpass-api/utils.rb', line 8

def self.cmd( command, output: false )
  puts "RUN COMMAND:  #{command}".green if Lastpass.verbose
  @stdout = ''
  Open3::popen3( command ) do |stdin, stdout, stderr, wait_thr|
    stdout.sync = true
    while line = stdout.gets
      puts line if Lastpass.verbose || output
      @stdout << line
    end

    exit_status = wait_thr.value
    unless exit_status.success?
      puts "COMMAND:  #{command}".red if Lastpass.verbose
      stderr_text = stderr.read
      puts stderr_text.red if Lastpass.verbose
      raise StandardError, "Command: '#{command}', Stdout: '#{stdout.read}', Stderr: '#{stderr_text}'"
    end
  end

  return @stdout
rescue Errno::ENOENT => e
  puts "COMMAND:  #{command}".red if Lastpass.verbose
  puts "#{e}".red if Lastpass.verbose
  raise StandardError, "Command: '#{command}', Error: '#{e}'"
end