Ask question to your $stdin. This command is useful in conjunction with a CLI
old_pwd = ask "Tell me the old password of mysql"
new_pwd = ask "Tell me the new password of mysql"
run "mysqladmin -u root -p password '#{new_pwd}', :input => old_pwd
# File lib/do/utils.rb, line 26 def ask(*args) question = args[0] options = args.last.is_a?(Hash) ? args.pop : {} result = "" `stty -echo` if options[:silent] loop do log("\e[36m%s: \e[0m" % question, false) result = $stdin.gets.chomp break if options[:allow_blank] || result != "" end `stty echo` && log("\n", false) if options[:silent] result end
Print the text into logger buffer, if you want to change the stream edit the constant DO_LOGGER
# File lib/do/utils.rb, line 63 def log(text, new_line=false) text += "\n" if new_line && text[-1] != \n\ DO_LOGGER.print text end
Execute a local command
# File lib/do/utils.rb, line 71 def run(*cmds) cmd = cmds.map(&:to_s).join(' ') log DO_LOGGER_FORMAT % [:do, :local, cmd] system cmd end
This is generally used when calling DO::Server from a CLI
[srv1, srv2, srv3].each do |server| server.run "long task" server.wait # because we want to see all outputs before start with new one end
# File lib/do/utils.rb, line 12 def wait log "\e[36mPress ENTER to continue...\e[0m" $stdin.gets end
Ask a yes/no question and return true if it is equal to y or yes
if yes?("Do you want to proceed?")
do_some
end
# File lib/do/utils.rb, line 48 def yes?(question) result = "" question += "?" if question[-1] != ?? loop do log("\e[36m%s (y/n): \e[0m" % question, false) result = $stdin.gets.chomp break if result =~ /y|yes|n|no/ end return result =~ /y|yes/ end
Generated with the Darkfish Rdoc Generator 2.