module Facter::OptionsValidator

Constants

DUPLICATED_OPTIONS_RULES
INVALID_PAIRS_RULES
LOG_LEVEL

Public Class Methods

conflicting_configs(options) click to toggle source
# File lib/facter/framework/core/options/options_validator.rb, line 53
def self.conflicting_configs(options)
  no_ruby = !options[:ruby]
  no_custom_facts = !options[:custom_facts]
  puppet = options[:puppet]
  custom_dir = options[:custom_dir].nil? ? false : options[:custom_dir].any?
  default_external_dir = Facter::OptionStore.default_external_dir
  # --puppet/-p option adds an external directory and is not an explicitly
  # set external_dir from cli or config file
  default_external_dir += [Puppet[:pluginfactdest]] if puppet && const_defined?('Puppet')
  external_dir = if options[:external_dir].nil? || options[:external_dir].none?
                   false
                 else
                   options[:external_dir] != default_external_dir
                 end
  [
    { 'no-ruby' => no_ruby, 'custom-dir' => custom_dir },
    { 'no-external-facts' => !options[:external_facts], 'external-dir' => external_dir },
    { 'no-custom-facts' => no_custom_facts, 'custom-dir' => custom_dir },
    { 'no-ruby' => no_ruby, 'puppet' => puppet },
    { 'no-custom-facts' => no_custom_facts, 'puppet' => puppet }
  ]
end
validate(options) click to toggle source
# File lib/facter/framework/core/options/options_validator.rb, line 20
def self.validate(options)
  DUPLICATED_OPTIONS_RULES.each do |key, value|
    if options.include?(key) && options.include?(value)
      write_error_and_exit("option #{value} cannot be specified more than once.")
    end
  end

  INVALID_PAIRS_RULES.each do |key, value|
    common_values = [value].flatten & options
    if options.include?(key) && common_values.any?
      write_error_and_exit("#{key} and #{common_values.first} options conflict: please specify only one.")
    end
  end
end
validate_configs(options) click to toggle source
# File lib/facter/framework/core/options/options_validator.rb, line 43
def self.validate_configs(options)
  conflicting_configs(options).each do |op|
    next unless op.values[0] && op.values[1]

    message = "#{op.keys[0]} and #{op.keys[1]} options conflict: please specify only one"
    write_error_and_exit(message)
  end
  validate_log_options(options)
end
validate_log_options(options) click to toggle source
# File lib/facter/framework/core/options/options_validator.rb, line 76
def self.validate_log_options(options)
  # TODO: find a better way to do this
  return if options[:debug] == true && options[:log_level] == :debug
  return if options[:verbose] == true && options[:log_level] == :info

  return unless [options[:debug],
                 options[:verbose],
                 options[:log_level] != Facter::DEFAULT_LOG_LEVEL]
                .count(true) > 1

  message = 'debug, verbose, and log-level options conflict: please specify only one.'
  write_error_and_exit(message)
end
write_error_and_exit(message) click to toggle source
# File lib/facter/framework/core/options/options_validator.rb, line 35
def self.write_error_and_exit(message)
  log = Facter::Log.new(self)
  log.error(message, true)
  Facter::Cli.start(['--help'])

  exit 1
end