Created
April 29, 2019 14:36
-
-
Save amitpatelx/0261f9904d96739513a045e84705f752 to your computer and use it in GitHub Desktop.
Execute shell command and read command output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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