Skip to content

Instantly share code, notes, and snippets.

@s-tajima
Created November 21, 2013 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-tajima/7576824 to your computer and use it in GitHub Desktop.
Save s-tajima/7576824 to your computer and use it in GitHub Desktop.
Method for realtime output of external command.
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