Module: Ghaki::Account::Hostname

Extended by:
SynOpts
Included in:
Base
Defined in:
lib/ghaki/account/hostname.rb

Constant Summary collapse

HOSTNAME_RETRY_MAX =

CONSTANTS

3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SynOpts

attr_syn_opts

Instance Attribute Details

#hostnameObject

OBJECT METHODS



39
40
41
# File 'lib/ghaki/account/hostname.rb', line 39

def hostname
  @hostname
end

Class Method Details

.get_envObject




27
28
29
30
31
32
33
34
# File 'lib/ghaki/account/hostname.rb', line 27

def Hostname.get_env
  if ENV.has_key?('HOSTNAME')
    ENV['HOSTNAME']
  else
    require 'socket'
    Socket.gethostname
  end
end

Instance Method Details

#ask_hostname(opts = {}) ⇒ Object




62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ghaki/account/hostname.rb', line 62

def ask_hostname opts={}
  o_prompt,r_prompt = _hostname_prompt(opts), nil
  defval = opts[:default] || @hostname
  defval = Hostname.get_env if defval == :env
  (1 .. (opts[:retry_max] || HOSTNAME_RETRY_MAX) ).each do
    self.hostname = HighLine.new.ask( r_prompt||o_prompt ) do |q|
      q.echo = true
      q.default = defval unless defval.nil?
    end
    return @hostname if valid_hostname?
    r_prompt ||= '* TRY AGAIN * ' + o_prompt
  end
  raise InvalidHostnameError, "Invalid Hostname: #{@hostname}"
end

#valid_hostname?Boolean


Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/ghaki/account/hostname.rb', line 54

def valid_hostname?
  return false if @hostname.nil?
  return false if @hostname.empty?
  return false unless @hostname =~ %r{\A\S+\z}o
  return true
end