Module: DO::Utils

Included in:
Commands, Server
Defined in:
lib/do/utils.rb

Instance Method Summary collapse

Instance Method Details

#ask(*args) ⇒ Object

Ask question to your $stdin. This command is useful in conjunction with a CLI

Example

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


26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

#log(text, new_line = false) ⇒ Object

Print the text into logger buffer, if you want to change the stream edit the constant DO_LOGGER



63
64
65
66
# 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

#run(*cmds) ⇒ Object Also known as: sh

Execute a local command



71
72
73
74
75
# 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

#waitObject

This is generally used when calling DO::Server from a CLI

Examples:

[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


12
13
14
15
# File 'lib/do/utils.rb', line 12

def wait
  log "\e[36mPress ENTER to continue...\e[0m"
  $stdin.gets
end

#yes?(question) ⇒ Boolean

Ask a yes/no question and return true if it is equal to y or yes

Examples:

if yes?("Do you want to proceed?")
  do_some
end

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# 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/i
  end
  return result =~ /y|yes/i
end