Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Created April 29, 2019 14:36
Show Gist options
  • Save amitpatelx/0261f9904d96739513a045e84705f752 to your computer and use it in GitHub Desktop.
Save amitpatelx/0261f9904d96739513a045e84705f752 to your computer and use it in GitHub Desktop.
Execute shell command and read command output
require 'open3'
module CommandExecutable
attr_accessor :error, :output, :command_status
SPLIT_BY_NEW_LINE_REGEX = /\r?\n/
def execute_shell_command(command)
puts "+-+-+-+-+-+ Executing >>>>>>>> $ #{command.join(' ')}"
Open3.popen3(*command) do |stdin, stdout, stderr, wait_thr|
self.error = stderr.read
self.output = stdout.read
#Extract error/warning lines
self.error = error.split(SPLIT_BY_NEW_LINE_REGEX).select { |line| line =~ /Error/i }.join('\n') if error && !error.empty?
if error && !error.empty?
puts "[ERROR] #{self.class} stderror : #{error}"
else
self.command_status = true
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment