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

Commit

Permalink
Merge 2657687 into 0954f8a
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-miclea committed Apr 13, 2020
2 parents 0954f8a + 2657687 commit 9b013bd
Show file tree
Hide file tree
Showing 26 changed files with 855 additions and 901 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Metrics/ClassLength:
- 'lib/custom_facts/util/fact.rb'
- 'lib/resolvers/windows/networking_resolver.rb'
- 'lib/custom_facts/util/collection.rb'
- 'lib/framework/core/options/option_store.rb'

Style/DoubleNegation:
Exclude:
Expand Down
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ RSpec/FilePath:
- 'spec/framework/core/fact_loaders/internal_fact_loader_spec.rb'
- 'spec/framework/core/fact_manager_spec.rb'
- 'spec/framework/core/options/options_validator_spec.rb'
- 'spec/framework/core/options/option_store_spec.rb'
- 'spec/framework/core/options_spec.rb'
- 'spec/framework/core/session_cache_spec.rb'
- 'spec/framework/formatters/fact_formatter_spec.rb'
Expand Down
5 changes: 2 additions & 3 deletions facter.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ global : {

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

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

debug_bool
Facter::Options[:debug] = debug_bool
end

# Returns a fact object by name. If you use this, you still have to
Expand Down Expand Up @@ -187,14 +184,13 @@ 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)
Facter::SessionCache.invalidate_all_caches
Facter::FactCollection.new.build_fact_collection!(resolved_facts)
end

# Check whether printing stack trace is enabled
Expand Down Expand Up @@ -244,14 +240,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
Facter::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(Facter::Options.get)

status = error_check(args, resolved_facts)

Expand Down Expand Up @@ -285,7 +281,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 +306,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,8 +325,10 @@ def error_check(args, resolved_facts)
#
# @api private
def log_blocked_facts
block_list = BlockList.instance.block_list
@logger.debug("blocking collection of #{block_list.join("\s")} facts") if block_list.any? && Options[:block]
block_list = Facter::BlockList.new(Facter::Options[:config]).block_list
return unless block_list.any? && Facter::Options[:block]

@logger.debug("blocking collection of #{block_list.join("\s")} facts")
end

# Used for printing errors regarding CLI user input validation
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.init(Options[:config]).block_list || {}
end
end
end
55 changes: 29 additions & 26 deletions lib/framework/config/config_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,45 @@

module Facter
class ConfigReader
attr_accessor :conf
class << self
attr_accessor :conf

def initialize(config_path = nil)
@config_file_path = config_path || default_path
refresh_config
end
def init(config_path = nil)
config_path ||= default_path
refresh_config(config_path)
self
end

def block_list
@conf['facts'] && @conf['facts']['blocklist']
end
def block_list
@conf['facts'] && @conf['facts']['blocklist']
end

def ttls
@conf['facts'] && @conf['facts']['ttls']
end
def ttls
@conf['facts'] && @conf['facts']['ttls']
end

def global
@conf['global']
end
def global
@conf['global']
end

def cli
@conf['cli']
end
def cli
@conf['cli']
end

def refresh_config
@conf = File.readable?(@config_file_path) ? Hocon.load(@config_file_path) : {}
end
def refresh_config(config_path)
@conf = File.readable?(config_path) ? Hocon.load(config_path) : {}
end

private
private

def default_path
os = OsDetector.instance.identifier
def default_path
os = OsDetector.instance.identifier

windows_path = File.join('C:', 'ProgramData', 'PuppetLabs', 'facter', 'etc', 'facter.conf')
linux_path = File.join('/', 'etc', 'puppetlabs', 'facter', 'facter.conf')
windows_path = File.join('C:', 'ProgramData', 'PuppetLabs', 'facter', 'etc', 'facter.conf')
linux_path = File.join('/', 'etc', 'puppetlabs', 'facter', 'facter.conf')

os == :windows ? windows_path : linux_path
os == :windows ? windows_path : linux_path
end
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].empty?

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
121 changes: 57 additions & 64 deletions lib/framework/core/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,63 @@

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
OptionStore.all
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
ConfigFileOptions.init
store(ConfigFileOptions.get)
end

def init_from_cli(cli_options = {}, user_query = nil)
Facter::OptionStore.cli = true
Facter::OptionStore.show_legacy = false
Facter::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, value|
value = '' if key == 'log_level' && value == 'log_level'
OptionStore.set(key, value)
end
end
end
end
end
Loading

0 comments on commit 9b013bd

Please sign in to comment.