Class: WAB::Impl::Configuration
- Inherits:
-
Object
- Object
- WAB::Impl::Configuration
- Defined in:
- lib/wab/impl/configuration.rb
Overview
Handles the configuration for a Shell Implementation and the Ruby Runner
Instance Attribute Summary collapse
-
#map ⇒ Object
Returns the value of attribute map.
Instance Method Summary collapse
-
#add_options(opts, options, path = '') ⇒ Object
Walks the options map and calls
opts.onfor each option so that all are provided when--helpis called. -
#arg_append(path, val, parse) ⇒ Object
Appends an arg to an array in the configuration.
- #get(path) ⇒ Object (also: #[])
-
#initialize(usage, options) ⇒ Configuration
constructor
A new instance of Configuration.
-
#parse_conf_file(file) ⇒ Object
Returns a Hash containing data obtained by parsing a UNIX style conf file.
-
#parse_config_file(file) ⇒ Object
Returns a Hash of configuration data.
- #set(path, value) ⇒ Object (also: #[]=)
- #set_map(node, path, value) ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(usage, options) ⇒ Configuration
Returns a new instance of Configuration.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/wab/impl/configuration.rb', line 14 def initialize(usage, ) @map = {} @opts = OptionParser.new(usage) config_file = nil log_increase = 0 @opts.on('-c', '--config PATH', String, 'Configuration file.') { |c| config_file = c } @opts.on('-r', '--require LIBRARY', String, 'Require.') { |r| require r } @opts.on('-v', '--verbose', 'Increase verbosity.') { log_increase += 1 } @opts.on('-h', '--help', 'Show this display.') { puts @opts.help; Process.exit!(0) } # Process command-line arguments and append them, in order, to an empty hash @map (@opts, ) modes = @opts.parse(ARGV) @map[:mode] = 0 < modes.length ? modes[0] : 'run' @map[:rest] = modes[1..-1] if 1 < modes.length # Move the @map sideways and replace with defaults. command_line_map = @map @map = {} build_default_map() config_file = './config/wabur.conf' if config_file.nil? # Load it and merge th econfig file into @map. @map = merge_map(@map, parse_config_file(config_file)) # Merge in the command line map. @map = merge_map(@map, command_line_map) unless command_line_map.empty? # Apply the log_increase. log_level_adjust(log_increase) end |
Instance Attribute Details
#map ⇒ Object
Returns the value of attribute map.
12 13 14 |
# File 'lib/wab/impl/configuration.rb', line 12 def map @map end |
Instance Method Details
#add_options(opts, options, path = '') ⇒ Object
Walks the options map and calls opts.on for each option so that all are provided when --help is called.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/wab/impl/configuration.rb', line 54 def (opts, , path='') .each_pair { |k,v| next unless v.is_a?(Hash) key_path = path.empty? ? k.to_s : "#{path}.#{k}" if v.has_key?(:val) default = v[:val] switch = "--#{key_path} #{v[:arg]}" doc_with_default = "#{v[:doc]} Default: #{default}" if default.is_a?(Array) if v.has_key?(:short) opts.on(v[:short], switch, String, v[:doc]) { |val| arg_append(key_path, val, v[:parse]) } else opts.on(switch, String, v[:doc]) { |val| arg_append(key_path, val, v[:parse]) } end elsif v.has_key?(:short) # If val is nil then the option was a flag so set to true opts.on(v[:short], switch, v[:type], doc_with_default) { |val| set(key_path, val || true) } else # If val is nil then the option was a flag so set to true opts.on(switch, v[:type], doc_with_default) { |val| set(key_path, val || true) } end else (opts, v, key_path) end } end |
#arg_append(path, val, parse) ⇒ Object
Appends an arg to an array in the configuration.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/wab/impl/configuration.rb', line 82 def arg_append(path, val, parse) parts = val.split('=') if 1 < parts.length val = {} parse.each_index { |i| val[parse[i].to_sym] = parts[i] } end a = get(path) if a.nil? a = [] set(path, a) end a.push(val) end |
#get(path) ⇒ Object Also known as: []
152 153 154 |
# File 'lib/wab/impl/configuration.rb', line 152 def get(path) Utils.get_node(@map, path) end |
#parse_conf_file(file) ⇒ Object
Returns a Hash containing data obtained by parsing a UNIX style conf file.
For example, handler.sample.count = 63 and handler.sample.path = /v1 will be parsed into the following:
{ handler: { sample: { count: 63, path: "/v1" } } }
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/wab/impl/configuration.rb', line 128 def parse_conf_file(file) config = {} File.open(File.(file)) do |f| f.each_line do |line| line.strip! next if line.empty? || line.start_with?('#') key, value = line.split('=').map(&:strip) set_map(config, key, value) end end config end |
#parse_config_file(file) ⇒ Object
Returns a Hash of configuration data.
TBD: Add validation to ensure only a Hash object is returned
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/wab/impl/configuration.rb', line 99 def parse_config_file(file) return {} unless File.exist?(file) case File.extname(file) when /\.conf$/i parse_conf_file(file) when /\.json$/i Oj.load_file(file, mode: :strict, symbol_keys: true) when /\.ya?ml$/i begin require 'safe_yaml/load' SafeYAML.load_file(file) || {} rescue LoadError # Re-raise with a more descriptive message. This should generally # abort the configuration loading. raise LoadError.new(%{Could not load the requested resource. Please install the 'safe_yaml' gem via Bundler or directly, and try loading again. }) end end end |
#set(path, value) ⇒ Object Also known as: []=
147 148 149 |
# File 'lib/wab/impl/configuration.rb', line 147 def set(path, value) set_map(@map, path, value) end |
#set_map(node, path, value) ⇒ Object
142 143 144 145 |
# File 'lib/wab/impl/configuration.rb', line 142 def set_map(node, path, value) return node if path.empty? Utils.set_value(node, path, value) end |
#usage ⇒ Object
48 49 50 |
# File 'lib/wab/impl/configuration.rb', line 48 def usage puts @opts.help end |