Module: CloudFlock::Interface::CLI::Opts

Extended by:
Opts
Included in:
Opts
Defined in:
lib/cloudflock/interface/cli/opts.rb,
lib/cloudflock/interface/cli/opts/servers.rb

Overview

Public: The CLI Opts module provides methods to abstract and simplify loading configuration information, parsing options and providing context to the application.

Constant Summary collapse

CONFIG_LOCATION =
"~/.flockrc"

Instance Method Summary collapse

Instance Method Details

#argv_servers(opts, options) ⇒ Object

Internal: Extend the Opts module to provide options specific to the servers migration CLI utility.

opts - OptionParser object to which to add options. options - Hash containing options flags and settings for the application.

Returns nothing.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cloudflock/interface/cli/opts/servers.rb', line 9

def argv_servers(opts, options)
  opts.on('-o', '--opencloud', 'Perform an Open Cloud Servers migration') do
    options[:function] = :opencloud
  end
  opts.on('-s', '--servers', 'Perform a Cloud Servers migration') do
    options[:function] = :servers
  end
  opts.on('-r', '--resume', 'Resume a migration') do
    options[:resume] = true
  end
end

#parse(function = '') ⇒ Object

Public: Open config files if applicable, overwriting default options with configuration passed files first, then with any options supplied via the command line.

function - String or Symbol containing the name of the function to parse

arguments for, if applicable. (default: '')

Returns a Hash containing an option to value map.



17
18
19
20
21
22
23
24
25
26
# File 'lib/cloudflock/interface/cli/opts.rb', line 17

def parse(function = '')
  options = parse_config_file(CONFIG_LOCATION)

  argv = parse_argv(function)
  if argv[:config_file].kind_of?(String)
    options.merge(parse_config_file(argv[:config_file]))
  end

  options.merge(argv)
end

#parse_argv(function) ⇒ Object

Internal: Parse and return options passed via the command line.

function - String or Symbol containing the name of the function to parse

arguments for.  This will cause the OptionParser to search for a
argv_function name for the given name of the function.

Returns a Hash containing an option to value map.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cloudflock/interface/cli/opts.rb', line 60

def parse_argv(function)
  options = {}

  optparse = OptionParser.new do |opts|
    opts.on('-v', '--verbose', 'Be verbose') do
      options[:verbose] = true
    end

    opts.on('-c', '--config FILE', 'Load configuration saved from previous' +
                                   ' session (useful with -r)') do |file|
      unless File.exists?(File.expand_path(file))
        puts "Configuration file #{file} does not exist!  Exiting."
        exit
      end
      options[:config_file] = File.expand_path(file)
    end

    # Pull in extra options if applicable
    function = ("argv_" + function.to_s).to_sym
    if CloudFlock::Interface::CLI::Opts.respond_to?(function)
      CloudFlock::Interface::CLI::Opts.send(function, opts, options)
    end
  end
  optparse.parse!

  options
end

#parse_config_file(file) ⇒ Object

Internal: Open and parse a given config file.

file - String containing path to a configuration file which will be parsed.

Returns a Hash containing option-value mappings.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloudflock/interface/cli/opts.rb', line 33

def parse_config_file(file)
  options = {}
  return options if file.nil?

  config_string = ""
  if File.exists?(File.expand_path(file))
    config_string = File.open(File.expand_path(file)).read
  end

  config_string.each_line do |line|
    line.gsub!(/#.*/, "").strip!
    next if line.empty?

    opt,value = line.split(/\s*/, 2)
    options[opt.to_sym] = value
  end

  options
end