module ManageIQ::ApplianceConsole::Prompts

Constants

CLEAR_CODE
DOMAIN_REGEXP
HOSTNAME_REGEXP
INT_REGEXP
IPV4_REGEXP
IPV6_REGEXP
IP_REGEXP
NONE_REGEXP

Public Instance Methods

are_you_sure?(clarifier = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 44
def are_you_sure?(clarifier = nil)
  clarifier = " you want to #{clarifier}" if clarifier && !clarifier.include?("want")
  agree("Are you sure#{clarifier}? (Y/N): ")
end
ask_for_disk(disk_name, verify = true, silent = false) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 140
def ask_for_disk(disk_name, verify = true, silent = false)
  require "linux_admin"
  disks = LinuxAdmin::Disk.local.select { |d| d.partitions.empty? }

  if disks.empty?
    say("No partition found for #{disk_name}. You probably want to add an unpartitioned disk and try again.") unless silent
  else
    default_choice = disks.size == 1 ? "1" : nil
    disk = ask_with_menu(
      disk_name,
      disks.collect { |d| [("#{d.path}: #{d.size.to_i / 1.megabyte} MB"), d] },
      default_choice
    ) do |q|
      q.choice("Don't partition the disk") { nil }
    end
  end
  if verify && disk.nil?
    say("")
    raise MiqSignalError unless are_you_sure?(" you don't want to partition the #{disk_name}")
  end
  disk
end
ask_for_domain(prompt, default = nil, validate = DOMAIN_REGEXP, error_text = "a valid Domain.", &block) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 58
def ask_for_domain(prompt, default = nil, validate = DOMAIN_REGEXP, error_text = "a valid Domain.", &block)
  just_ask(prompt, default, validate, error_text, &block)
end
ask_for_hostname(prompt, default = nil, validate = HOSTNAME_REGEXP, error_text = "a valid Hostname.", &block) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 86
def ask_for_hostname(prompt, default = nil, validate = HOSTNAME_REGEXP, error_text = "a valid Hostname.", &block)
  just_ask(prompt, default, validate, error_text, &block)
end
ask_for_hour_number(prompt) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 105
def ask_for_hour_number(prompt)
  ask_for_integer(prompt, (0..23))
end
ask_for_integer(prompt, range = nil, default = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 136
def ask_for_integer(prompt, range = nil, default = nil)
  just_ask(prompt, default, INT_REGEXP, "an integer", Integer) { |q| q.in = range if range }
end
ask_for_ip(prompt, default, validate = IP_REGEXP, error_text = "a valid IP Address.", &block) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 62
def ask_for_ip(prompt, default, validate = IP_REGEXP, error_text = "a valid IP Address.", &block)
  just_ask(prompt, default, validate, error_text, &block)
end
ask_for_ip_or_hostname(prompt, default = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 90
def ask_for_ip_or_hostname(prompt, default = nil)
  validation = ->(h) { (h =~ HOSTNAME_REGEXP || h =~ IP_REGEXP) && h.length > 0 }
  ask_for_ip(prompt, default, validation, "a valid Hostname or IP Address.")
end
ask_for_ip_or_hostname_or_none(prompt, default = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 95
def ask_for_ip_or_hostname_or_none(prompt, default = nil)
  validation = Regexp.union(NONE_REGEXP, HOSTNAME_REGEXP, IP_REGEXP)
  ask_for_ip(prompt, default, validation, "a valid Hostname or IP Address.").gsub(NONE_REGEXP, "")
end
ask_for_ip_or_none(prompt, default = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 66
def ask_for_ip_or_none(prompt, default = nil)
  ask_for_ip(prompt, default, Regexp.union(NONE_REGEXP, IP_REGEXP)).gsub(NONE_REGEXP, "")
end
ask_for_ipv4(prompt, default) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 70
def ask_for_ipv4(prompt, default)
  ask_for_ip(prompt, default, IPV4_REGEXP)
end
ask_for_ipv4_or_none(prompt, default = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 74
def ask_for_ipv4_or_none(prompt, default = nil)
  ask_for_ip(prompt, default, Regexp.union(NONE_REGEXP, IPV4_REGEXP)).gsub(NONE_REGEXP, "")
end
ask_for_ipv6(prompt, default) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 78
def ask_for_ipv6(prompt, default)
  ask_for_ip(prompt, default, IPV6_REGEXP)
end
ask_for_ipv6_or_none(prompt, default = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 82
def ask_for_ipv6_or_none(prompt, default = nil)
  ask_for_ip(prompt, default, Regexp.union(IPV6_REGEXP, NONE_REGEXP)).gsub(NONE_REGEXP, '')
end
ask_for_many(prompt, collective = nil, default = nil, max_length = 255, max_count = 6) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 117
def ask_for_many(prompt, collective = nil, default = nil, max_length = 255, max_count = 6)
  collective ||= "#{prompt}s"
  validate = ->(p) { (p.length < max_length) && (p.split(/[\s,;]+/).length <= max_count) }
  error_message = "up to #{max_count} #{prompt}s separated by a space and up to #{max_length} characters"
  just_ask(collective, default, validate, error_message).split(/[\s,;]+/).collect(&:strip)
end
ask_for_month_day_number(prompt) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 113
def ask_for_month_day_number(prompt)
  ask_for_integer(prompt, (1..31))
end
ask_for_password(prompt, default = nil) { |q| ... } click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 124
def ask_for_password(prompt, default = nil)
  pass = just_ask(prompt, default.present? ? "********" : nil) do |q|
    q.echo = '*'
    yield q if block_given?
  end
  pass == "********" ? (default || "") : pass
end
ask_for_schedule_frequency(prompt, default = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 100
def ask_for_schedule_frequency(prompt, default = nil)
  validation = ->(h) { %w(hourly daily weekly monthly).include?(h) }
  just_ask(prompt, default, validation, "hourly, daily, weekly or monthly")
end
ask_for_string(prompt, default = nil) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 132
def ask_for_string(prompt, default = nil)
  just_ask(prompt, default)
end
ask_for_uri(prompt, expected_scheme, opts = {}) { |q| ... } click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 18
def ask_for_uri(prompt, expected_scheme, opts = {})
  require 'uri'
  just_ask(prompt, nil, nil, 'a valid URI') do |q|
    q.validate = lambda do |a|
      # Convert all backslashes in the URI to forward slashes and strip whitespace
      a.tr!('\\', '/')
      a.strip!
      u = URI(a)
      # validate it has a hostname/ip and a share
      u.scheme == expected_scheme &&
        (u.host =~ HOSTNAME_REGEXP || u.hostname =~ IP_REGEXP) &&
        (opts[:optional_path] || !u.path.empty?)
    end
    yield q if block_given?
  end
end
ask_for_week_day_number(prompt) click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 109
def ask_for_week_day_number(prompt)
  ask_for_integer(prompt, (0..6))
end
ask_with_menu(prompt, options, default = nil, clear_screen_after = true) { |menu| ... } click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 175
def ask_with_menu(prompt, options, default = nil, clear_screen_after = true)
  say("#{prompt}\n\n")

  default = default_to_index(default, options)
  selection = nil
  choose do |menu|
    menu.default      = default
    menu.index        = :number
    menu.index_suffix = ") "
    menu.prompt       = "\nChoose the #{prompt.downcase}:#{" |#{default}|" if default} "
    options.each { |o, v| menu.choice(o) { |c| selection = v || c } }
    yield menu if block_given?
  end
  clear_screen if clear_screen_after
  selection
end
ask_yn?(prompt, default = nil) { |q| ... } click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 49
def ask_yn?(prompt, default = nil)
  ask("#{prompt}? (Y/N): ") do |q|
    q.default = default if default
    q.validate = ->(p) { (p.blank? && default) || %w(y n).include?(p.downcase[0]) }
    q.responses[:not_valid] = "Please provide yes or no."
    yield q if block_given?
  end.downcase[0] == 'y'
end
clear_screen() click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 40
def clear_screen
  print CLEAR_CODE
end
default_to_index(default, options) click to toggle source

use the integer index for menu prompts ensure default is a string

# File lib/manageiq/appliance_console/prompts.rb, line 165
def default_to_index(default, options)
  return unless default
  default_index = if options.kind_of?(Hash)
                    options.values.index(default) || options.keys.index(default)
                  else
                    options.index(default)
                  end
  default_index ? (default_index.to_i + 1).to_s : default.to_s
end
just_ask(prompt, default = nil, validate = nil, error_text = nil, klass = nil) { |q| ... } click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 192
def just_ask(prompt, default = nil, validate = nil, error_text = nil, klass = nil)
  ask("Enter the #{prompt}: ", klass) do |q|
    q.readline = true
    q.default = default.to_s if default
    q.validate = validate if validate
    q.responses[:not_valid] = error_text ? "Please provide #{error_text}" : "Please provide in the specified format"
    yield q if block_given?
  end
end
press_any_key() click to toggle source
# File lib/manageiq/appliance_console/prompts.rb, line 35
def press_any_key
  say("\nPress any key to continue.")
  STDIN.noecho(&:getch)
end