Class: HieraSimulator::Cli
- Inherits:
-
Object
- Object
- HieraSimulator::Cli
- Defined in:
- lib/hiera-simulator/cli.rb
Overview
Command Line Interface
Class Method Summary collapse
-
.command_line_override(options, config) ⇒ Object
Overrides from command line.
-
.execute(options, config_in) ⇒ Object
Handle command line interface.
-
.find_dir(dir) ⇒ String
Find a user-supplied directory, either absolute path, or relative to the current working directory.
-
.parse_options ⇒ Hash
Parse command line options.
-
.print_answer(answer, options) ⇒ Object
Print answer.
-
.run(options_in = nil, config_in = {}) ⇒ Object
Drive the command line interface - this is called externally.
Class Method Details
.command_line_override(options, config) ⇒ Object
Overrides from command line
41 42 43 44 45 46 47 |
# File 'lib/hiera-simulator/cli.rb', line 41 def self.command_line_override(, config) merges = {} [:hiera_yaml_path, :json_datadir, :yaml_datadir, :factdir, :fact_file].each do |key| merges[key.to_s] = [key] if .key?(key) && ![key].nil? end config.merge!(merges) unless merges.empty? end |
.execute(options, config_in) ⇒ Object
Handle command line interface
16 17 18 19 20 21 22 23 24 |
# File 'lib/hiera-simulator/cli.rb', line 16 def self.execute(, config_in) config = HieraSimulator::Config.new(config_in) command_line_override(, config) config.validate facts = HieraSimulator::Facts.facts(config, [:hostname], [:stringify_facts]) raise "No facts found for host #{[:hostname]}" if facts.empty? = { resolution_type: [:resolution_type], verbose: [:verbose] } HieraSimulator::HieraLookup.lookup([:key], config, facts, ) end |
.find_dir(dir) ⇒ String
Find a user-supplied directory, either absolute path, or relative to the current working directory. If directory cannot be find, raise exception.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/hiera-simulator/cli.rb', line 128 def self.find_dir(dir) result = [] dir_array = dir.is_a?(Array) ? dir : [dir] dir_array.each do |dir_ent| if File.directory?(dir_ent) result << File.absolute_path(dir_ent) elsif File.directory?(File.join(File.absolute_path(Dir.getwd), factdir)) result << File.join(File.absolute_path(Dir.getwd), dir_ent) else raise Errno::ENOENT, "Specified directory #{dir_ent} does not exist or is inaccessible" end end return result[0] unless dir.is_a?(Array) result end |
.parse_options ⇒ Hash
Parse command line options
51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/hiera-simulator/cli.rb', line 51 def self. result = { verbose: false, resolution_type: :priority, hostname: nil, fact_file: nil, stringify_facts: nil, } OptionParser.new do |opts| opts. = 'Usage: hiera-simulator -n <Node_FQDN> [options] key' opts.on('--hiera-yaml-path PATH', 'Path to hiera.yaml file') do |path| dir = find_dir(File.dirname(path)) result[:hiera_yaml_path] = File.absolute_path(File.join(dir, File.basename(path))) raise Errno::ENOENT, 'The specified hiera.yaml file does not exist' unless File.file?(result[:hiera_yaml_path]) end opts.on('--yaml-datadir DIR', 'YAML data directory') do |datadir| result[:yaml_datadir] = find_dir(datadir) end opts.on('--json-datadir DIR', 'JSON data directory') do |datadir| result[:json_datadir] = find_dir(datadir) end opts.on('-d', '--debug', 'Turn on Hiera debugging/verbose mode') do result[:verbose] = true end opts.on('--hostname FQDN', '-n', 'Use facts from last run of FQDN') do |fqdn| result[:hostname] = fqdn end opts.on('--fact-file PATH', 'Use facts from the specified file (.yaml/.json only)') do |path| result[:fact_file] = path result[:hostname] = '.' if result[:hostname].nil? end opts.on('--factsdir DIR', '-f', 'Base directory of YAML facts') do |factdir| result[:factdir] = find_dir(factdir) end opts.on('--puppetdb', '-p', 'Force lookup of facts in puppetdb') do |puppetdb| result[:puppetdb] = puppetdb end opts.on('--array', 'Return answer as an array') do result[:resolution_type] = :array end opts.on('--hash', 'Return answer as an hash') do result[:resolution_type] = :hash end opts.on('--[no-]stringify-facts', 'Override default stringify facts behavior') do |x| result[:stringify_facts] = x end end.parse! if ARGV.empty? puts 'ERROR: You did not specify a data item to look up!' exit 1 end if result[:hostname].nil? puts 'ERROR: You did not specify a host name to simulate (-n <hostname>)' exit 1 end result[:key] = ARGV.delete_at(0) result end |
.print_answer(answer, options) ⇒ Object
Print answer
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hiera-simulator/cli.rb', line 27 def self.print_answer(answer, ) STDERR.puts "Hiera version = #{Hiera::VERSION}" if [:verbose] if answer.nil? puts 'nil' return 1 elsif answer.is_a?(String) puts answer else pp answer end 0 end |
.run(options_in = nil, config_in = {}) ⇒ Object
Drive the command line interface - this is called externally
9 10 11 12 13 |
# File 'lib/hiera-simulator/cli.rb', line 9 def self.run( = nil, config_in = {}) = .nil? ? : answer = execute(, config_in) exit print_answer(answer, ) end |