Created
November 21, 2013 06:13
-
-
Save s-tajima/7576824 to your computer and use it in GitHub Desktop.
Method for realtime output of external command.
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
def exec_command(command, dry_run = false) | |
if dry_run | |
puts command | |
return | |
end | |
begin | |
PTY.spawn(command) do |stdin, stdout, pid| | |
begin | |
line = '' | |
stdin.each_char { |char| | |
line << char | |
if line =~ /\(y\/n\): $/ | |
stdout.puts gets | |
end | |
line = '' if char == '\n' | |
} | |
rescue Errno::EIO | |
end | |
Process.wait(pid) | |
end | |
rescue PTY::ChildExited | |
error_out "[Error] Cmd: #{command}" | |
end | |
error_out "[Error] Cmd: #{command}" unless $?.exitstatus == 0 | |
return true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment