Module: CloudFlock::App

Extended by:
App
Includes:
ConsoleGlitter
Included in:
App, Common
Defined in:
lib/cloudflock/app.rb,
lib/cloudflock/error.rb,
lib/cloudflock/errstr.rb,
lib/cloudflock/app/files-migrate.rb,
lib/cloudflock/app/common/cleanup.rb,
lib/cloudflock/app/common/servers.rb,
lib/cloudflock/app/server-migrate.rb,
lib/cloudflock/app/server-profile.rb,
lib/cloudflock/app/common/rackspace.rb,
lib/cloudflock/app/common/watchdogs.rb,
lib/cloudflock/app/common/exclusions.rb,
lib/cloudflock/app/common/cleanup/unix.rb,
lib/cloudflock/app/common/exclusions/unix.rb,
lib/cloudflock/app/common/platform_action.rb,
lib/cloudflock/app/common/exclusions/unix/centos.rb,
lib/cloudflock/app/common/exclusions/unix/redhat.rb

Overview

Public: The App module provides any functionality that is expected to be used by all CLI applications.

Defined Under Namespace

Modules: Common, Rackspace, Watchdogs Classes: FilesMigrate, ServerMigrate, ServerProfile

Instance Method Summary collapse

Instance Method Details

#check_option(options, name, prompt, prompt_options = {}) ⇒ Object

Public: Check if an option is set; return the value if so, otherwise prompt the user for a response.

options - Hash containing options to test against. name - The key in the options Hash expected to contain the

response desired.

prompt - Prompt to present to the user. prompt_options - Options to pass along to ConsoleGlitter::UI#prompt.

(default: {})

Returns the contents of the options or else a String if options is nil.



24
25
26
27
28
# File 'lib/cloudflock/app.rb', line 24

def check_option(options, name, prompt, prompt_options = {})
  return options[name] unless options[name].nil?

  options[name] = UI.prompt(prompt, prompt_options)
end

#check_option_fs(options, name, prompt, prompt_options = {}) ⇒ Object

Public: Wrap check_option, allowing for filesystem autocompletion in user response if the option is not set.

options - Hash containing options to test against. name - The key in the options Hash expected to contain the

response desired.

prompt - Prompt to present to the user. prompt_options - Options to pass along to ConsoleGlitter::UI#prompt.

(default: {})

Returns the contents of the options or else a String if options is nil.



42
43
44
45
46
# File 'lib/cloudflock/app.rb', line 42

def check_option_fs(options, name, prompt, prompt_options = {})
  return options[name] unless options[name].nil?

  options[name] = UI.prompt_filesystem(prompt, prompt_options)
end

#check_option_pw(options, name, prompt, prompt_options = {}) ⇒ Object

Public: Wrap check_option, disabling local echo for password entry if the option is not set.

options - Hash containing options to test against. name - The key in the options Hash expected to contain the

response desired.

prompt - Prompt to present to the user. prompt_options - Options to pass along to ConsoleGlitter::UI#prompt.

(default: {})

Returns the contents of the options or else a String if options is nil.



60
61
62
63
64
# File 'lib/cloudflock/app.rb', line 60

def check_option_pw(options, name, prompt, prompt_options = {})
  return options[name] unless options[name].nil?

  options[name] = UI.secure_prompt(prompt, prompt_options)
end

#check_option_yn(options, name, prompt, prompt_options = {}) ⇒ Object

Public: Check if an option is set; return the value if so, otherwise prompt the user for a response.

options - Hash containing options to test against. name - The key in the options Hash expected to contain the

response desired.

prompt - Prompt to present to the user. prompt_options - Options to pass along to ConsoleGlitter::UI#prompt_yn.

(default: {})

Returns true or false.



77
78
79
80
81
# File 'lib/cloudflock/app.rb', line 77

def check_option_yn(options, name, prompt, prompt_options = {})
  return(options[name] ? true : false) unless options[name].nil?

  options[name] = UI.prompt_yn(prompt, prompt_options)
end

#load_config_if_present(options) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/cloudflock/app.rb', line 83

def load_config_if_present(options)
  if File.file?(options[:config_file].to_s)
    YAML.load_file(options[:config_file]).merge(options)
  else
    options
  end
rescue Psych::SyntaxError, NoMethodError
  options
end

#parse_options(options = {}) ⇒ Object

Public: Parse options and expose global options which are expected to be useful in any CLI application.

options - Hash containing already-set options.

Yields the OptionsParser object in use if a block is given.

Returns a Hash.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cloudflock/app.rb', line 101

def parse_options(options = {})
  opts = OptionParser.new

  yield opts if block_given?

  opts.separator ''
  opts.separator 'Global Options:'

  opts.on('-c', '--config FILE', 'Specify configuration file') do |file|
    options[:config_file] = File.expand_path(file)
  end

  opts.on_tail('--version', 'Show Version Information') do
    puts "CloudFlock v#{CloudFlock::VERSION}"
    exit
  end

  opts.on_tail('-?', '--help', 'Show this Message') do
    puts opts
    exit
  end

  opts.parse!(ARGV)

  load_config_if_present(options)
rescue OptionParser::MissingArgument, OptionParser::InvalidOption => error
  puts error.message.capitalize
  puts
  ARGV.clear
  ARGV.unshift('-?')
  retry
end