Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Merge ec6e480 into 0f7cb7a
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-miclea committed Apr 9, 2020
2 parents 0f7cb7a + ec6e480 commit 10a0d82
Show file tree
Hide file tree
Showing 21 changed files with 502 additions and 670 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,9 @@ RSpec/DescribedClass:

RSpec/NestedGroups:
Max: 5

Style/ModuleFunction:
Enabled: false

Style/Attr:
Enabled: false
2 changes: 1 addition & 1 deletion facter.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ global : {

cli : {
debug : false,
trace : true,
trace : false,
verbose : false,
log-level : "warn"
}
24 changes: 11 additions & 13 deletions lib/facter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
module Facter
class ResolveCustomFactError < StandardError; end

@options = Options.instance
ConfigFileOptions.init
Options.init
Log.add_legacy_logger(STDOUT)
@logger = Log.new(self)
@already_searched = {}
Expand Down Expand Up @@ -103,8 +104,7 @@ def debugging?
#
# @api public
def debugging(debug_bool)
@options.priority_options[:debug] = debug_bool
@options.refresh
Options[:debug] = debug_bool

debug_bool
end
Expand Down Expand Up @@ -187,11 +187,10 @@ def search_path
#
# @api public
def to_hash
@options.priority_options[:to_hash] = true
@options.refresh

log_blocked_facts

# Options.init_from_api
reset
resolved_facts = Facter::FactManager.instance.resolve_facts
SessionCache.invalidate_all_caches
FactCollection.new.build_fact_collection!(resolved_facts)
Expand Down Expand Up @@ -244,14 +243,14 @@ def version
#
# @api private
def to_user_output(cli_options, *args)
@options.priority_options = { is_cli: true }.merge!(cli_options.map { |(k, v)| [k.to_sym, v] }.to_h)
@options.refresh(args)
cli_options = cli_options.map { |(k, v)| [k.to_sym, v] }.to_h
Options.init_from_cli(cli_options, args)
@logger.info("executed with command line: #{ARGV.drop(1).join(' ')}")
log_blocked_facts

resolved_facts = Facter::FactManager.instance.resolve_facts(args)
SessionCache.invalidate_all_caches
fact_formatter = Facter::FormatterFactory.build(@options)

fact_formatter = Facter::FormatterFactory.build(Options.get)

status = error_check(args, resolved_facts)

Expand Down Expand Up @@ -285,7 +284,6 @@ def add_fact_to_searched_facts(user_query, value)
#
# @return [ResolvedFact]
def resolve_fact(user_query)
@options.refresh([user_query])
user_query = user_query.to_s
resolved_facts = Facter::FactManager.instance.resolve_facts([user_query])
SessionCache.invalidate_all_caches
Expand All @@ -311,7 +309,7 @@ def resolve_fact(user_query)
#
# @api private
def error_check(args, resolved_facts)
if Options.instance[:strict]
if Options[:strict]
missing_names = args - resolved_facts.map(&:user_query).uniq
if missing_names.count.positive?
status = 1
Expand All @@ -330,7 +328,7 @@ def error_check(args, resolved_facts)
#
# @api private
def log_blocked_facts
block_list = BlockList.instance.block_list
block_list = BlockList.new(Options[:config]).block_list
@logger.debug("blocking collection of #{block_list.join("\s")} facts") if block_list.any? && Options[:block]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/framework/cli/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def version
desc '--list-block-groups', 'List block groups'
map ['--list-block-groups'] => :list_block_groups
def list_block_groups(*_args)
puts Facter::BlockList.instance.block_groups.to_yaml.lines[1..-1].join
puts Facter::BlockList.new.block_groups.to_yaml.lines[1..-1].join
end

def self.exit_on_failure?
Expand Down
5 changes: 1 addition & 4 deletions lib/framework/config/block_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module Facter
class BlockList
include Singleton

attr_reader :block_groups, :block_list

def initialize(block_list_path = nil)
Expand All @@ -28,8 +26,7 @@ def blocked_facts

def load_block_groups
@block_groups = File.readable?(@block_groups_file_path) ? Hocon.load(@block_groups_file_path) : {}
options = Options.instance
@block_list = ConfigReader.new(options[:config]).block_list || {}
@block_list = ConfigReader.new(Options[:config]).block_list || {}
end
end
end
Binary file added lib/framework/core/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/framework/core/fact_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def filter_facts!(searched_facts)
private

def filter_legacy_facts!(resolved_facts)
return if Options[:show_legacy]
return unless !Options[:show_legacy] && Options[:user_query].nil?

resolved_facts.reject!(&:legacy?) unless Options[:user_query]
resolved_facts.reject!(&:legacy?)
end
end
end
2 changes: 1 addition & 1 deletion lib/framework/core/fact_loaders/fact_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize

def load(options)
@internal_loader ||= InternalFactLoader.new
@external_fact_loader ||= ExternalFactLoader.new
@external_fact_loader = ExternalFactLoader.new

@facts = []
@external_facts = []
Expand Down
125 changes: 62 additions & 63 deletions lib/framework/core/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,69 @@

module Facter
class Options
include Facter::DefaultOptions
include Facter::ConfigFileOptions
include Facter::PriorityOptions
include Facter::HelperOptions
include Facter::ValidateOptions

include Singleton

attr_accessor :priority_options

def initialize
@options = {}
@priority_options = {}
end

def refresh(user_query = [])
@user_query = user_query
initialize_options

@options
end

def get
@options
end

def [](option)
@options.fetch(option, nil)
end

def custom_dir?
@options[:custom_dir] && @options[:custom_facts]
end

def custom_dir
@options[:custom_dir]
end

def external_dir?
@options[:external_dir] && @options[:external_facts]
end

def external_dir
@options[:external_dir]
end

def self.method_missing(name, *args, &block)
Facter::Options.instance.send(name.to_s, *args, &block)
rescue NoMethodError
super
end

def self.respond_to_missing?(name, include_private) end

private

def initialize_options
@options = { config: @priority_options[:config] }
augment_with_defaults!
augment_with_to_hash_defaults! if @priority_options[:to_hash]
augment_with_config_file_options!(@options[:config])
augment_with_priority_options!(@priority_options)
validate_configs
augment_with_helper_options!(@user_query)
class << self

def cli?
OptionStore.cli
end

def get
options = {}
OptionStore.instance_variables.each do |iv|
variable_name = iv.to_s.delete('@')
options[variable_name.to_sym] = OptionStore.send(variable_name.to_sym)
end
options
end

def [](key)
OptionStore.send(key.to_sym)
end

def []=(key, value)
OptionStore.send("#{key}=".to_sym, value)
end

def custom_dir?
OptionStore.custom_dir && OptionStore.custom_facts
end

def custom_dir
OptionStore.custom_dir.flatten
end

def external_dir?
OptionStore.external_dir && OptionStore.external_facts
end

def external_dir
OptionStore.external_dir
end

def init
OptionStore.cli = false
store(ConfigFileOptions.get)
end

def init_from_cli(cli_options = {}, user_query = nil)
OptionStore.cli = true
OptionStore.show_legacy = false
OptionStore.user_query = user_query

ConfigFileOptions.init(cli_options[:config])
store(ConfigFileOptions.get)
store(cli_options)

Facter::OptionsValidator.validate_configs(get)
end

def store(options)
options.each do |key, val|
val = '' if key == 'log_level' && val == 'log_level'
OptionStore.send("#{key}=".to_sym, val)
end
end
end
end
end
30 changes: 21 additions & 9 deletions lib/framework/core/options/config_file_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

module Facter
module ConfigFileOptions
def augment_with_config_file_options!(config_path)
conf_reader = Facter::ConfigReader.new(config_path)
extend self

attr :options, :cli

def get
@options
end

def init(config_path = nil)
@options = {}
conf_reader ||= Facter::ConfigReader.new(config_path)

augment_config_path(config_path)
if @priority_options[:is_cli]

if Options.cli?
augment_cli(conf_reader.cli)
augment_ruby(conf_reader.global)
end
augment_custom(conf_reader.global)
augment_external(conf_reader.global)
augment_show_legacy(conf_reader.global)
augment_facts(conf_reader.ttls)
augment_facts(conf_reader.ttls, config_path)
end

private
Expand All @@ -34,25 +44,27 @@ def augment_cli(file_cli_conf)
def augment_custom(file_global_conf)
return unless file_global_conf

if @priority_options[:is_cli]
if Options.cli?
@options[:custom_facts] = !file_global_conf['no-custom-facts'] unless file_global_conf['no-custom-facts'].nil?
end

@options[:custom_dir] = [file_global_conf['custom-dir']].flatten unless file_global_conf['custom-dir'].nil?
end

def augment_external(global_conf)
return unless global_conf

if @priority_options[:is_cli]
if Options.cli?
@options[:external_facts] = !global_conf['no-external-facts'] unless global_conf['no-external-facts'].nil?
end

@options[:external_dir] = [global_conf['external-dir']].flatten unless global_conf['external-dir'].nil?
end

def augment_ruby(global_conf)
return unless global_conf

@options[:ruby] = !global_conf['no-ruby'] unless global_conf['no-ruby'].nil?
@options[:ruby] = global_conf['no-ruby'].nil? ? true : !global_conf['no-ruby']
end

def augment_show_legacy(global_conf)
Expand All @@ -61,8 +73,8 @@ def augment_show_legacy(global_conf)
@options[:show_legacy] = global_conf['show-legacy'] unless global_conf['show-legacy'].nil?
end

def augment_facts(ttls)
blocked_facts = Facter::BlockList.instance.blocked_facts
def augment_facts(ttls, config_path)
blocked_facts = Facter::BlockList.new(config_path).blocked_facts
@options[:blocked_facts] = blocked_facts unless blocked_facts.nil?

@options[:ttls] = ttls unless ttls.nil?
Expand Down
35 changes: 0 additions & 35 deletions lib/framework/core/options/default_options.rb

This file was deleted.

Loading

0 comments on commit 10a0d82

Please sign in to comment.